Skip to content

Commit 5ab08c7

Browse files
committed
fix: address comments
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 26ce31b commit 5ab08c7

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

Lib/test/test_exceptions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,36 @@ def test_memory_error_in_subinterp(self):
18431843
rc, _, err = script_helper.assert_python_ok("-c", code)
18441844
self.assertIn(b'MemoryError', err)
18451845

1846+
@cpython_only
1847+
# Python built with Py_TRACE_REFS fail with a fatal error in
1848+
# _PyRefchain_Trace() on memory allocation error.
1849+
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1850+
def test_exec_set_nomemory_hang(self):
1851+
import_module("_testcapi")
1852+
# gh-134163: Test case that triggers no memory hang condition
1853+
# The frame_lasti need to upper 257,
1854+
# because when calling PyLong_FromLong, malloc is not invoked,
1855+
# so no MemError is triggered
1856+
# we need to warm up the memory to reproduce the issue
1857+
warmup_code = "a = list(range(0, 1))\n" * 20
1858+
user_input = warmup_code + dedent("""
1859+
try:
1860+
import _testcapi
1861+
_testcapi.set_nomemory(0)
1862+
b = list(range(1000, 2000))
1863+
except Exception as e:
1864+
import traceback
1865+
traceback.print_exc()
1866+
""")
1867+
with SuppressCrashReport():
1868+
with script_helper.spawn_python('-c', user_input) as p:
1869+
p.wait()
1870+
output = p.stdout.read()
1871+
1872+
self.assertIn(p.returncode, (0, 1))
1873+
self.assertGreater(len(output), 0) # At minimum, should not hang
1874+
self.assertIn(b"MemoryError", output)
1875+
18461876

18471877
class NameErrorTests(unittest.TestCase):
18481878
def test_name_error_has_name(self):

Lib/test/test_repl.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
SuppressCrashReport,
1515
SHORT_TIMEOUT,
1616
)
17-
from test.support.script_helper import kill_python, spawn_python
17+
from test.support.script_helper import kill_python
1818
from test.support.import_helper import import_module
1919

2020
try:
@@ -98,35 +98,6 @@ def test_no_memory(self):
9898
# Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr.
9999
self.assertIn(p.returncode, (1, 120))
100100

101-
@cpython_only
102-
# Python built with Py_TRACE_REFS fail with a fatal error in
103-
# _PyRefchain_Trace() on memory allocation error.
104-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
105-
def test_exec_set_nomemory_hang(self):
106-
import_module("_testcapi")
107-
# gh-134163: Test case that triggers no memory hang condition
108-
# The frame_lasti need to upper 257,
109-
# because when calling PyLong_FromLong, malloc is not invoked,
110-
# so no MemError is triggered
111-
# we need to warm up the memory to reproduce the issue
112-
warmup_code = "a = list(range(0, 1))\n" * 20
113-
user_input = warmup_code + dedent("""
114-
try:
115-
import _testcapi
116-
_testcapi.set_nomemory(0)
117-
b = list(range(1000, 2000))
118-
except Exception as e:
119-
import traceback
120-
traceback.print_exc()
121-
""")
122-
with SuppressCrashReport():
123-
p = spawn_python('-c', user_input)
124-
output = kill_python(p)
125-
126-
self.assertIn(p.returncode, (0, 1, 120))
127-
self.assertGreater(len(output), 0) # At minimum, should not hang
128-
self.assertIn(b"MemoryError", output)
129-
130101
@cpython_only
131102
def test_multiline_string_parsing(self):
132103
# bpo-39209: Multiline string tokens need to be handled in the tokenizer

0 commit comments

Comments
 (0)