Skip to content

Commit 8d3c3a8

Browse files
committed
fix: use temp here
Signed-off-by: yihong0618 <[email protected]>
1 parent 60d200e commit 8d3c3a8

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

Lib/test/test_atexit.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import atexit
22
import os
33
import subprocess
4+
import sys
5+
import tempfile
46
import textwrap
57
import unittest
68
from 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

223233
if __name__ == "__main__":

0 commit comments

Comments
 (0)