Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,23 @@ def test_remove_redundant_nop_edge_case(self):
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
except:
global a
else:
print(a)
""")

g, l = {'a': 5}, {}
exec(code, g, l);
compile(code, "<test>", "exec")


@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
# Ensure that compiled code snippets have correct line and column numbers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where a :keyword:`global` declaration in an :keyword:`except` block
is rejected when the global is used in the :keyword:`else` block.
2 changes: 1 addition & 1 deletion Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,8 +1813,8 @@ 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:
Expand Down