Skip to content

Commit 3871923

Browse files
[3.13] gh-154582: Fix test_invalid_utf8_arg in non-UTF-8 multibyte locales (GH-154584) (GH-154605)
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 233022d commit 3871923

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
@@ -326,8 +326,26 @@ def run_utf8_mode(arg):
326326
)
327327
test_args = [valid_utf8, invalid_utf8]
328328

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

0 commit comments

Comments
 (0)