11"""Tests for forkserver preload functionality."""
22
33import multiprocessing
4- import multiprocessing .forkserver
54import tempfile
65import unittest
7- from multiprocessing . forkserver import _handle_preload
6+ from multiprocessing import forkserver
87
98
109class TestForkserverPreload (unittest .TestCase ):
1110 """Tests for forkserver preload functionality."""
1211
1312 def setUp (self ):
1413 self .ctx = multiprocessing .get_context ('forkserver' )
15- # Ensure clean state for each test
16- multiprocessing .forkserver ._forkserver ._stop ()
14+ forkserver ._forkserver ._stop ()
1715
1816 def tearDown (self ):
19- # Clean up after tests
20- multiprocessing .forkserver ._forkserver ._stop ()
17+ forkserver ._forkserver ._stop ()
2118 self .ctx .set_forkserver_preload ([])
2219
2320 @staticmethod
@@ -27,10 +24,8 @@ def _send_value(conn, value):
2724
2825 def test_preload_on_error_ignore_default (self ):
2926 """Test that invalid modules are silently ignored by default."""
30- # With on_error='ignore' (default), invalid module is ignored
3127 self .ctx .set_forkserver_preload (['nonexistent_module_xyz' ])
3228
33- # Should be able to start a process without errors
3429 r , w = self .ctx .Pipe (duplex = False )
3530 p = self .ctx .Process (target = self ._send_value , args = (w , 42 ))
3631 p .start ()
@@ -46,7 +41,6 @@ def test_preload_on_error_ignore_explicit(self):
4641 """Test that invalid modules are silently ignored with on_error='ignore'."""
4742 self .ctx .set_forkserver_preload (['nonexistent_module_xyz' ], on_error = 'ignore' )
4843
49- # Should be able to start a process without errors
5044 r , w = self .ctx .Pipe (duplex = False )
5145 p = self .ctx .Process (target = self ._send_value , args = (w , 99 ))
5246 p .start ()
@@ -62,7 +56,6 @@ def test_preload_on_error_warn(self):
6256 """Test that invalid modules emit warnings with on_error='warn'."""
6357 self .ctx .set_forkserver_preload (['nonexistent_module_xyz' ], on_error = 'warn' )
6458
65- # Should still be able to start a process, warnings are in subprocess
6659 r , w = self .ctx .Pipe (duplex = False )
6760 p = self .ctx .Process (target = self ._send_value , args = (w , 123 ))
6861 p .start ()
@@ -78,26 +71,20 @@ def test_preload_on_error_fail_breaks_context(self):
7871 """Test that invalid modules with on_error='fail' breaks the forkserver."""
7972 self .ctx .set_forkserver_preload (['nonexistent_module_xyz' ], on_error = 'fail' )
8073
81- # The forkserver should fail to start when it tries to import
82- # The exception is raised during p.start() when trying to communicate
83- # with the failed forkserver process
8474 r , w = self .ctx .Pipe (duplex = False )
8575 try :
8676 p = self .ctx .Process (target = self ._send_value , args = (w , 42 ))
8777 with self .assertRaises ((EOFError , ConnectionError , BrokenPipeError )) as cm :
88- p .start () # Exception raised here
89- # Verify that the helpful note was added
78+ p .start ()
9079 notes = getattr (cm .exception , '__notes__' , [])
9180 self .assertTrue (notes , "Expected exception to have __notes__" )
9281 self .assertIn ('Forkserver process may have crashed' , notes [0 ])
9382 finally :
94- # Ensure pipes are closed even if exception is raised
9583 w .close ()
9684 r .close ()
9785
9886 def test_preload_valid_modules_with_on_error_fail (self ):
9987 """Test that valid modules work fine with on_error='fail'."""
100- # Valid modules should work even with on_error='fail'
10188 self .ctx .set_forkserver_preload (['os' , 'sys' ], on_error = 'fail' )
10289
10390 r , w = self .ctx .Pipe (duplex = False )
@@ -127,7 +114,7 @@ def test_handle_preload_main_on_error_fail(self):
127114 f .write ('raise RuntimeError("test error in __main__")\n ' )
128115 f .flush ()
129116 with self .assertRaises (RuntimeError ) as cm :
130- _handle_preload (['__main__' ], main_path = f .name , on_error = 'fail' )
117+ forkserver . _handle_preload (['__main__' ], main_path = f .name , on_error = 'fail' )
131118 self .assertIn ("test error in __main__" , str (cm .exception ))
132119
133120 def test_handle_preload_main_on_error_warn (self ):
@@ -136,7 +123,7 @@ def test_handle_preload_main_on_error_warn(self):
136123 f .write ('raise ImportError("test import error")\n ' )
137124 f .flush ()
138125 with self .assertWarns (ImportWarning ) as cm :
139- _handle_preload (['__main__' ], main_path = f .name , on_error = 'warn' )
126+ forkserver . _handle_preload (['__main__' ], main_path = f .name , on_error = 'warn' )
140127 self .assertIn ("Failed to preload __main__" , str (cm .warning ))
141128 self .assertIn ("test import error" , str (cm .warning ))
142129
@@ -145,40 +132,36 @@ def test_handle_preload_main_on_error_ignore(self):
145132 with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' ) as f :
146133 f .write ('raise ImportError("test import error")\n ' )
147134 f .flush ()
148- # Should not raise
149- _handle_preload (['__main__' ], main_path = f .name , on_error = 'ignore' )
135+ forkserver ._handle_preload (['__main__' ], main_path = f .name , on_error = 'ignore' )
150136
151137 def test_handle_preload_main_valid (self ):
152138 """Test that valid __main__ preload works."""
153139 with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' ) as f :
154140 f .write ('test_var = 42\n ' )
155141 f .flush ()
156- _handle_preload (['__main__' ], main_path = f .name , on_error = 'fail' )
157- # Should complete without raising
142+ forkserver ._handle_preload (['__main__' ], main_path = f .name , on_error = 'fail' )
158143
159144 def test_handle_preload_module_on_error_fail (self ):
160145 """Test that module import failures raise with on_error='fail'."""
161146 with self .assertRaises (ModuleNotFoundError ):
162- _handle_preload (['nonexistent_test_module_xyz' ], on_error = 'fail' )
147+ forkserver . _handle_preload (['nonexistent_test_module_xyz' ], on_error = 'fail' )
163148
164149 def test_handle_preload_module_on_error_warn (self ):
165150 """Test that module import failures warn with on_error='warn'."""
166151 with self .assertWarns (ImportWarning ) as cm :
167- _handle_preload (['nonexistent_test_module_xyz' ], on_error = 'warn' )
152+ forkserver . _handle_preload (['nonexistent_test_module_xyz' ], on_error = 'warn' )
168153 self .assertIn ("Failed to preload module" , str (cm .warning ))
169154
170155 def test_handle_preload_module_on_error_ignore (self ):
171156 """Test that module import failures are ignored with on_error='ignore'."""
172- # Should not raise
173- _handle_preload (['nonexistent_test_module_xyz' ], on_error = 'ignore' )
157+ forkserver ._handle_preload (['nonexistent_test_module_xyz' ], on_error = 'ignore' )
174158
175159 def test_handle_preload_combined (self ):
176160 """Test preloading both __main__ and modules."""
177161 with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' ) as f :
178162 f .write ('import sys\n ' )
179163 f .flush ()
180- _handle_preload (['__main__' , 'os' , 'sys' ], main_path = f .name , on_error = 'fail' )
181- # Should complete without raising
164+ forkserver ._handle_preload (['__main__' , 'os' , 'sys' ], main_path = f .name , on_error = 'fail' )
182165
183166
184167if __name__ == '__main__' :
0 commit comments