From a3829f5631deacecc39930d60c9af8c54fba5850 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 2 Dec 2026 17:47:55 +0000 Subject: [PATCH 1/2] Fix assertion failure in `optimize_load_fast` on fallthrough into an unreachable empty block --- Lib/test/test_compile.py | 25 +++++++++++++++++++ ...-12-02-12-00-00.gh-issue-162952.kR3fTz.rst | 2 ++ Python/flowgraph.c | 6 ++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index b17626252fb537..1b49806d0cfed3 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -646,6 +646,31 @@ def test_compile_redundant_jump_after_convert_pseudo_ops(self): compile(ast.fix_missing_locations(tree), "", "exec") + def test_compile_fallthrough_to_unreachable_block_after_convert_pseudo_ops(self): + # See gh-162952 + code=textwrap.dedent(""" + while name_2: + break + else: + try: + pass + finally: + pass + lambda: 0 + """) + + tree = ast.parse(code) + + # make all instruction locations the same to create redundancies + for node in ast.walk(tree): + if hasattr(node,"lineno"): + del node.lineno + del node.end_lineno + del node.col_offset + del node.end_col_offset + + compile(ast.fix_missing_locations(tree), "", "exec") + def test_compile_ast(self): fname = __file__ if fname.lower().endswith('pyc'): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst new file mode 100644 index 00000000000000..59b79934a6545d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst @@ -0,0 +1,2 @@ +Fix an assertion failure in the compiler when removing a redundant jump +made an unreachable empty block reachable. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index fc676dc6feb167..ecf7c9cff8d02e 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -2855,7 +2855,11 @@ static void load_fast_push_block(basicblock ***sp, basicblock *target, Py_ssize_t start_depth) { - assert(target->b_startdepth >= 0 && target->b_startdepth == start_depth); + if (target->b_startdepth < 0) { + assert(target->b_iused == 0); + target->b_startdepth = (int)start_depth; + } + assert(target->b_startdepth == start_depth); if (!target->b_visited) { target->b_visited = 1; *(*sp)++ = target; From ad55c0e69540ffc7e82169ebf4a6499bcda1cbff Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 4 Dec 2026 17:07:13 +0000 Subject: [PATCH 2/2] Drop news; fix indentation --- Lib/test/test_compile.py | 24 +++++++++---------- ...-12-02-12-00-00.gh-issue-162952.kR3fTz.rst | 2 -- 2 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 1b49806d0cfed3..683d953cea2ea9 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -614,10 +614,10 @@ def test_compile_redundant_jumps_and_nops_after_moving_cold_blocks(self): # make all instruction locations the same to create redundancies for node in ast.walk(tree): if hasattr(node,"lineno"): - del node.lineno - del node.end_lineno - del node.col_offset - del node.end_col_offset + del node.lineno + del node.end_lineno + del node.col_offset + del node.end_col_offset compile(ast.fix_missing_locations(tree), "", "exec") @@ -639,10 +639,10 @@ def test_compile_redundant_jump_after_convert_pseudo_ops(self): # make all instruction locations the same to create redundancies for node in ast.walk(tree): if hasattr(node,"lineno"): - del node.lineno - del node.end_lineno - del node.col_offset - del node.end_col_offset + del node.lineno + del node.end_lineno + del node.col_offset + del node.end_col_offset compile(ast.fix_missing_locations(tree), "", "exec") @@ -664,10 +664,10 @@ def test_compile_fallthrough_to_unreachable_block_after_convert_pseudo_ops(self) # make all instruction locations the same to create redundancies for node in ast.walk(tree): if hasattr(node,"lineno"): - del node.lineno - del node.end_lineno - del node.col_offset - del node.end_col_offset + del node.lineno + del node.end_lineno + del node.col_offset + del node.end_col_offset compile(ast.fix_missing_locations(tree), "", "exec") diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst deleted file mode 100644 index 59b79934a6545d..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-02-12-00-00.gh-issue-162952.kR3fTz.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix an assertion failure in the compiler when removing a redundant jump -made an unreachable empty block reachable.