1- import faulthandler
21import locale
32import os
43import platform
54import random
65import re
76import sys
8- import sysconfig
9- import tempfile
107import time
118import unittest
9+
10+ from test import support
11+ from test .support import os_helper
12+
1213from test .libregrtest .cmdline import _parse_args , Namespace
1314from test .libregrtest .logger import Logger
1415from test .libregrtest .runtest import (
1516 findtests , split_test_packages , run_single_test , abs_module_name ,
1617 PROGRESS_MIN_TIME , State , RunTests , HuntRefleak ,
17- FilterTuple , TestList , StrPath , StrJSON , TestName )
18+ FilterTuple , TestList , StrJSON , TestName )
1819from test .libregrtest .setup import setup_tests , setup_test_dir
1920from test .libregrtest .pgo import setup_pgo_tests
2021from test .libregrtest .results import TestResults
21- from test .libregrtest .utils import (strip_py_suffix , count , format_duration ,
22- printlist , get_build_info )
23- from test import support
24- from test .support import os_helper
25- from test .support import threading_helper
26-
27-
28- # bpo-38203: Maximum delay in seconds to exit Python (call Py_Finalize()).
29- # Used to protect against threading._shutdown() hang.
30- # Must be smaller than buildbot "1200 seconds without output" limit.
31- EXIT_TIMEOUT = 120.0
22+ from test .libregrtest .utils import (
23+ strip_py_suffix , count , format_duration , StrPath ,
24+ printlist , get_build_info , get_temp_dir , get_work_dir , exit_timeout )
3225
3326
3427class Regrtest :
@@ -104,7 +97,9 @@ def __init__(self, ns: Namespace):
10497 self .verbose : bool = ns .verbose
10598 self .quiet : bool = ns .quiet
10699 if ns .huntrleaks :
107- self .hunt_refleak : HuntRefleak = HuntRefleak (* ns .huntrleaks )
100+ warmups , runs , filename = ns .huntrleaks
101+ filename = os .path .abspath (filename )
102+ self .hunt_refleak : HuntRefleak = HuntRefleak (warmups , runs , filename )
108103 else :
109104 self .hunt_refleak = None
110105 self .test_dir : StrPath | None = ns .testdir
@@ -454,64 +449,6 @@ def display_summary(self):
454449 state = self .get_state ()
455450 print (f"Result: { state } " )
456451
457- @staticmethod
458- def fix_umask ():
459- if support .is_emscripten :
460- # Emscripten has default umask 0o777, which breaks some tests.
461- # see https://github.com/emscripten-core/emscripten/issues/17269
462- old_mask = os .umask (0 )
463- if old_mask == 0o777 :
464- os .umask (0o027 )
465- else :
466- os .umask (old_mask )
467-
468- @staticmethod
469- def select_temp_dir (tmp_dir ):
470- if tmp_dir :
471- tmp_dir = os .path .expanduser (tmp_dir )
472- else :
473- # When tests are run from the Python build directory, it is best practice
474- # to keep the test files in a subfolder. This eases the cleanup of leftover
475- # files using the "make distclean" command.
476- if sysconfig .is_python_build ():
477- tmp_dir = sysconfig .get_config_var ('abs_builddir' )
478- if tmp_dir is None :
479- # bpo-30284: On Windows, only srcdir is available. Using
480- # abs_builddir mostly matters on UNIX when building Python
481- # out of the source tree, especially when the source tree
482- # is read only.
483- tmp_dir = sysconfig .get_config_var ('srcdir' )
484- tmp_dir = os .path .join (tmp_dir , 'build' )
485- else :
486- tmp_dir = tempfile .gettempdir ()
487-
488- return os .path .abspath (tmp_dir )
489-
490- def is_worker (self ):
491- return (self .worker_json is not None )
492-
493- @staticmethod
494- def make_temp_dir (tmp_dir : StrPath , is_worker : bool ):
495- os .makedirs (tmp_dir , exist_ok = True )
496-
497- # Define a writable temp dir that will be used as cwd while running
498- # the tests. The name of the dir includes the pid to allow parallel
499- # testing (see the -j option).
500- # Emscripten and WASI have stubbed getpid(), Emscripten has only
501- # milisecond clock resolution. Use randint() instead.
502- if sys .platform in {"emscripten" , "wasi" }:
503- nounce = random .randint (0 , 1_000_000 )
504- else :
505- nounce = os .getpid ()
506-
507- if is_worker :
508- work_dir = 'test_python_worker_{}' .format (nounce )
509- else :
510- work_dir = 'test_python_{}' .format (nounce )
511- work_dir += os_helper .FS_NONASCII
512- work_dir = os .path .join (tmp_dir , work_dir )
513- return work_dir
514-
515452 @staticmethod
516453 def cleanup_temp_dir (tmp_dir : StrPath ):
517454 import glob
@@ -534,17 +471,16 @@ def main(self, tests: TestList | None = None):
534471
535472 strip_py_suffix (self .cmdline_args )
536473
537- self .tmp_dir = self .select_temp_dir (self .tmp_dir )
538-
539- self .fix_umask ()
474+ self .tmp_dir = get_temp_dir (self .tmp_dir )
540475
541476 if self .want_cleanup :
542477 self .cleanup_temp_dir (self .tmp_dir )
543478 sys .exit (0 )
544479
545- work_dir = self .make_temp_dir (self .tmp_dir , self .is_worker ())
480+ os .makedirs (self .tmp_dir , exist_ok = True )
481+ work_dir = get_work_dir (parent_dir = self .tmp_dir )
546482
547- try :
483+ with exit_timeout () :
548484 # Run the tests in a context manager that temporarily changes the
549485 # CWD to a temporary and writable directory. If it's not possible
550486 # to create or change the CWD, the original CWD will be used.
@@ -556,13 +492,6 @@ def main(self, tests: TestList | None = None):
556492 # processes.
557493
558494 self ._main ()
559- except SystemExit as exc :
560- # bpo-38203: Python can hang at exit in Py_Finalize(), especially
561- # on threading._shutdown() call: put a timeout
562- if threading_helper .can_start_thread :
563- faulthandler .dump_traceback_later (EXIT_TIMEOUT , exit = True )
564-
565- sys .exit (exc .code )
566495
567496 def create_run_tests (self ):
568497 return RunTests (
@@ -579,7 +508,7 @@ def create_run_tests(self):
579508 quiet = self .quiet ,
580509 hunt_refleak = self .hunt_refleak ,
581510 test_dir = self .test_dir ,
582- junit_filename = self .junit_filename ,
511+ use_junit = ( self .junit_filename is not None ) ,
583512 memory_limit = self .memory_limit ,
584513 gc_threshold = self .gc_threshold ,
585514 use_resources = self .use_resources ,
@@ -634,11 +563,6 @@ def run_tests(self) -> int:
634563 self .fail_rerun )
635564
636565 def _main (self ):
637- if self .is_worker ():
638- from test .libregrtest .runtest_mp import worker_process
639- worker_process (self .worker_json )
640- return
641-
642566 if self .want_wait :
643567 input ("Press any key to continue..." )
644568
0 commit comments