Skip to content

Commit 8fdac41

Browse files
committed
gh-101634: regrtest reports decoding error as failed test
When running the Python test suite with -jN option, if a worker stdout cannot be decoded from the locale encoding report a failed testn so the exitcode is non-zero. Patch by Victor Stinner.
1 parent 161012f commit 8fdac41

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Lib/test/libregrtest/runtest_mp.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def _runtest(self, test_name: str) -> MultiprocessResult:
277277
encoding = locale.getencoding()
278278
else:
279279
encoding = sys.stdout.encoding
280+
280281
# gh-94026: Write stdout+stderr to a tempfile as workaround for
281282
# non-blocking pipes on Emscripten with NodeJS.
282283
with tempfile.TemporaryFile('w+', encoding=encoding) as stdout_fh:
@@ -298,7 +299,12 @@ def _runtest(self, test_name: str) -> MultiprocessResult:
298299
retcode = self._run_process(test_name, None, stdout_fh)
299300
tmp_files = ()
300301
stdout_fh.seek(0)
301-
stdout = stdout_fh.read().strip()
302+
303+
try:
304+
stdout = stdout_fh.read().strip()
305+
except Exception as exc:
306+
err_msg = f"Cannot read process stdout: {exc}"
307+
return self.mp_result_error(ChildError(test_name), '', err_msg)
302308

303309
if retcode is None:
304310
return self.mp_result_error(Timeout(test_name), stdout)
@@ -481,6 +487,8 @@ def _process_result(self, item: QueueOutput) -> bool:
481487
# Thread got an exception
482488
format_exc = item[1]
483489
print_warning(f"regrtest worker thread failed: {format_exc}")
490+
result = ChildError("<regrtest worker>")
491+
self.regrtest.accumulate_result(result)
484492
return True
485493

486494
self.test_index += 1

Lib/test/test_regrtest.py

+31
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,37 @@ def test_leak_tmp_file(self):
15511551
f"files (1): mytmpfile",
15521552
output)
15531553

1554+
def test_mp_decode_error(self):
1555+
# gh-101634: If a worker stdout cannot be decoded, report a failed test
1556+
# and a non-zero exit code.
1557+
if sys.platform == 'win32':
1558+
encoding = locale.getencoding()
1559+
else:
1560+
encoding = sys.stdout.encoding
1561+
1562+
nonascii = b"byte:\xa0\xa9\xff\n"
1563+
try:
1564+
nonascii.decode(encoding)
1565+
except UnicodeDecodeError:
1566+
pass
1567+
else:
1568+
self.skipTest(f"{encoding} can decode non-ASCII bytes {nonascii!a}")
1569+
1570+
code = textwrap.dedent(fr"""
1571+
import sys
1572+
# bytes which cannot be decoded from UTF-8
1573+
nonascii = {nonascii!a}
1574+
sys.stdout.buffer.write(nonascii)
1575+
sys.stdout.buffer.flush()
1576+
""")
1577+
testname = self.create_test(code=code)
1578+
1579+
output = self.run_tests("--fail-env-changed", "-v", "-j1", testname,
1580+
exitcode=EXITCODE_BAD_TEST)
1581+
self.check_executed_tests(output, [testname],
1582+
failed=[testname],
1583+
randomize=True)
1584+
15541585

15551586
class TestUtils(unittest.TestCase):
15561587
def test_format_duration(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When running the Python test suite with ``-jN`` option, if a worker stdout
2+
cannot be decoded from the locale encoding report a failed testn so the
3+
exitcode is non-zero. Patch by Victor Stinner.

0 commit comments

Comments
 (0)