mirror of https://github.com/python/cpython
gh-111123: symtable should visit exception handlers before the else block (#111142)
This commit is contained in:
parent
f71cd5394e
commit
b578e51f02
|
@ -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
|
||||
===========
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue