]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111123: symtable should visit exception handlers before the else block (#111142)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sat, 21 Oct 2023 12:38:29 +0000 (13:38 +0100)
committerGitHub <noreply@github.com>
Sat, 21 Oct 2023 12:38:29 +0000 (13:38 +0100)
Doc/whatsnew/3.13.rst
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst [new file with mode: 0644]
Python/symtable.c

index 5da5f9380618ebfb44ec47baebe31a67e6ca4140..e830c9170fd3470af03e718dc28a83f923bf6f45 100644 (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
 ===========
 
index c4452e38934cf88cc9e865228a8bad31e4c1b1e8..df6e5e4b55f728125b1c8224cb7c494c1c9887c9 100644 (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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst
new file mode 100644 (file)
index 0000000..f2cebe2
--- /dev/null
@@ -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.
index 75ea9e902f4381a77937452ab368da1104dcca9d..da7fec0ee7cf0cf826ea2d15d638301759c3ed8c 100644 (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: