Skip to content

Commit 045be92

Browse files
committed
Simplify temporary file handling in tests
Use delete=True (default) for NamedTemporaryFile instead of manually managing cleanup with try/finally blocks. Keep file open during test and let context manager handle automatic deletion. Also remove now- unused os import.
1 parent 70c05d8 commit 045be92

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

Lib/test/test_multiprocessing_forkserver/test_preload.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import multiprocessing
44
import multiprocessing.forkserver
5-
import os
65
import tempfile
76
import unittest
87
from multiprocessing.forkserver import _handle_preload
@@ -124,54 +123,38 @@ class TestHandlePreload(unittest.TestCase):
124123

125124
def test_handle_preload_main_on_error_fail(self):
126125
"""Test that __main__ import failures raise with on_error='fail'."""
127-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
126+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
128127
f.write('raise RuntimeError("test error in __main__")\n')
129-
bad_main_path = f.name
130-
131-
try:
128+
f.flush()
132129
with self.assertRaises(RuntimeError) as cm:
133-
_handle_preload(['__main__'], main_path=bad_main_path, on_error='fail')
130+
_handle_preload(['__main__'], main_path=f.name, on_error='fail')
134131
self.assertIn("test error in __main__", str(cm.exception))
135-
finally:
136-
os.unlink(bad_main_path)
137132

138133
def test_handle_preload_main_on_error_warn(self):
139134
"""Test that __main__ import failures warn with on_error='warn'."""
140-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
135+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
141136
f.write('raise ImportError("test import error")\n')
142-
bad_main_path = f.name
143-
144-
try:
137+
f.flush()
145138
with self.assertWarns(ImportWarning) as cm:
146-
_handle_preload(['__main__'], main_path=bad_main_path, on_error='warn')
139+
_handle_preload(['__main__'], main_path=f.name, on_error='warn')
147140
self.assertIn("Failed to preload __main__", str(cm.warning))
148141
self.assertIn("test import error", str(cm.warning))
149-
finally:
150-
os.unlink(bad_main_path)
151142

152143
def test_handle_preload_main_on_error_ignore(self):
153144
"""Test that __main__ import failures are ignored with on_error='ignore'."""
154-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
145+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
155146
f.write('raise ImportError("test import error")\n')
156-
bad_main_path = f.name
157-
158-
try:
147+
f.flush()
159148
# Should not raise
160-
_handle_preload(['__main__'], main_path=bad_main_path, on_error='ignore')
161-
finally:
162-
os.unlink(bad_main_path)
149+
_handle_preload(['__main__'], main_path=f.name, on_error='ignore')
163150

164151
def test_handle_preload_main_valid(self):
165152
"""Test that valid __main__ preload works."""
166-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
153+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
167154
f.write('test_var = 42\n')
168-
valid_main_path = f.name
169-
170-
try:
171-
_handle_preload(['__main__'], main_path=valid_main_path, on_error='fail')
155+
f.flush()
156+
_handle_preload(['__main__'], main_path=f.name, on_error='fail')
172157
# Should complete without raising
173-
finally:
174-
os.unlink(valid_main_path)
175158

176159
def test_handle_preload_module_on_error_fail(self):
177160
"""Test that module import failures raise with on_error='fail'."""
@@ -191,15 +174,11 @@ def test_handle_preload_module_on_error_ignore(self):
191174

192175
def test_handle_preload_combined(self):
193176
"""Test preloading both __main__ and modules."""
194-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
177+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
195178
f.write('import sys\n')
196-
valid_main_path = f.name
197-
198-
try:
199-
_handle_preload(['__main__', 'os', 'sys'], main_path=valid_main_path, on_error='fail')
179+
f.flush()
180+
_handle_preload(['__main__', 'os', 'sys'], main_path=f.name, on_error='fail')
200181
# Should complete without raising
201-
finally:
202-
os.unlink(valid_main_path)
203182

204183

205184
if __name__ == '__main__':

0 commit comments

Comments
 (0)