Skip to content

Commit f848b2b

Browse files
committed
pythongh-101634: regrtest reports decoding error as failed test (python#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. (cherry picked from commit 2ac3eec)
1 parent 5d764de commit f848b2b

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
@@ -1553,6 +1554,41 @@ def test_leak_tmp_file(self):
15531554
f"files (1): mytmpfile",
15541555
output)
15551556

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

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