Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit709c02b

Browse files
committed
gh-109162: libregrtest: add worker.py
Add new worker.py file:* Move create_worker_process() and worker_process() to this file.* Add main() function to worker.py. create_worker_process() now runs the command: "python -m test.libregrtest.worker JSON".* create_worker_process() now starts the worker process in the current working directory. Regrtest now gets the absolute path of the reflog.txt filename: -R command line option filename.* Remove --worker-json command line option. Remove test_regrtest.test_worker_json().Related changes:* Add write_json() and from_json() methods to TestResult.* Rename select_temp_dir() to get_temp_dir() and move it to utils.* Rename make_temp_dir() to get_work_dir() and move it to utils. It no longer calls os.makedirs(): Regrtest.main() now calls it.* Move fix_umask() to utils. The function is now called by setup_tests().* Move StrPath to utils.* Add exit_timeout() context manager to utils.* RunTests: Replace junit_filename (StrPath) with use_junit (bool).
1 parent2dd6a86 commit709c02b

File tree

10 files changed

+238
-213
lines changed

10 files changed

+238
-213
lines changed

‎Lib/test/libregrtest/cmdline.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def _create_parser():
216216
group.add_argument('--wait',action='store_true',
217217
help='wait for user input, e.g., allow a debugger '
218218
'to be attached')
219-
group.add_argument('--worker-json',metavar='ARGS')
220219
group.add_argument('-S','--start',metavar='START',
221220
help='the name of the test at which to start.'+
222221
more_details)

‎Lib/test/libregrtest/main.py‎

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
importfaulthandler
21
importlocale
32
importos
43
importplatform
54
importrandom
65
importre
76
importsys
8-
importsysconfig
9-
importtempfile
107
importtime
118
importunittest
9+
10+
fromtestimportsupport
11+
fromtest.supportimportos_helper
12+
1213
fromtest.libregrtest.cmdlineimport_parse_args,Namespace
1314
fromtest.libregrtest.loggerimportLogger
1415
fromtest.libregrtest.runtestimport (
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)
1819
fromtest.libregrtest.setupimportsetup_tests,setup_test_dir
1920
fromtest.libregrtest.pgoimportsetup_pgo_tests
2021
fromtest.libregrtest.resultsimportTestResults
21-
fromtest.libregrtest.utilsimport (strip_py_suffix,count,format_duration,
22-
printlist,get_build_info)
23-
fromtestimportsupport
24-
fromtest.supportimportos_helper
25-
fromtest.supportimportthreading_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+
fromtest.libregrtest.utilsimport (
23+
strip_py_suffix,count,format_duration,StrPath,
24+
printlist,get_build_info,get_temp_dir,get_work_dir,exit_timeout)
3225

3326

3427
classRegrtest:
@@ -104,7 +97,9 @@ def __init__(self, ns: Namespace):
10497
self.verbose:bool=ns.verbose
10598
self.quiet:bool=ns.quiet
10699
ifns.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-
deffix_umask():
459-
ifsupport.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-
ifold_mask==0o777:
464-
os.umask(0o027)
465-
else:
466-
os.umask(old_mask)
467-
468-
@staticmethod
469-
defselect_temp_dir(tmp_dir):
470-
iftmp_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-
ifsysconfig.is_python_build():
477-
tmp_dir=sysconfig.get_config_var('abs_builddir')
478-
iftmp_dirisNone:
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-
returnos.path.abspath(tmp_dir)
489-
490-
defis_worker(self):
491-
return (self.worker_jsonisnotNone)
492-
493-
@staticmethod
494-
defmake_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-
ifsys.platformin {"emscripten","wasi"}:
503-
nounce=random.randint(0,1_000_000)
504-
else:
505-
nounce=os.getpid()
506-
507-
ifis_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-
returnwork_dir
514-
515452
@staticmethod
516453
defcleanup_temp_dir(tmp_dir:StrPath):
517454
importglob
@@ -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
ifself.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+
withexit_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-
exceptSystemExitasexc:
560-
# bpo-38203: Python can hang at exit in Py_Finalize(), especially
561-
# on threading._shutdown() call: put a timeout
562-
ifthreading_helper.can_start_thread:
563-
faulthandler.dump_traceback_later(EXIT_TIMEOUT,exit=True)
564-
565-
sys.exit(exc.code)
566495

