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