gh-111123: symtable should visit exception handlers before the else block (#111142)

This commit is contained in:
Irit Katriel 2023-10-21 13:38:29 +01:00 committed by GitHub
parent f71cd5394e
commit b578e51f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 2 deletions

View File

@ -106,6 +106,10 @@ Other Language Changes
the file is not accessible.
(Contributed by Moonsik Park in :gh:`82367`.)
* Fixed a bug where a :keyword:`global` decleration in an :keyword:`except` block
is rejected when the global is used in the :keyword:`else` block.
(Contributed by Irit Katriel in :gh:`111123`.)
New Modules
===========

View File

@ -1283,6 +1283,23 @@ class TestSpecifics(unittest.TestCase):
def f():
a if (1 if b else c) else d
def test_global_declaration_in_except_used_in_else(self):
# See gh-111123
code = textwrap.dedent("""\
def f():
try:
pass
%s Exception:
global a
else:
print(a)
""")
g, l = {'a': 5}, {}
for kw in ("except", "except*"):
exec(code % kw, g, l);
@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
# Ensure that compiled code snippets have correct line and column numbers

View File

@ -0,0 +1,2 @@
Fix a bug where a :keyword:`global` declaration in an :keyword:`except` block
is rejected when the global is used in the :keyword:`else` block.

View File

@ -1813,14 +1813,14 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
break;
case Try_kind:
VISIT_SEQ(st, stmt, s->v.Try.body);
VISIT_SEQ(st, stmt, s->v.Try.orelse);
VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
VISIT_SEQ(st, stmt, s->v.Try.orelse);
VISIT_SEQ(st, stmt, s->v.Try.finalbody);
break;
case TryStar_kind:
VISIT_SEQ(st, stmt, s->v.TryStar.body);
VISIT_SEQ(st, stmt, s->v.TryStar.orelse);
VISIT_SEQ(st, excepthandler, s->v.TryStar.handlers);
VISIT_SEQ(st, stmt, s->v.TryStar.orelse);
VISIT_SEQ(st, stmt, s->v.TryStar.finalbody);
break;
case Assert_kind: