Skip to content

Commit

Permalink
pythongh-110367: Enhance regrtest -jN --verbose3
Browse files Browse the repository at this point in the history
When using worker processes (-jN) with --verbose3 option, regrtest
can now display the worker output even if a worker process does
crash.  Previously, sys.stdout and sys.stderr were replaced and so
the worker output was lost on a crash.
  • Loading branch information
vstinner committed Oct 4, 2023
1 parent 254e30c commit 9112bc3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Lib/test/libregrtest/run_workers.py
Expand Up @@ -262,6 +262,9 @@ def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> Ru
kwargs = {}
if match_tests:
kwargs['match_tests'] = match_tests
if self.runtests.output_on_failure:
kwargs['verbose'] = True
kwargs['output_on_failure'] = False
return self.runtests.copy(
tests=tests,
json_file=json_file,
Expand Down Expand Up @@ -562,8 +565,16 @@ def _process_result(self, item: QueueOutput) -> TestResult:
self.results.accumulate_result(result, self.runtests)
self.display_result(mp_result)

if mp_result.worker_stdout:
print(mp_result.worker_stdout, flush=True)
# Display worker stdout
if not self.runtests.output_on_failure:
show_stdout = True
else:
# --verbose3 ignores stdout on success
show_stdout = (result.state != State.PASSED)
if show_stdout:
stdout = mp_result.worker_stdout
if stdout:
print(stdout, flush=True)

return result

Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_regrtest.py
Expand Up @@ -2036,6 +2036,26 @@ def test_add_python_opts(self):
with self.subTest(opt=opt):
self.check_add_python_opts(opt)

def test_worker_output_on_failure(self):
code = textwrap.dedent(r"""
import ctypes
import unittest
class CrashTests(unittest.TestCase):
def test_crash(self):
print("just before crash!", flush=True)
# Reading at NULL is likely to crash
ctypes.string_at(0)
""")
testname = self.create_test(code=code)

output = self.run_tests("-j1", testname, exitcode=EXITCODE_BAD_TEST)
self.check_executed_tests(output, testname,
failed=[testname],
stats=0, parallel=True)
self.check_line(output, "just before crash!", full=True, regex=False)


class TestUtils(unittest.TestCase):
def test_format_duration(self):
Expand Down
@@ -0,0 +1,4 @@
regrtest: When using worker processes (-jN) with --verbose3 option, regrtest
can now display the worker output even if a worker process does crash.
Previously, sys.stdout and sys.stderr were replaced and so the worker output
was lost on a crash. Patch by Victor Stinner.

0 comments on commit 9112bc3

Please sign in to comment.