Skip to content

Commit f8c3ac7

Browse files
committed
pythongh-109413: regrtest: add WorkerRunTests class
1 parent a65a3d4 commit f8c3ac7

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

Lib/test/libregrtest/main.py

Lines changed: 0 additions & 1 deletion
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ 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 create_worker_runtests(self, **override):
9998
state = dataclasses.asdict(self)
10099
state.update(override)
101-
return RunTests(**state)
100+
return WorkerRunTests(**state)
102101

103102
def get_match_tests(self, test_name) -> FilterTuple | None:
104103
if self.match_tests_dict is not None:
@@ -120,13 +119,6 @@ def iter_tests(self):
120119
else:
121120
yield from self.tests
122121

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-
130122
def json_file_use_stdout(self) -> bool:
131123
# Use STDOUT in two cases:
132124
#
@@ -141,9 +133,21 @@ def json_file_use_stdout(self) -> bool:
141133
)
142134

143135

136+
@dataclasses.dataclass(slots=True, frozen=True)
137+
class WorkerRunTests(RunTests):
138+
json_file: JsonFile | None
139+
140+
def as_json(self) -> StrJSON:
141+
return json.dumps(self, cls=_EncodeRunTests)
142+
143+
@staticmethod
144+
def from_json(worker_json: StrJSON) -> 'RunTests':
145+
return json.loads(worker_json, object_hook=_decode_runtests)
146+
147+
144148
class _EncodeRunTests(json.JSONEncoder):
145149
def default(self, o: Any) -> dict[str, Any]:
146-
if isinstance(o, RunTests):
150+
if isinstance(o, WorkerRunTests):
147151
result = dataclasses.asdict(o)
148152
result["__runtests__"] = True
149153
return result
@@ -158,6 +162,6 @@ def _decode_runtests(data: dict[str, Any]) -> RunTests | dict[str, Any]:
158162
data['hunt_refleak'] = HuntRefleak(**data['hunt_refleak'])
159163
if data['json_file']:
160164
data['json_file'] = JsonFile(**data['json_file'])
161-
return RunTests(**data)
165+
return WorkerRunTests(**data)
162166
else:
163167
return data

Lib/test/libregrtest/worker.py

Lines changed: 3 additions & 3 deletions
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)