File tree Expand file tree Collapse file tree 2 files changed +27
-26
lines changed Expand file tree Collapse file tree 2 files changed +27
-26
lines changed Original file line number Diff line number Diff line change 11import atexit
22import os
3+ import subprocess
34import textwrap
45import unittest
56from test import support
6- from test .support import script_helper
7+ from test .support import SuppressCrashReport , script_helper
78from test .support import threading_helper
89
910class 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
193219if __name__ == "__main__" :
194220 unittest .main ()
Original file line number Diff line number Diff 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 ):
You can’t perform that action at this time.
0 commit comments