MCPcopy Index your code
hub / github.com/python/cpython / test_exec_set_nomemory_hang

Method test_exec_set_nomemory_hang

Lib/test/test_exceptions.py:1929–1955  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

import_moduleFunction · 0.90
dedentFunction · 0.90
SuppressCrashReportClass · 0.90
assertInMethod · 0.80
assertGreaterMethod · 0.80
waitMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected