@@ -4432,35 +4432,29 @@ def test_dict_arg(self):
4432
4432
def _extract_logrecord_process_name (key , logMultiprocessing , conn = None ):
4433
4433
prev_logMultiprocessing = logging .logMultiprocessing
4434
4434
logging .logMultiprocessing = logMultiprocessing
4435
- old_mp = None
4436
4435
try :
4437
4436
import multiprocessing as mp
4438
4437
name = mp .current_process ().name
4439
4438
4440
4439
r1 = logging .makeLogRecord ({'msg' : f'msg1_{ key } ' })
4441
4440
4442
- old_mp = sys .modules ['multiprocessing' ]
4443
- del sys .modules ['multiprocessing' ]
4444
- assert old_mp
4445
-
4446
- r2 = logging .makeLogRecord ({'msg' : f'msg2_{ key } ' })
4441
+ with support .swap_item (sys .modules , 'multiprocessing' , None ):
4442
+ # We need to restore the previous `multiprocessing` module:
4443
+ # https://bugs.python.org/issue45128
4444
+ r2 = logging .makeLogRecord ({'msg' : f'msg2_{ key } ' })
4447
4445
4448
4446
results = {'processName' : name ,
4449
4447
'r1.processName' : r1 .processName ,
4450
4448
'r2.processName' : r2 .processName ,
4451
4449
}
4452
4450
finally :
4453
4451
logging .logMultiprocessing = prev_logMultiprocessing
4454
- if old_mp is not None :
4455
- # We need to restore the previous `multiprocessing` module:
4456
- # https://bugs.python.org/issue45128
4457
- sys .modules ['multiprocessing' ] = old_mp
4458
4452
if conn :
4459
4453
conn .send (results )
4460
4454
else :
4461
4455
return results
4462
4456
4463
- def test_multiprocessing (self ):
4457
+ def test_multiprocessing (self , first_pass = True ):
4464
4458
multiprocessing_imported = 'multiprocessing' in sys .modules
4465
4459
try :
4466
4460
# logMultiprocessing is True by default
@@ -4496,17 +4490,15 @@ def test_multiprocessing(self):
4496
4490
if multiprocessing_imported :
4497
4491
import multiprocessing
4498
4492
4499
- def test_multiprocessing_again (self ):
4500
- # https://bugs.python.org/issue45128
4501
- import sys
4502
- import multiprocessing .queues
4493
+ if first_pass : # https://bugs.python.org/issue45128
4494
+ import multiprocessing .queues
4503
4495
4504
- self .test_multiprocessing ()
4496
+ self .test_multiprocessing (first_pass = False )
4505
4497
4506
- import multiprocessing
4507
- import multiprocessing .connection
4508
- from multiprocessing .connection import wait
4509
- multiprocessing .connection # It was AttributeError here
4498
+ import multiprocessing
4499
+ import multiprocessing .connection
4500
+ from multiprocessing .connection import wait
4501
+ multiprocessing .connection # It was AttributeError here
4510
4502
4511
4503
def test_optional (self ):
4512
4504
r = logging .makeLogRecord ({})
0 commit comments