@@ -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 :
@@ -6349,7 +6352,9 @@ def test_atexit(self):
6349
6352
class _TestSpawnedSysPath (BaseTestCase ):
6350
6353
"""Test that sys.path is setup in forkserver and spawn processes."""
6351
6354
6352
- ALLOWED_TYPES = ('processes' ,)
6355
+ ALLOWED_TYPES = {'processes' }
6356
+ # Not applicable to fork which inherits everything from the process as is.
6357
+ START_METHODS = {"forkserver" , "spawn" }
6353
6358
6354
6359
def setUp (self ):
6355
6360
self ._orig_sys_path = list (sys .path )
@@ -6361,11 +6366,8 @@ def setUp(self):
6361
6366
sys .path [:] = [p for p in sys .path if p ] # remove any existing ""s
6362
6367
sys .path .insert (0 , self ._temp_dir )
6363
6368
sys .path .insert (0 , "" ) # Replaced with an abspath in child.
6364
- try :
6365
- self ._ctx_forkserver = multiprocessing .get_context ("forkserver" )
6366
- except ValueError :
6367
- self ._ctx_forkserver = None
6368
- self ._ctx_spawn = multiprocessing .get_context ("spawn" )
6369
+ self .assertIn (self .start_method , self .START_METHODS )
6370
+ self ._ctx = multiprocessing .get_context (self .start_method )
6369
6371
6370
6372
def tearDown (self ):
6371
6373
sys .path [:] = self ._orig_sys_path
@@ -6376,15 +6378,15 @@ def enq_imported_module_names(queue):
6376
6378
queue .put (tuple (sys .modules ))
6377
6379
6378
6380
def test_forkserver_preload_imports_sys_path (self ):
6379
- ctx = self ._ctx_forkserver
6380
- if not ctx :
6381
- self .skipTest ("requires forkserver start method." )
6381
+ if self ._ctx .get_start_method () != "forkserver" :
6382
+ self .skipTest ("forkserver specific test." )
6382
6383
self .assertNotIn (self ._mod_name , sys .modules )
6383
6384
multiprocessing .forkserver ._forkserver ._stop () # Must be fresh.
6384
- ctx .set_forkserver_preload (
6385
+ self . _ctx .set_forkserver_preload (
6385
6386
["test.test_multiprocessing_forkserver" , self ._mod_name ])
6386
- q = ctx .Queue ()
6387
- proc = ctx .Process (target = self .enq_imported_module_names , args = (q ,))
6387
+ q = self ._ctx .Queue ()
6388
+ proc = self ._ctx .Process (
6389
+ target = self .enq_imported_module_names , args = (q ,))
6388
6390
proc .start ()
6389
6391
proc .join ()
6390
6392
child_imported_modules = q .get ()
@@ -6402,23 +6404,19 @@ def enq_sys_path_and_import(queue, mod_name):
6402
6404
queue .put (None )
6403
6405
6404
6406
def test_child_sys_path (self ):
6405
- for ctx in (self ._ctx_spawn , self ._ctx_forkserver ):
6406
- if not ctx :
6407
- continue
6408
- with self .subTest (f"{ ctx .get_start_method ()} start method" ):
6409
- q = ctx .Queue ()
6410
- proc = ctx .Process (target = self .enq_sys_path_and_import ,
6411
- args = (q , self ._mod_name ))
6412
- proc .start ()
6413
- proc .join ()
6414
- child_sys_path = q .get ()
6415
- import_error = q .get ()
6416
- q .close ()
6417
- self .assertNotIn ("" , child_sys_path ) # replaced by an abspath
6418
- self .assertIn (self ._temp_dir , child_sys_path ) # our addition
6419
- # ignore the first element, it is the absolute "" replacement
6420
- self .assertEqual (child_sys_path [1 :], sys .path [1 :])
6421
- self .assertIsNone (import_error , msg = f"child could not import { self ._mod_name } " )
6407
+ q = self ._ctx .Queue ()
6408
+ proc = self ._ctx .Process (
6409
+ target = self .enq_sys_path_and_import , args = (q , self ._mod_name ))
6410
+ proc .start ()
6411
+ proc .join ()
6412
+ child_sys_path = q .get ()
6413
+ import_error = q .get ()
6414
+ q .close ()
6415
+ self .assertNotIn ("" , child_sys_path ) # replaced by an abspath
6416
+ self .assertIn (self ._temp_dir , child_sys_path ) # our addition
6417
+ # ignore the first element, it is the absolute "" replacement
6418
+ self .assertEqual (child_sys_path [1 :], sys .path [1 :])
6419
+ self .assertIsNone (import_error , msg = f"child could not import { self ._mod_name } " )
6422
6420
6423
6421
6424
6422
class MiscTestCase (unittest .TestCase ):
@@ -6615,6 +6613,8 @@ def install_tests_in_module_dict(remote_globs, start_method,
6615
6613
if base is BaseTestCase :
6616
6614
continue
6617
6615
assert set (base .ALLOWED_TYPES ) <= ALL_TYPES , base .ALLOWED_TYPES
6616
+ if base .START_METHODS and start_method not in base .START_METHODS :
6617
+ continue # class not intended for this start method.
6618
6618
for type_ in base .ALLOWED_TYPES :
6619
6619
if only_type and type_ != only_type :
6620
6620
continue
@@ -6628,6 +6628,7 @@ class Temp(base, Mixin, unittest.TestCase):
6628
6628
Temp = hashlib_helper .requires_hashdigest ('sha256' )(Temp )
6629
6629
Temp .__name__ = Temp .__qualname__ = newname
6630
6630
Temp .__module__ = __module__
6631
+ Temp .start_method = start_method
6631
6632
remote_globs [newname ] = Temp
6632
6633
elif issubclass (base , unittest .TestCase ):
6633
6634
if only_type :
0 commit comments