Skip to content

Commit 2ac3eec

Browse files
authored
gh-101634: regrtest reports decoding error as failed test (#106169)
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.
1 parent 3f8483c commit 2ac3eec

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Lib/test/libregrtest/runtest_mp.py

+11-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,14 @@ 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+
# gh-101634: Catch UnicodeDecodeError if stdout cannot be
307+
# decoded from encoding
308+
err_msg = f"Cannot read process stdout: {exc}"
309+
return self.mp_result_error(ChildError(test_name), '', err_msg)
302310

303311
if retcode is None:
304312
return self.mp_result_error(Timeout(test_name), stdout)
@@ -481,6 +489,8 @@ def _process_result(self, item: QueueOutput) -> bool:
481489
# Thread got an exception
482490
format_exc = item[1]
483491
print_warning(f"regrtest worker thread failed: {format_exc}")
492+
result = ChildError("<regrtest worker>")
493+
self.regrtest.accumulate_result(result)
484494
return True
485495

486496
self.test_index += 1

Lib/test/test_regrtest.py

+36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import contextlib
88
import glob
99
import io
10+
import locale
1011
import os.path
1112
import platform
1213
import re
@@ -1551,6 +1552,41 @@ def test_leak_tmp_file(self):
15511552
f"files (1): mytmpfile",
15521553
output)
15531554

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

15551591
class TestUtils(unittest.TestCase):
15561592
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)