diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 8b6a07be477dcc..d5be1539df112c 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -59,6 +59,27 @@ def test_assemble_init_allocation_failure(self): """) script_helper.assert_python_ok('-c', code) + @support.requires_subprocess() + @support.nomemtest + def test_push_cold_blocks_to_end_allocation_failure(self): + code = textwrap.dedent(""" + from test import support + + source = "async def f():\\n await x\\n" + for n in range(1, 1000): + with support.inject_memory_error_cm(n, n + 1): + try: + compile(source, "", "exec") + except MemoryError: + continue + break + else: + raise AssertionError("compile() never succeeded") + if n == 1: + raise AssertionError("no MemoryError raised") + """) + script_helper.assert_python_ok('-c', code) + def test_other_newlines(self): compile("\r\n", "", "exec") compile("\r", "", "exec") diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst new file mode 100644 index 00000000000000..eaa01aa05435a7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst @@ -0,0 +1,2 @@ +Fixed a crash when an allocation failure occurred while compiling code +containing cold blocks. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index a5138d1a1fa284..ba6e0b12a33aab 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -3546,8 +3546,9 @@ push_cold_blocks_to_end(cfg_builder *g) { if (!IS_LABEL(b->b_next->b_label)) { b->b_next->b_label.id = next_lbl++; } - basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, b->b_next->b_label.id, - NO_LOCATION); + RETURN_IF_ERROR( + basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, + b->b_next->b_label.id, NO_LOCATION)); explicit_jump->b_cold = 1; explicit_jump->b_next = b->b_next; explicit_jump->b_predecessors = 1;