https://github.com/python/cpython/commit/dc00dcfe88f9d764e0ad41adc5ffeedd0e34a073
commit: dc00dcfe88f9d764e0ad41adc5ffeedd0e34a073
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2025-11-11T19:53:31Z
summary:

[3.14] gh-140193: Forward port test_exec_set_nomemory_hang from 3.13 
(GH-140187) (#141420)

gh-140193: Forward port test_exec_set_nomemory_hang from 3.13 (GH-140187)

* chore: test_exec_set_nomemory_hang from 3.13
* fix: apply comments
* Update Lib/test/test_exceptions.py
* Update Lib/test/test_exceptions.py
* fix: windows too long name 60 times is enough

---------
(cherry picked from commit 0f09bda643d778fb20fb79fecdfd09f20f9d9717)

Signed-off-by: yihong0618 <[email protected]>
Co-authored-by: yihong <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>

files:
M Lib/test/test_exceptions.py

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 273f73937183b8..4f5ffe66eaf585 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1914,6 +1914,39 @@ def test_keyerror_context(self):
             exc2 = None
 
 
+    @cpython_only
+    # Python built with Py_TRACE_REFS fail with a fatal error in
+    # _PyRefchain_Trace() on memory allocation error.
+    @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
+    def test_exec_set_nomemory_hang(self):
+        import_module("_testcapi")
+        # gh-134163: A MemoryError inside code that was wrapped by a try/except
+        # block would lead to an infinite loop.
+
+        # The frame_lasti needs to be greater than 257 to prevent
+        # PyLong_FromLong() from returning cached integers, which
+        # don't require a memory allocation. Prepend some dummy code
+        # to artificially increase the instruction index.
+        warmup_code = "a = list(range(0, 1))\n" * 60
+        user_input = warmup_code + dedent("""
+            try:
+                import _testcapi
+                _testcapi.set_nomemory(0)
+                b = list(range(1000, 2000))
+            except Exception as e:
+                import traceback
+                traceback.print_exc()
+            """)
+        with SuppressCrashReport():
+            with script_helper.spawn_python('-c', user_input) as p:
+                p.wait()
+                output = p.stdout.read()
+
+        self.assertIn(p.returncode, (0, 1))
+        self.assertGreater(len(output), 0)  # At minimum, should not hang
+        self.assertIn(b"MemoryError", output)
+
+
 class NameErrorTests(unittest.TestCase):
     def test_name_error_has_name(self):
         try:

_______________________________________________
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