https://github.com/python/cpython/commit/89be6f7778edda0399950aa368658ecfb7315028
commit: 89be6f7778edda0399950aa368658ecfb7315028
branch: 3.14
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-26T19:19:06Z
summary:

[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 b1e530ebf3b68e884ab37882d690d95e5d82aef5)

Co-authored-by: Claude Opus 4.8 <[email protected]>

files:
M Lib/test/test_cmd_line.py

diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 2f4fee0443cff38..adc665135576d46 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -331,8 +331,26 @@ def run_utf8_mode(arg):
         )
         test_args = [valid_utf8, invalid_utf8]
 
-        for run_cmd in (run_default, run_c_locale, run_utf8_mode):
-            with self.subTest(run_cmd=run_cmd):
+        for run_cmd, encoding in (
+            (run_default, sys.getfilesystemencoding()),
+            (run_c_locale, None),
+            (run_utf8_mode, None),
+        ):
+            with self.subTest(run_cmd=run_cmd.__name__):
+                # Arbitrary bytes round-trip through surrogateescape only in
+                # UTF-8 and single-byte encodings, not in a multibyte encoding
+                # such as EUC-JP.
+                if encoding is not None:
+                    try:
+                        lossless = len(bytes(range(256)).decode(
+                            encoding, 'surrogateescape')) == 256
+                    except UnicodeError:
+                        lossless = False
+                else:
+                    lossless = True
+                if not lossless:
+                    self.skipTest(f'{encoding} cannot losslessly '
+                                  f'round-trip arbitrary bytes')
                 for arg in test_args:
                     proc = run_cmd(arg)
                     self.assertEqual(proc.stdout.rstrip(), ascii(arg))

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to