From b76251b041f91bbfada6b462771f1ed5ed3fb11b Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 20 Oct 2023 23:11:25 +0100 Subject: [PATCH 1/6] gh-111123: symtable should visit exception handlers before the else block --- Lib/test/test_compile.py | 17 +++++++++++++++++ Python/symtable.c | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index c4452e38934cf8..8741df393f2a53 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -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, "", "exec") + + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): # Ensure that compiled code snippets have correct line and column numbers diff --git a/Python/symtable.c b/Python/symtable.c index 75ea9e902f4381..9b02e81763db5f 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -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: From 1db8a700a52ce009c53685f76e377d6bff687362 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 20 Oct 2023 23:14:14 +0100 Subject: [PATCH 2/6] add news --- .../2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst 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 index 00000000000000..0e74c7d483c87b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst @@ -0,0 +1,2 @@ +Fix bug where a :keyword:`global` decleration in an :keyword:`except` block +is rejected when the global is used in the :keyword:`else` block. From 1e121f83b13b7ad19f01142a42dec2581b00bcdd Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:18:49 +0100 Subject: [PATCH 3/6] typo --- .../2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0e74c7d483c87b..c42fd6fe469d95 100644 --- 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 @@ -1,2 +1,2 @@ -Fix bug where a :keyword:`global` decleration in an :keyword:`except` block +Fix bug where a :keyword:`global` declaration in an :keyword:`except` block is rejected when the global is used in the :keyword:`else` block. From 72fd4d1ce08e888a06ff56720c95e1cd7ee5ac9a Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Sat, 21 Oct 2023 11:43:58 +0100 Subject: [PATCH 4/6] except* too --- Lib/test/test_compile.py | 6 +++--- Python/symtable.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 8741df393f2a53..df6e5e4b55f728 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1289,15 +1289,15 @@ def test_global_declaration_in_except_used_in_else(self): def f(): try: pass - except: + %s Exception: global a else: print(a) """) g, l = {'a': 5}, {} - exec(code, g, l); - compile(code, "", "exec") + for kw in ("except", "except*"): + exec(code % kw, g, l); @requires_debug_ranges() diff --git a/Python/symtable.c b/Python/symtable.c index 9b02e81763db5f..da7fec0ee7cf0c 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -1819,8 +1819,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) 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: From 2216553c2e170d241b3f7589902462c119e328d1 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Sat, 21 Oct 2023 11:46:41 +0100 Subject: [PATCH 5/6] add what's new --- Doc/whatsnew/3.13.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 5da5f9380618eb..e830c9170fd347 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -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 =========== From eafa6d1fb19db4c8994b9a41cbb878b54ce33cbc Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Sat, 21 Oct 2023 11:47:35 +0100 Subject: [PATCH 6/6] typo Co-authored-by: Pablo Galindo Salgado --- .../2023-10-20-23-14-06.gh-issue-111123.jjVc3M.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c42fd6fe469d95..f2cebe287db3ee 100644 --- 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 @@ -1,2 +1,2 @@ -Fix bug where a :keyword:`global` declaration in an :keyword:`except` block +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.