https://github.com/python/cpython/commit/e0d1ef25476875317be6e5ee74e1f254eeb49126
commit: e0d1ef25476875317be6e5ee74e1f254eeb49126
branch: main
author: Pengyu <[email protected]>
committer: Eclips4 <[email protected]>
date: 2026-11-10T21:30:12+02:00
summary:

gh-159054: Check `basicblock_addop()` failure in `push_cold_blocks_to_end()` 
(#159057)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst
M Lib/test/test_compile.py
M Python/flowgraph.c

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index d2069a0ac4214d..b17626252fb537 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -64,6 +64,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, "<test>", "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", "<test>", "exec")
         compile("\r", "<test>", "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 7b8a3e9cf9b57b..fc676dc6feb167 100644
--- a/Python/flowgraph.c
+++ b/Python/flowgraph.c
@@ -3557,8 +3557,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, NULL);
+            RETURN_IF_ERROR(
+                basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT,
+                                 b->b_next->b_label.id, NO_LOCATION, NULL));
             explicit_jump->b_cold = 1;
             explicit_jump->b_next = b->b_next;
             explicit_jump->b_predecessors = 1;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to