Skip to content

Commit f8ff80f

Browse files
authored
pythongh-109413: regrtest: add WorkerRunTests class (python#112588)
1 parent 0584443 commit f8ff80f

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

Lib/test/libregrtest/main.py

-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ def create_run_tests(self, tests: TestTuple):
423423
python_cmd=self.python_cmd,
424424
randomize=self.randomize,
425425
random_seed=self.random_seed,
426-
json_file=None,
427426
)
428427

429428
def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int:

Lib/test/libregrtest/run_workers.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .logger import Logger
1919
from .result import TestResult, State
2020
from .results import TestResults
21-
from .runtests import RunTests, JsonFile, JsonFileType
21+
from .runtests import RunTests, WorkerRunTests, JsonFile, JsonFileType
2222
from .single import PROGRESS_MIN_TIME
2323
from .utils import (
2424
StrPath, TestName,
@@ -162,7 +162,7 @@ def stop(self) -> None:
162162
self._stopped = True
163163
self._kill()
164164

165-
def _run_process(self, runtests: RunTests, output_fd: int,
165+
def _run_process(self, runtests: WorkerRunTests, output_fd: int,
166166
tmp_dir: StrPath | None = None) -> int | None:
167167
popen = create_worker_process(runtests, output_fd, tmp_dir)
168168
self._popen = popen
@@ -252,9 +252,7 @@ def create_json_file(self, stack: contextlib.ExitStack) -> tuple[JsonFile, TextI
252252
json_file = JsonFile(json_fd, JsonFileType.UNIX_FD)
253253
return (json_file, json_tmpfile)
254254

255-
def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> RunTests:
256-
"""Create the worker RunTests."""
257-
255+
def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> WorkerRunTests:
258256
tests = (test_name,)
259257
if self.runtests.rerun:
260258
match_tests = self.runtests.get_match_tests(test_name)
@@ -267,12 +265,12 @@ def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> Ru
267265
if self.runtests.output_on_failure:
268266
kwargs['verbose'] = True
269267
kwargs['output_on_failure'] = False
270-
return self.runtests.copy(
268+
return self.runtests.create_worker_runtests(
271269
tests=tests,
272270
json_file=json_file,
273271
**kwargs)
274272

275-
def run_tmp_files(self, worker_runtests: RunTests,
273+
def run_tmp_files(self, worker_runtests: WorkerRunTests,
276274
stdout_fd: int) -> tuple[int | None, list[StrPath]]:
277275
# gh-93353: Check for leaked temporary files in the parent process,
278276
# since the deletion of temporary files can happen late during

Lib/test/libregrtest/runtests.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ class RunTests:
9393
python_cmd: tuple[str, ...] | None
9494
randomize: bool
9595
random_seed: int | str
96-
json_file: JsonFile | None
9796

98-
def copy(self, **override):
97+
def copy(self, **override) -> 'RunTests':
9998
state = dataclasses.asdict(self)
10099
state.update(override)
101100
return RunTests(**state)
102101

102+
def create_worker_runtests(self, **override):
103+
state = dataclasses.asdict(self)
104+
state.update(override)
105+
return WorkerRunTests(**state)
106+
103107
def get_match_tests(self, test_name) -> FilterTuple | None:
104108
if self.match_tests_dict is not None:
105109
return self.match_tests_dict.get(test_name, None)
@@ -120,13 +124,6 @@ def iter_tests(self):
120124
else:
121125
yield from self.tests
122126

123-
def as_json(self) -> StrJSON:
124-
return json.dumps(self, cls=_EncodeRunTests)
125-
126-
@staticmethod
127-
def from_json(worker_json: StrJSON) -> 'RunTests':
128-
return json.loads(worker_json, object_hook=_decode_runtests)
129-
130127
def json_file_use_stdout(self) -> bool:
131128
# Use STDOUT in two cases:
132129
#
@@ -141,9 +138,21 @@ def json_file_use_stdout(self) -> bool:
141138
)
142139

143140

141+
@dataclasses.dataclass(slots=True, frozen=True)
142+
class WorkerRunTests(RunTests):
143+
json_file: JsonFile
144+
145+
def as_json(self) -> StrJSON:
146+
return json.dumps(self, cls=_EncodeRunTests)
147+
148+
@staticmethod
149+
def from_json(worker_json: StrJSON) -> 'WorkerRunTests':
150+
return json.loads(worker_json, object_hook=_decode_runtests)
151+
152+
144153
class _EncodeRunTests(json.JSONEncoder):
145154
def default(self, o: Any) -> dict[str, Any]:
146-
if isinstance(o, RunTests):
155+
if isinstance(o, WorkerRunTests):
147156
result = dataclasses.asdict(o)
148157
result["__runtests__"] = True
149158
return result
@@ -158,6 +167,6 @@ def _decode_runtests(data: dict[str, Any]) -> RunTests | dict[str, Any]:
158167
data['hunt_refleak'] = HuntRefleak(**data['hunt_refleak'])
159168
if data['json_file']:
160169
data['json_file'] = JsonFile(**data['json_file'])
161-
return RunTests(**data)
170+
return WorkerRunTests(**data)
162171
else:
163172
return data

Lib/test/libregrtest/worker.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from test.support import os_helper, Py_DEBUG
88

99
from .setup import setup_process, setup_test_dir
10-
from .runtests import RunTests, JsonFile, JsonFileType
10+
from .runtests import WorkerRunTests, JsonFile, JsonFileType
1111
from .single import run_single_test
1212
from .utils import (
1313
StrPath, StrJSON, TestFilter,
@@ -17,7 +17,7 @@
1717
USE_PROCESS_GROUP = (hasattr(os, "setsid") and hasattr(os, "killpg"))
1818

1919

20-
def create_worker_process(runtests: RunTests, output_fd: int,
20+
def create_worker_process(runtests: WorkerRunTests, output_fd: int,
2121
tmp_dir: StrPath | None = None) -> subprocess.Popen:
2222
python_cmd = runtests.python_cmd
2323
worker_json = runtests.as_json()
@@ -73,7 +73,7 @@ def create_worker_process(runtests: RunTests, output_fd: int,
7373

7474

7575
def worker_process(worker_json: StrJSON) -> NoReturn:
76-
runtests = RunTests.from_json(worker_json)
76+
runtests = WorkerRunTests.from_json(worker_json)
7777
test_name = runtests.tests[0]
7878
match_tests: TestFilter = runtests.match_tests
7979
json_file: JsonFile = runtests.json_file

0 commit comments

Comments
 (0)