From e2de0b6576ed959e239e60b018e99b3293872526 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Jun 2024 16:03:30 +0100 Subject: [PATCH 1/5] gh-120225: fix crash in compiler on empty block at end of exception handler --- Lib/test/test_compile.py | 10 ++++++++++ Python/flowgraph.c | 7 +------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index ba0bcc9c1ced99..ae23aea08d99bc 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1409,6 +1409,16 @@ def f(): for kw in ("except", "except*"): exec(code % kw, g, l); + def test_regression_gh_120225(self): + async def name_4(): + match b'': + case True: + pass + case name_5 if f'e': + {name_3: name_4 async for name_2 in name_5} + case []: + pass + [[]] @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 17617e119fdaa4..74468c45d608eb 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -2304,13 +2304,8 @@ push_cold_blocks_to_end(cfg_builder *g) { if (!IS_LABEL(b->b_next->b_label)) { b->b_next->b_label.id = next_lbl++; } - cfg_instr *prev_instr = basicblock_last_instr(b); - // b cannot be empty because at the end of an exception handler - // there is always a POP_EXCEPT + RERAISE/RETURN - assert(prev_instr); - basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, b->b_next->b_label.id, - prev_instr->i_loc); + NO_LOCATION); explicit_jump->b_cold = 1; explicit_jump->b_next = b->b_next; b->b_next = explicit_jump; From 23662b1fbe097c3fb14039b0939b2d910bdd4a52 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Jun 2024 16:09:14 +0100 Subject: [PATCH 2/5] add news --- .../2024-06-07-16-09-04.gh-issue-120225.kuYf9t.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-06-07-16-09-04.gh-issue-120225.kuYf9t.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-07-16-09-04.gh-issue-120225.kuYf9t.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-07-16-09-04.gh-issue-120225.kuYf9t.rst new file mode 100644 index 00000000000000..d00b9aaa8192e3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-07-16-09-04.gh-issue-120225.kuYf9t.rst @@ -0,0 +1 @@ +Fix crash in compiler on empty block at end of exception handler. From 7d377a2768ee13179e24f25abd5509311625115e Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Jun 2024 16:51:56 +0100 Subject: [PATCH 3/5] fix test_dis --- Lib/test/test_dis.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index b1a1b77c53e8cb..1f23bf8ccfc293 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -578,10 +578,14 @@ async def _asyncwith(c): RETURN_CONST 0 (None) %4d L12: CLEANUP_THROW - L13: JUMP_BACKWARD_NO_INTERRUPT 25 (to L5) - L14: CLEANUP_THROW - L15: JUMP_BACKWARD_NO_INTERRUPT 9 (to L11) - L16: PUSH_EXC_INFO + + -- L13: JUMP_BACKWARD_NO_INTERRUPT 25 (to L5) + +%4d L14: CLEANUP_THROW + + -- L15: JUMP_BACKWARD_NO_INTERRUPT 9 (to L11) + +%4d L16: PUSH_EXC_INFO WITH_EXCEPT_START GET_AWAITABLE 2 LOAD_CONST 0 (None) @@ -627,6 +631,8 @@ async def _asyncwith(c): _asyncwith.__code__.co_firstlineno + 1, _asyncwith.__code__.co_firstlineno + 3, _asyncwith.__code__.co_firstlineno + 1, + _asyncwith.__code__.co_firstlineno + 1, + _asyncwith.__code__.co_firstlineno + 1, _asyncwith.__code__.co_firstlineno + 3, ) From ae14ce7f427158d14620cd0cb612f7f25afd8d19 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Jun 2024 21:34:14 +0100 Subject: [PATCH 4/5] Revert "fix test_dis" This reverts commit 7d377a2768ee13179e24f25abd5509311625115e. --- Lib/test/test_dis.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 1f23bf8ccfc293..b1a1b77c53e8cb 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -578,14 +578,10 @@ async def _asyncwith(c): RETURN_CONST 0 (None) %4d L12: CLEANUP_THROW - - -- L13: JUMP_BACKWARD_NO_INTERRUPT 25 (to L5) - -%4d L14: CLEANUP_THROW - - -- L15: JUMP_BACKWARD_NO_INTERRUPT 9 (to L11) - -%4d L16: PUSH_EXC_INFO + L13: JUMP_BACKWARD_NO_INTERRUPT 25 (to L5) + L14: CLEANUP_THROW + L15: JUMP_BACKWARD_NO_INTERRUPT 9 (to L11) + L16: PUSH_EXC_INFO WITH_EXCEPT_START GET_AWAITABLE 2 LOAD_CONST 0 (None) @@ -631,8 +627,6 @@ async def _asyncwith(c): _asyncwith.__code__.co_firstlineno + 1, _asyncwith.__code__.co_firstlineno + 3, _asyncwith.__code__.co_firstlineno + 1, - _asyncwith.__code__.co_firstlineno + 1, - _asyncwith.__code__.co_firstlineno + 1, _asyncwith.__code__.co_firstlineno + 3, ) From 41f74e0d2ba0669c5e69cf1d14ef4081dad06d7d Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Jun 2024 21:35:33 +0100 Subject: [PATCH 5/5] set b_predecessors to 1 on a new explicit_jump block --- Python/flowgraph.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 74468c45d608eb..aed694aee91f91 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -2308,6 +2308,7 @@ push_cold_blocks_to_end(cfg_builder *g) { NO_LOCATION); explicit_jump->b_cold = 1; explicit_jump->b_next = b->b_next; + explicit_jump->b_predecessors = 1; b->b_next = explicit_jump; /* set target */