@@ -111,9 +111,6 @@ def __init__(self, ns: Namespace):
111111 # used by --slow
112112 self .test_times = []
113113
114- # used by --coverage, trace.Trace instance
115- self .tracer = None
116-
117114 # used to display the progress bar "[ 3/100]"
118115 self .start_time = time .perf_counter ()
119116 self .test_count_text = ''
@@ -443,28 +440,18 @@ def display_result(self):
443440 print (count (len (self .run_no_tests ), "test" ), "run no tests:" )
444441 printlist (self .run_no_tests )
445442
446- def run_test (self , test_index , test_name , previous_test , save_modules ):
447- text = test_name
448- if previous_test :
449- text = '%s -- %s' % (text , previous_test )
450- self .display_progress (test_index , text )
451-
452- if self .tracer :
443+ def run_test (self , test_name : str , runtests : RunTests , tracer ):
444+ if tracer is not None :
453445 # If we're tracing code coverage, then we don't exit with status
454446 # if on a false return value from main.
455- cmd = ('result = runtest(self.ns, test_name); '
456- 'self.accumulate_result(result)' )
447+ cmd = ('result = runtest(self.ns, test_name)' )
457448 ns = dict (locals ())
458- self . tracer .runctx (cmd , globals = globals (), locals = ns )
449+ tracer .runctx (cmd , globals = globals (), locals = ns )
459450 result = ns ['result' ]
460451 else :
461452 result = runtest (self .ns , test_name )
462- self .accumulate_result (result )
463453
464- # Unload the newly imported modules (best effort finalization)
465- for module in sys .modules .keys ():
466- if module not in save_modules and module .startswith ("test." ):
467- support .unload (module )
454+ self .accumulate_result (result )
468455
469456 return result
470457
@@ -477,7 +464,9 @@ def run_tests_sequentially(self, runtests):
477464
478465 if coverage :
479466 import trace
480- self .tracer = trace .Trace (trace = False , count = True )
467+ tracer = trace .Trace (trace = False , count = True )
468+ else :
469+ tracer = None
481470
482471 save_modules = sys .modules .keys ()
483472
@@ -491,8 +480,17 @@ def run_tests_sequentially(self, runtests):
491480 for test_index , test_name in enumerate (tests_iter , 1 ):
492481 start_time = time .perf_counter ()
493482
494- result = self .run_test (test_index , test_name ,
495- previous_test , save_modules )
483+ text = test_name
484+ if previous_test :
485+ text = '%s -- %s' % (text , previous_test )
486+ self .display_progress (test_index , text )
487+
488+ result = self .run_test (test_name , runtests , tracer )
489+
490+ # Unload the newly imported modules (best effort finalization)
491+ for module in sys .modules .keys ():
492+ if module not in save_modules and module .startswith ("test." ):
493+ support .unload (module )
496494
497495 if result .must_stop (fail_fast , fail_env_changed ):
498496 break
@@ -508,6 +506,8 @@ def run_tests_sequentially(self, runtests):
508506 if previous_test :
509507 print (previous_test )
510508
509+ return tracer
510+
511511 def display_header (self ):
512512 # Print basic platform information
513513 print ("==" , platform .python_implementation (), * sys .version .split ())
@@ -604,45 +604,28 @@ def set_tests(self, runtests: RunTests):
604604 self .test_count_text = '/{}' .format (len (self .tests ))
605605 self .test_count_width = len (self .test_count_text ) - 1
606606
607- def run_tests (self ):
608- # For a partial run, we do not need to clutter the output.
609- if (self .want_header
610- or not (self .ns .pgo or self .ns .quiet or self .ns .single
611- or self .tests or self .ns .args )):
612- self .display_header ()
613-
614- if self .ns .huntrleaks :
615- warmup , repetitions , _ = self .ns .huntrleaks
616- if warmup < 3 :
617- msg = ("WARNING: Running tests with --huntrleaks/-R and less than "
618- "3 warmup repetitions can give false positives!" )
619- print (msg , file = sys .stdout , flush = True )
620-
621- if self .randomize :
622- print ("Using random seed" , self .random_seed )
623-
624- tests = self .selected
625- runtests = RunTests (tuple (tests ), forever = self .forever )
607+ def run_tests (self , runtests : RunTests ):
626608 self .first_runtests = runtests
627609 self .set_tests (runtests )
628610 if self .ns .use_mp :
629611 self ._run_tests_mp (runtests )
612+ tracer = None
630613 else :
631- self .run_tests_sequentially (runtests )
632- return runtests
614+ tracer = self .run_tests_sequentially (runtests )
615+ return tracer
633616
634- def finalize (self ):
617+ def finalize_tests (self , tracer ):
635618 if self .next_single_filename :
636619 if self .next_single_test :
637620 with open (self .next_single_filename , 'w' ) as fp :
638621 fp .write (self .next_single_test + '\n ' )
639622 else :
640623 os .unlink (self .next_single_filename )
641624
642- if self . tracer :
643- r = self . tracer .results ()
644- r .write_results (show_missing = True , summary = True ,
645- coverdir = self .ns .coverdir )
625+ if tracer is not None :
626+ results = tracer .results ()
627+ results .write_results (show_missing = True , summary = True ,
628+ coverdir = self .ns .coverdir )
646629
647630 if self .ns .runleaks :
648631 os .system ("leaks %d" % os .getpid ())
@@ -858,15 +841,32 @@ def get_exitcode(self):
858841 return exitcode
859842
860843 def action_run_tests (self ):
861- runtests = self .run_tests ()
844+ if self .ns .huntrleaks :
845+ warmup , repetitions , _ = self .ns .huntrleaks
846+ if warmup < 3 :
847+ msg = ("WARNING: Running tests with --huntrleaks/-R and less than "
848+ "3 warmup repetitions can give false positives!" )
849+ print (msg , file = sys .stdout , flush = True )
850+
851+ # For a partial run, we do not need to clutter the output.
852+ if (self .want_header
853+ or not (self .ns .pgo or self .ns .quiet or self .ns .single
854+ or self .tests or self .ns .args )):
855+ self .display_header ()
856+
857+ if self .randomize :
858+ print ("Using random seed" , self .random_seed )
859+
860+ runtests = RunTests (tuple (self .selected ), forever = self .forever )
861+ tracer = self .run_tests (runtests )
862862 self .display_result ()
863863
864864 need_rerun = self .need_rerun
865865 if self .ns .rerun and need_rerun :
866866 self .rerun_failed_tests (need_rerun , runtests )
867867
868868 self .display_summary ()
869- self .finalize ( )
869+ self .finalize_tests ( tracer )
870870
871871 def _main (self ):
872872 if self .is_worker ():
0 commit comments