22
33import multiprocessing
44import multiprocessing .forkserver
5- import os
65import tempfile
76import unittest
87from 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
205184if __name__ == '__main__' :
0 commit comments