567496
defcreate_run_tests(self):
568497
returnRunTests(
@@ -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_filenameisnotNone),
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-
ifself.is_worker():
638-
fromtest.libregrtest.runtest_mpimportworker_process
639-
worker_process(self.worker_json)
640-
return
641-
642566
ifself.want_wait:
643567
input("Press any key to continue...")
644568

‎Lib/test/libregrtest/refleak.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def get_pooled_int(value):
6868
warmups=hunt_refleak.warmups
6969
runs=hunt_refleak.runs
7070
filename=hunt_refleak.filename
71-
filename=os.path.join(os_helper.SAVEDCWD,filename)
7271
repcount=warmups+runs
7372

7473
# Pre-allocate to ensure that the loop doesn't allocate anything new

‎Lib/test/libregrtest/results.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
fromtest.supportimportTestStats
33

44
fromtest.libregrtest.runtestimport (
5-
TestName,TestTuple,TestList,FilterDict,StrPath,State,
5+
TestName,TestTuple,TestList,FilterDict,State,
66
TestResult,RunTests)
7-
fromtest.libregrtest.utilsimportprintlist,count,format_duration
7+
fromtest.libregrtest.utilsimport (
8+
printlist,count,format_duration,StrPath)
89

910

1011
EXITCODE_BAD_TEST=2

‎Lib/test/libregrtest/runtest.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
fromtest.supportimportos_helper
1818
fromtest.supportimportthreading_helper
1919
fromtest.libregrtest.save_envimportsaved_test_environment
20-
fromtest.libregrtest.utilsimportclear_caches,format_duration,print_warning
20+
fromtest.libregrtest.utilsimport (
21+
clear_caches,format_duration,print_warning,StrPath)
2122

2223

2324
StrJSON=str
24-
StrPath=str
2525
TestName=str
2626
TestTuple=tuple[TestName, ...]
2727
TestList=list[TestName]
@@ -215,6 +215,33 @@ def get_rerun_match_tests(self) -> FilterTuple | None:
215215
returnNone
216216
returntuple(match_tests)
217217

218+
defwrite_json(self,file)->None:
219+
json.dump(self,file,cls=_EncodeTestResult)
220+
221+
@staticmethod
222+
deffrom_json(worker_json)->'TestResult':
223+
returnjson.loads(worker_json,object_hook=_decode_test_result)
224+
225+
226+
class_EncodeTestResult(json.JSONEncoder):
227+
defdefault(self,o:Any)->dict[str,Any]:
228+
ifisinstance(o,TestResult):
229+
result=dataclasses.asdict(o)
230+
result["__test_result__"]=o.__class__.__name__
231+
returnresult
232+
else:
233+
returnsuper().default(o)
234+
235+
236+
def_decode_test_result(data:dict[str,Any])->TestResult|dict[str,Any]:
237+
if"__test_result__"indata:
238+
data.pop('__test_result__')
239+
ifdata['stats']isnotNone:
240+
data['stats']=TestStats(**data['stats'])
241+
returnTestResult(**data)
242+
else:
243+
returndata
244+
218245

219246
@dataclasses.dataclass(slots=True,frozen=True)
220247
classRunTests:
@@ -234,7 +261,7 @@ class RunTests:
234261
quiet:bool=False
235262
hunt_refleak:HuntRefleak|None=None
236263
test_dir:StrPath|None=None
237-
junit_filename:StrPath|None=None
264+
use_junit:bool=False
238265
memory_limit:str|None=None
239266
gc_threshold:int|None=None
240267
use_resources:list[str]=None
@@ -358,7 +385,7 @@ def setup_support(runtests: RunTests):
358385
support.set_match_tests(runtests.match_tests,runtests.ignore_tests)
359386
support.failfast=runtests.fail_fast
360387
support.verbose=runtests.verbose
361-
ifruntests.junit_filename:
388+
ifruntests.use_junit:
362389
support.junit_xml_list= []
363390
else:
364391
support.junit_xml_list=None
@@ -434,8 +461,8 @@ def run_single_test(test_name: TestName, runtests: RunTests) -> TestResult:
434461
435462
Returns a TestResult.
436463
437-
If runtests.junit_filename is not None, xml_data is a list containing each
438-
generatedtestsuite element.
464+
If runtests.use_junit, xml_data is a list containing each generated
465+
testsuite element.
439466
"""
440467
start_time=time.perf_counter()
441468
result=TestResult(test_name)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp