Skip to content

Commit dc00dcf

Browse files
miss-islingtonyihong0618ZeroIntensity
authored
[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 0f09bda) Signed-off-by: yihong0618 <[email protected]> Co-authored-by: yihong <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
1 parent 7618a40 commit dc00dcf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,39 @@ def test_keyerror_context(self):
19141914
exc2 = None
19151915

19161916

1917+
@cpython_only
1918+
# Python built with Py_TRACE_REFS fail with a fatal error in
1919+
# _PyRefchain_Trace() on memory allocation error.
1920+
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1921+
def test_exec_set_nomemory_hang(self):
1922+
import_module("_testcapi")
1923+
# gh-134163: A MemoryError inside code that was wrapped by a try/except
1924+
# block would lead to an infinite loop.
1925+
1926+
# The frame_lasti needs to be greater than 257 to prevent
1927+
# PyLong_FromLong() from returning cached integers, which
1928+
# don't require a memory allocation. Prepend some dummy code
1929+
# to artificially increase the instruction index.
1930+
warmup_code = "a = list(range(0, 1))\n" * 60
1931+
user_input = warmup_code + dedent("""
1932+
try:
1933+
import _testcapi
1934+
_testcapi.set_nomemory(0)
1935+
b = list(range(1000, 2000))
1936+
except Exception as e:
1937+
import traceback
1938+
traceback.print_exc()
1939+
""")
1940+
with SuppressCrashReport():
1941+
with script_helper.spawn_python('-c', user_input) as p:
1942+
p.wait()
1943+
output = p.stdout.read()
1944+
1945+
self.assertIn(p.returncode, (0, 1))
1946+
self.assertGreater(len(output), 0) # At minimum, should not hang
1947+
self.assertIn(b"MemoryError", output)
1948+
1949+
19171950
class NameErrorTests(unittest.TestCase):
19181951
def test_name_error_has_name(self):
19191952
try:

0 commit comments

Comments
 (0)