Skip to content

Commit 89be6f7

Browse files
[3.14] gh-154582: Fix test_invalid_utf8_arg in non-UTF-8 multibyte locales (GH-154584) (GH-154604)
Arbitrary bytes round-trip through surrogateescape only in UTF-8 and single-byte encodings, not in a stateful multibyte encoding such as EUC-JP. (cherry picked from commit b1e530e) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 6065d74 commit 89be6f7

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,26 @@ def run_utf8_mode(arg):
331331
)
332332
test_args = [valid_utf8, invalid_utf8]
333333

334-
for run_cmd in (run_default, run_c_locale, run_utf8_mode):
335-
with self.subTest(run_cmd=run_cmd):
334+
for run_cmd, encoding in (
335+
(run_default, sys.getfilesystemencoding()),
336+
(run_c_locale, None),
337+
(run_utf8_mode, None),
338+
):
339+
with self.subTest(run_cmd=run_cmd.__name__):
340+
# Arbitrary bytes round-trip through surrogateescape only in
341+
# UTF-8 and single-byte encodings, not in a multibyte encoding
342+
# such as EUC-JP.
343+
if encoding is not None:
344+
try:
345+
lossless = len(bytes(range(256)).decode(
346+
encoding, 'surrogateescape')) == 256
347+
except UnicodeError:
348+
lossless = False
349+
else:
350+
lossless = True
351+
if not lossless:
352+
self.skipTest(f'{encoding} cannot losslessly '
353+
f'round-trip arbitrary bytes')
336354
for arg in test_args:
337355
proc = run_cmd(arg)
338356
self.assertEqual(proc.stdout.rstrip(), ascii(arg))

0 commit comments

Comments
 (0)