@@ -258,6 +258,9 @@ def __call__(self, *args, **kwds):
258
258
class BaseTestCase (object ):
259
259
260
260
ALLOWED_TYPES = ('processes' , 'manager' , 'threads' )
261
+ # If not empty, limit which start method suites run this class.
262
+ START_METHODS : set [str ] = set ()
263
+ start_method = None # set by install_tests_in_module_dict()
261
264
262
265
def assertTimingAlmostEqual (self , a , b ):
263
266
if CHECK_TIMINGS :
@@ -6403,7 +6406,9 @@ def test_atexit(self):
6403
6406
class _TestSpawnedSysPath (BaseTestCase ):
6404
6407
"""Test that sys.path is setup in forkserver and spawn processes."""
6405
6408
6406
- ALLOWED_TYPES = ('processes' ,)
6409
+ ALLOWED_TYPES = {'processes' }
6410
+ # Not applicable to fork which inherits everything from the process as is.
6411
+ START_METHODS = {"forkserver" , "spawn" }
6407
6412
6408
6413
def setUp (self ):
6409
6414
self ._orig_sys_path = list (sys .path )
@@ -6415,11 +6420,8 @@ def setUp(self):
6415
6420
sys .path [:] = [p for p in sys .path if p ] # remove any existing ""s
6416
6421
sys .path .insert (0 , self ._temp_dir )
6417
6422
sys .path .insert (0 , "" ) # Replaced with an abspath in child.
6418
- try :
6419
- self ._ctx_forkserver = multiprocessing .get_context ("forkserver" )
6420
- except ValueError :
6421
- self ._ctx_forkserver = None
6422
- self ._ctx_spawn = multiprocessing .get_context ("spawn" )
6423
+ self .assertIn (self .start_method , self .START_METHODS )
6424
+ self ._ctx = multiprocessing .get_context (self .start_method )
6423
6425
6424
6426
def tearDown (self ):
6425
6427
sys .path [:] = self ._orig_sys_path
@@ -6430,15 +6432,15 @@ def enq_imported_module_names(queue):
6430
6432
queue .put (tuple (sys .modules ))
6431
6433
6432
6434
def test_forkserver_preload_imports_sys_path (self ):
6433
- ctx = self ._ctx_forkserver
6434
- if not ctx :
6435
- self .skipTest ("requires forkserver start method." )
6435
+ if self ._ctx .get_start_method () != "forkserver" :
6436
+ self .skipTest ("forkserver specific test." )
6436
6437
self .assertNotIn (self ._mod_name , sys .modules )
6437
6438
multiprocessing .forkserver ._forkserver ._stop () # Must be fresh.
6438
- ctx .set_forkserver_preload (
6439
+ self . _ctx .set_forkserver_preload (
6439
6440
["test.test_multiprocessing_forkserver" , self ._mod_name ])
6440
- q = ctx .Queue ()
6441
- proc = ctx .Process (target = self .enq_imported_module_names , args = (q ,))
6441
+ q = self ._ctx .Queue ()
6442
+ proc = self ._ctx .Process (
6443
+ target = self .enq_imported_module_names , args = (q ,))
6442
6444
proc .start ()
6443
6445
proc .join ()
6444
6446
child_imported_modules = q .get ()
@@ -6456,23 +6458,19 @@ def enq_sys_path_and_import(queue, mod_name):
6456
6458
queue .put (None )
6457
6459
6458
6460
def test_child_sys_path (self ):
6459
- for ctx in (self ._ctx_spawn , self ._ctx_forkserver ):
6460
- if not ctx :
6461
- continue
6462
- with self .subTest (f"{ ctx .get_start_method ()} start method" ):
6463
- q = ctx .Queue ()
6464
- proc = ctx .Process (target = self .enq_sys_path_and_import ,
6465
- args = (q , self ._mod_name ))
6466
- proc .start ()
6467
- proc .join ()
6468
- child_sys_path = q .get ()
6469
- import_error = q .get ()
6470
- q .close ()
6471
- self .assertNotIn ("" , child_sys_path ) # replaced by an abspath
6472
- self .assertIn (self ._temp_dir , child_sys_path ) # our addition
6473
- # ignore the first element, it is the absolute "" replacement
6474
- self .assertEqual (child_sys_path [1 :], sys .path [1 :])
6475
- self .assertIsNone (import_error , msg = f"child could not import { self ._mod_name } " )
6461
+ q = self ._ctx .Queue ()
6462
+ proc = self ._ctx .Process (
6463
+ target = self .enq_sys_path_and_import , args = (q , self ._mod_name ))
6464
+ proc .start ()
6465
+ proc .join ()
6466
+ child_sys_path = q .get ()
6467
+ import_error = q .get ()
6468
+ q .close ()
6469
+ self .assertNotIn ("" , child_sys_path ) # replaced by an abspath
6470
+ self .assertIn (self ._temp_dir , child_sys_path ) # our addition
6471
+ # ignore the first element, it is the absolute "" replacement
6472
+ self .assertEqual (child_sys_path [1 :], sys .path [1 :])
6473
+ self .assertIsNone (import_error , msg = f"child could not import { self ._mod_name } " )
6476
6474
6477
6475
6478
6476
class MiscTestCase (unittest .TestCase ):
@@ -6669,6 +6667,8 @@ def install_tests_in_module_dict(remote_globs, start_method,
6669
6667
if base is BaseTestCase :
6670
6668
continue
6671
6669
assert set (base .ALLOWED_TYPES ) <= ALL_TYPES , base .ALLOWED_TYPES
6670
+ if base .START_METHODS and start_method not in base .START_METHODS :
6671
+ continue # class not intended for this start method.
6672
6672
for type_ in base .ALLOWED_TYPES :
6673
6673
if only_type and type_ != only_type :
6674
6674
continue
@@ -6682,6 +6682,7 @@ class Temp(base, Mixin, unittest.TestCase):
6682
6682
Temp = hashlib_helper .requires_hashdigest ('sha256' )(Temp )
6683
6683
Temp .__name__ = Temp .__qualname__ = newname
6684
6684
Temp .__module__ = __module__
6685
+ Temp .start_method = start_method
6685
6686
remote_globs [newname ] = Temp
6686
6687
elif issubclass (base , unittest .TestCase ):
6687
6688
if only_type :
0 commit comments