MCPcopy Index your code
hub / github.com/python/cpython / test_worker_decode_error

Method test_worker_decode_error

Lib/test/test_regrtest.py:2066–2111  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2064 output)
2065
2066 def test_worker_decode_error(self):
2067 # gh-109425: Use "backslashreplace" error handler to decode stdout.
2068 if sys.platform == 'win32':
2069 encoding = locale.getencoding()
2070 else:
2071 encoding = sys.stdout.encoding
2072 if encoding is None:
2073 encoding = sys.__stdout__.encoding
2074 if encoding is None:
2075 self.skipTest("cannot get regrtest worker encoding")
2076
2077 nonascii = bytes(ch for ch in range(128, 256))
2078 corrupted_output = b"nonascii:%s\n" % (nonascii,)
2079 # gh-108989: On Windows, assertion errors are written in UTF-16: when
2080 # decoded each letter is follow by a NUL character.
2081 assertion_failed = 'Assertion failed: tstate_is_alive(tstate)\n'
2082 corrupted_output += assertion_failed.encode('utf-16-le')
2083 try:
2084 corrupted_output.decode(encoding)
2085 except UnicodeDecodeError:
2086 pass
2087 else:
2088 self.skipTest(f"{encoding} can decode non-ASCII bytes")
2089
2090 expected_line = corrupted_output.decode(encoding, 'backslashreplace')
2091
2092 code = textwrap.dedent(fr"""
2093 import sys
2094 import unittest
2095
2096 class Tests(unittest.TestCase):
2097 def test_pass(self):
2098 pass
2099
2100 # bytes which cannot be decoded from UTF-8
2101 corrupted_output = {corrupted_output!a}
2102 sys.stdout.buffer.write(corrupted_output)
2103 sys.stdout.buffer.flush()
2104 """)
2105 testname = self.create_test(code=code)
2106
2107 output = self.run_tests("--fail-env-changed", "-v", "-j1", testname)
2108 self.check_executed_tests(output, [testname],
2109 parallel=True,
2110 stats=1)
2111 self.check_line(output, expected_line, regex=False)
2112
2113 def test_doctest(self):
2114 code = textwrap.dedent(r'''

Callers

nothing calls this directly

Calls 8

run_testsMethod · 0.95
skipTestMethod · 0.80
create_testMethod · 0.80
check_executed_testsMethod · 0.80
check_lineMethod · 0.80
encodeMethod · 0.45
decodeMethod · 0.45
dedentMethod · 0.45

Tested by

no test coverage detected