Skip to content

Commit ecfca20

Browse files
hugovkvstinner
andauthored
[3.12] gh-119727: Add --single-process option to regrtest (GH-119728) (#130359)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 60410f3 commit ecfca20

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

Lib/test/libregrtest/cmdline.py

+11
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def __init__(self, **kwargs) -> None:
174174
self.tempdir = None
175175
self._add_python_opts = True
176176
self.xmlpath = None
177+
self.single_process = False
177178

178179
super().__init__(**kwargs)
179180

@@ -307,6 +308,12 @@ def _create_parser():
307308
group.add_argument('-j', '--multiprocess', metavar='PROCESSES',
308309
dest='use_mp', type=int,
309310
help='run PROCESSES processes at once')
311+
group.add_argument('--single-process', action='store_true',
312+
dest='single_process',
313+
help='always run all tests sequentially in '
314+
'a single process, ignore -jN option, '
315+
'and failed tests are also rerun sequentially '
316+
'in the same process')
310317
group.add_argument('-T', '--coverage', action='store_true',
311318
dest='trace',
312319
help='turn on code coverage tracing using the trace '
@@ -436,6 +443,10 @@ def _parse_args(args, **kwargs):
436443
else:
437444
ns._add_python_opts = False
438445

446+
# --singleprocess overrides -jN option
447+
if ns.single_process:
448+
ns.use_mp = None
449+
439450
# When both --slow-ci and --fast-ci options are present,
440451
# --slow-ci has the priority
441452
if ns.slow_ci:

Lib/test/libregrtest/main.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
8787
self.cmdline_args: TestList = ns.args
8888

8989
# Workers
90-
if ns.use_mp is None:
91-
num_workers = 0 # run sequentially
90+
self.single_process: bool = ns.single_process
91+
if self.single_process or ns.use_mp is None:
92+
num_workers = 0 # run sequentially in a single process
9293
elif ns.use_mp <= 0:
93-
num_workers = -1 # use the number of CPUs
94+
num_workers = -1 # run in parallel, use the number of CPUs
9495
else:
95-
num_workers = ns.use_mp
96+
num_workers = ns.use_mp # run in parallel
9697
self.num_workers: int = num_workers
9798
self.worker_json: StrJSON | None = ns.worker_json
9899

@@ -234,7 +235,7 @@ def list_tests(tests: TestTuple):
234235

235236
def _rerun_failed_tests(self, runtests: RunTests):
236237
# Configure the runner to re-run tests
237-
if self.num_workers == 0:
238+
if self.num_workers == 0 and not self.single_process:
238239
# Always run tests in fresh processes to have more deterministic
239240
# initial state. Don't re-run tests in parallel but limit to a
240241
# single worker process to have side effects (on the system load
@@ -244,7 +245,6 @@ def _rerun_failed_tests(self, runtests: RunTests):
244245
tests, match_tests_dict = self.results.prepare_rerun()
245246

246247
# Re-run failed tests
247-
self.log(f"Re-running {len(tests)} failed tests in verbose mode in subprocesses")
248248
runtests = runtests.copy(
249249
tests=tests,
250250
rerun=True,
@@ -254,7 +254,15 @@ def _rerun_failed_tests(self, runtests: RunTests):
254254
match_tests_dict=match_tests_dict,
255255
output_on_failure=False)
256256
self.logger.set_tests(runtests)
257-
self._run_tests_mp(runtests, self.num_workers)
257+
258+
msg = f"Re-running {len(tests)} failed tests in verbose mode"
259+
if not self.single_process:
260+
msg = f"{msg} in subprocesses"
261+
self.log(msg)
262+
self._run_tests_mp(runtests, self.num_workers)
263+
else:
264+
self.log(msg)
265+
self.run_tests_sequentially(runtests)
258266
return runtests
259267

260268
def rerun_failed_tests(self, runtests: RunTests):
@@ -367,7 +375,7 @@ def run_tests_sequentially(self, runtests):
367375
tests = count(jobs, 'test')
368376
else:
369377
tests = 'tests'
370-
msg = f"Run {tests} sequentially"
378+
msg = f"Run {tests} sequentially in a single process"
371379
if runtests.timeout:
372380
msg += " (timeout: %s)" % format_duration(runtests.timeout)
373381
self.log(msg)
@@ -589,7 +597,7 @@ def _add_cross_compile_opts(self, regrtest_opts):
589597
keep_environ = True
590598

591599
if cross_compile and hostrunner:
592-
if self.num_workers == 0:
600+
if self.num_workers == 0 and not self.single_process:
593601
# For now use only two cores for cross-compiled builds;
594602
# hostrunner can be expensive.
595603
regrtest_opts.extend(['-j', '2'])

Lib/test/test_regrtest.py

+13
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,19 @@ def test_verbose3_huntrleaks(self):
466466
self.assertEqual(regrtest.hunt_refleak.runs, 10)
467467
self.assertFalse(regrtest.output_on_failure)
468468

469+
def test_single_process(self):
470+
args = ['-j2', '--single-process']
471+
with support.captured_stderr():
472+
regrtest = self.create_regrtest(args)
473+
self.assertEqual(regrtest.num_workers, 0)
474+
self.assertTrue(regrtest.single_process)
475+
476+
args = ['--fast-ci', '--single-process']
477+
with support.captured_stderr():
478+
regrtest = self.create_regrtest(args)
479+
self.assertEqual(regrtest.num_workers, 0)
480+
self.assertTrue(regrtest.single_process)
481+
469482

470483
@dataclasses.dataclass(slots=True)
471484
class Rerun:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``--single-process`` command line option to Python test runner (regrtest).
2+
Patch by Victor Stinner.

0 commit comments

Comments
 (0)