Debian Codegraph @

lib-python/3/test/libregrtest/worker.py

raw symbols SCIP
1 import subprocess
2 import sys
3 import os
4 from typing import Any, NoReturn
5
6 from test.support import os_helper
7
8 from .setup import setup_process, setup_test_dir
9 from .runtests import WorkerRunTests, JsonFile, JsonFileType
10 from .single import run_single_test
11 from .utils import (
12 StrPath, StrJSON, TestFilter,
13 get_temp_dir, get_work_dir, exit_timeout)
14
15
16 USE_PROCESS_GROUP = (hasattr(os, "setsid") and hasattr(os, "killpg"))
17
18
19 def create_worker_process(runtests: WorkerRunTests, output_fd: int,
20 tmp_dir: StrPath | None = None) -> subprocess.Popen:
21 worker_json = runtests.as_json()
22
23 cmd = runtests.create_python_cmd()
24 cmd.extend(['-m', 'test.libregrtest.worker', worker_json])
25
26 env = dict(os.environ)
27 if tmp_dir is not None:
28 env['TMPDIR'] = tmp_dir
29 env['TEMP'] = tmp_dir
30 env['TMP'] = tmp_dir
31
32 # Running the child from the same working directory as regrtest's original
33 # invocation ensures that TEMPDIR for the child is the same when
34 # sysconfig.is_python_build() is true. See issue 15300.
35 #
36 # Emscripten and WASI Python must start in the Python source code directory
37 # to get 'python.js' or 'python.wasm' file. Then worker_process() changes
38 # to a temporary directory created to run tests.
39 work_dir = os_helper.SAVEDCWD
40
41 kwargs: dict[str, Any] = dict(
42 env=env,
43 stdout=output_fd,
44 # bpo-45410: Write stderr into stdout to keep messages order
45 stderr=output_fd,
46 text=True,
47 close_fds=True,
48 cwd=work_dir,
49 )
50 if USE_PROCESS_GROUP:
51 kwargs['start_new_session'] = True
52
53 # Pass json_file to the worker process
54 json_file = runtests.json_file
55 json_file.configure_subprocess(kwargs)
56
57 with json_file.inherit_subprocess():
58 return subprocess.Popen(cmd, **kwargs)
59
60
61 def worker_process(worker_json: StrJSON) -> NoReturn:
62 runtests = WorkerRunTests.from_json(worker_json)
63 test_name = runtests.tests[0]
64 match_tests: TestFilter = runtests.match_tests
65 json_file: JsonFile = runtests.json_file
66
67 setup_test_dir(runtests.test_dir)
68 setup_process()
69
70 if runtests.rerun:
71 if match_tests:
72 matching = "matching: " + ", ".join(pattern for pattern, result in match_tests if result)
73 print(f"Re-running {test_name} in verbose mode ({matching})", flush=True)
74 else:
75 print(f"Re-running {test_name} in verbose mode", flush=True)
76
77 result = run_single_test(test_name, runtests)
78
79 if json_file.file_type == JsonFileType.STDOUT:
80 print()
81 result.write_json_into(sys.stdout)
82 else:
83 with json_file.open('w', encoding='utf-8') as json_fp:
84 result.write_json_into(json_fp)
85
86 sys.exit(0)
87
88
89 def main():
90 if len(sys.argv) != 2:
91 print("usage: python -m test.libregrtest.worker JSON")
92 sys.exit(1)
93 worker_json = sys.argv[1]
94
95 tmp_dir = get_temp_dir()
96 work_dir = get_work_dir(tmp_dir, worker=True)
97
98 with exit_timeout():
99 with os_helper.temp_cwd(work_dir, quiet=True):
100 worker_process(worker_json)
101
102
103 if __name__ == "__main__":
104 main()