Skip to content

Commit eb3c0dc

Browse files
[3.11] gh-109413: regrtest: add WorkerRunTests class (GH-112588) (#112594)
gh-109413: regrtest: add WorkerRunTests class (GH-112588) (cherry picked from commit f8ff80f) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent d838c7a commit eb3c0dc

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
@@ -418,7 +418,6 @@ def create_run_tests(self, tests: TestTuple):
418418
python_cmd=self.python_cmd,
419419
randomize=self.randomize,
420420
random_seed=self.random_seed,
421-
json_file=None,
422421
)
423422

424423
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
@@ -250,9 +250,7 @@ def create_json_file(self, stack: contextlib.ExitStack) -> tuple[JsonFile, TextI
250250
json_file = JsonFile(json_fd, JsonFileType.UNIX_FD)
251251
return (json_file, json_tmpfile)
252252

253-
def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> RunTests:
254-
"""Create the worker RunTests."""
255-
253+
def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> WorkerRunTests:
256254
tests = (test_name,)
257255
if self.runtests.rerun:
258256
match_tests = self.runtests.get_match_tests(test_name)
@@ -265,12 +263,12 @@ def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> Ru
265263
if self.runtests.output_on_failure:
266264
kwargs['verbose'] = True
267265
kwargs['output_on_failure'] = False
268-
return self.runtests.copy(
266+
return self.runtests.create_worker_runtests(
269267
tests=tests,
270268
json_file=json_file,
271269
**kwargs)
272270

273-
def run_tmp_files(self, worker_runtests: RunTests,
271+
def run_tmp_files(self, worker_runtests: WorkerRunTests,
274272
stdout_fd: int) -> tuple[int | None, list[StrPath]]:
275273
# gh-93353: Check for leaked temporary files in the parent process,
276274
# 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
@@ -91,13 +91,17 @@ class RunTests:
9191
python_cmd: tuple[str, ...] | None
9292
randomize: bool
9393
random_seed: int | str
94-
json_file: JsonFile | None
9594

96-
def copy(self, **override):
95+
def copy(self, **override) -> 'RunTests':
9796
state = dataclasses.asdict(self)
9897
state.update(override)
9998
return RunTests(**state)
10099

100+
def create_worker_runtests(self, **override):
101+
state = dataclasses.asdict(self)
102+
state.update(override)
103+
return WorkerRunTests(**state)
104+
101105
def get_match_tests(self, test_name) -> FilterTuple | None:
102106
if self.match_tests_dict is not None:
103107
return self.match_tests_dict.get(test_name, None)
@@ -118,13 +122,6 @@ def iter_tests(self):
118122
else:
119123
yield from self.tests
120124

121-
def as_json(self) -> StrJSON:
122-
return json.dumps(self, cls=_EncodeRunTests)
123-
124-
@staticmethod
125-
def from_json(worker_json: StrJSON) -> 'RunTests':
126-
return json.loads(worker_json, object_hook=_decode_runtests)
127-
128125
def json_file_use_stdout(self) -> bool:
129126
# Use STDOUT in two cases:
130127
#
@@ -139,9 +136,21 @@ def json_file_use_stdout(self) -> bool:
139136
)
140137

141138

139+
@dataclasses.dataclass(slots=True, frozen=True)
140+
class WorkerRunTests(RunTests):
141+
json_file: JsonFile
142+
143+
def as_json(self) -> StrJSON:
144+
return json.dumps(self, cls=_EncodeRunTests)
145+
146+
@staticmethod
147+
def from_json(worker_json: StrJSON) -> 'WorkerRunTests':
148+
return json.loads(worker_json, object_hook=_decode_runtests)
149+
150+
142151
class _EncodeRunTests(json.JSONEncoder):
143152
def default(self, o: Any) -> dict[str, Any]:
144-
if isinstance(o, RunTests):
153+
if isinstance(o, WorkerRunTests):
145154
result = dataclasses.asdict(o)
146155
result["__runtests__"] = True
147156
return result
@@ -156,6 +165,6 @@ def _decode_runtests(data: dict[str, Any]) -> RunTests | dict[str, Any]:
156165
data['hunt_refleak'] = HuntRefleak(**data['hunt_refleak'])
157166
if data['json_file']:
158167
data['json_file'] = JsonFile(**data['json_file'])
159-
return RunTests(**data)
168+
return WorkerRunTests(**data)
160169
else:
161170
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
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()
@@ -71,7 +71,7 @@ def create_worker_process(runtests: RunTests, output_fd: int,
7171

7272

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

0 commit comments

Comments
 (0)