Skip to content

Commit d664393

Browse files
committed
fix: move tests to atexit
Signed-off-by: yihong0618 <[email protected]>
1 parent 0f0cbef commit d664393

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

Lib/test/test_atexit.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import atexit
22
import os
3+
import subprocess
34
import textwrap
45
import unittest
56
from test import support
6-
from test.support import script_helper
7+
from test.support import SuppressCrashReport, script_helper
78
from test.support import threading_helper
89

910
class GeneralTest(unittest.TestCase):
@@ -189,6 +190,31 @@ def callback():
189190
self.assertEqual(os.read(r, len(expected)), expected)
190191
os.close(r)
191192

193+
# Python built with Py_TRACE_REFS fail with a fatal error in
194+
# _PyRefchain_Trace() on memory allocation error.
195+
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
196+
def test_atexit_with_low_memory(self):
197+
# gh-140080: Test that setting low memory after registering an atexit
198+
# callback doesn't cause an infinite loop during finalization.
199+
user_input = textwrap.dedent("""
200+
import atexit
201+
import _testcapi
202+
203+
def callback():
204+
pass
205+
206+
atexit.register(callback)
207+
# Simulate low memory condition
208+
_testcapi.set_nomemory(0)
209+
""")
210+
with SuppressCrashReport():
211+
with script_helper.spawn_python('-c', user_input,
212+
stderr=subprocess.PIPE) as p:
213+
p.wait()
214+
p.stdout.read()
215+
216+
self.assertIn(p.returncode, (0, 1))
217+
192218

193219
if __name__ == "__main__":
194220
unittest.main()

Lib/test/test_exceptions.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -342,31 +342,6 @@ def testMemoryErrorBigSource(self, size):
342342
with self.assertRaisesRegex(OverflowError, "Parser column offset overflow"):
343343
compile(src, '<fragment>', 'exec')
344344

345-
@cpython_only
346-
# Python built with Py_TRACE_REFS fail with a fatal error in
347-
# _PyRefchain_Trace() on memory allocation error.
348-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
349-
def test_atexit_with_low_memory(self):
350-
# gh-140080: Test that setting low memory after registering an atexit
351-
# callback doesn't cause an infinite loop during finalization.
352-
user_input = dedent("""
353-
import atexit
354-
import _testcapi
355-
356-
def callback():
357-
pass
358-
359-
atexit.register(callback)
360-
# Simulate low memory condition
361-
_testcapi.set_nomemory(0)
362-
""")
363-
with SuppressCrashReport():
364-
with script_helper.spawn_python('-c', user_input) as p:
365-
p.wait()
366-
output = p.stdout.read()
367-
368-
# The key point is that the process should exit (not hang)
369-
self.assertIn(p.returncode, (0, 1))
370345

371346
@cpython_only
372347
def testSettingException(self):

0 commit comments

Comments
 (0)