11import atexit
22import os
33import subprocess
4+ import sys
5+ import tempfile
46import textwrap
57import unittest
68from test import support
@@ -196,7 +198,7 @@ def callback():
196198 def test_atexit_with_low_memory (self ):
197199 # gh-140080: Test that setting low memory after registering an atexit
198200 # callback doesn't cause an infinite loop during finalization.
199- user_input = textwrap .dedent ("""
201+ code = textwrap .dedent ("""
200202 import atexit
201203 import _testcapi
202204
@@ -207,17 +209,25 @@ def callback():
207209 # Simulate low memory condition
208210 _testcapi.set_nomemory(0)
209211 """ )
210- with SuppressCrashReport ():
211- with script_helper .spawn_python ('-c' , user_input ,
212- stderr = subprocess .PIPE ) as p :
213- p .wait ()
214- stdout = p .stdout .read ()
215- stderr = p .stderr .read ()
216-
217- self .assertIn (p .returncode , (0 , 1 ))
218- self .assertNotIn (b"hello" , stdout )
212+
213+ with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' , delete = False ) as f :
214+ f .write (code )
215+ script = f .name
216+
217+ try :
218+ with SuppressCrashReport ():
219+ proc = subprocess .run (
220+ [sys .executable , script ],
221+ capture_output = True ,
222+ timeout = 10
223+ )
224+ finally :
225+ os .unlink (script )
226+
227+ self .assertIn (proc .returncode , (0 , 1 ))
228+ self .assertNotIn (b"hello" , proc .stdout )
219229 # MemoryError should appear in stderr
220- self .assertIn (b"MemoryError" , stderr )
230+ self .assertIn (b"MemoryError" , proc . stderr )
221231
222232
223233if __name__ == "__main__" :
0 commit comments