MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_invalid_utf8_arg

Method test_invalid_utf8_arg

Lib/test/test_cmd_line.py:266–309  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

264 @unittest.skipIf(sys.platform == 'win32',
265 'Windows has a native unicode API')
266 def test_invalid_utf8_arg(self):
267 # bpo-35883: Py_DecodeLocale() must escape b'\xfd\xbf\xbf\xbb\xba\xba'
268 # byte sequence with surrogateescape rather than decoding it as the
269 # U+7fffbeba character which is outside the [U+0000; U+10ffff] range of
270 # Python Unicode characters.
271 #
272 # Test with default config, in the C locale, in the Python UTF-8 Mode.
273 code = 'import sys, os; s=os.fsencode(sys.argv[1]); print(ascii(s))'
274
275 # TODO: RUSTPYTHON
276 def run_default(arg):
277 cmd = [sys.executable, '-c', code, arg]
278 return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
279
280 # TODO: RUSTPYTHON
281 def run_c_locale(arg):
282 cmd = [sys.executable, '-c', code, arg]
283 env = dict(os.environ)
284 env['LC_ALL'] = 'C'
285 return subprocess.run(cmd, stdout=subprocess.PIPE,
286 text=True, env=env)
287
288 # TODO: RUSTPYTHON
289 def run_utf8_mode(arg):
290 cmd = [sys.executable, '-X', 'utf8', '-c', code, arg]
291 return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
292
293 valid_utf8 = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8')
294 # invalid UTF-8 byte sequences with a valid UTF-8 sequence
295 # in the middle.
296 invalid_utf8 = (
297 b'\xff' # invalid byte
298 b'\xc3\xff' # invalid byte sequence
299 b'\xc3\xa9' # valid utf-8: U+00E9 character
300 b'\xed\xa0\x80' # lone surrogate character (invalid)
301 b'\xfd\xbf\xbf\xbb\xba\xba' # character outside [U+0000; U+10ffff]
302 )
303 test_args = [valid_utf8, invalid_utf8]
304
305 for run_cmd in (run_default, run_c_locale, run_utf8_mode):
306 with self.subTest(run_cmd=run_cmd):
307 for arg in test_args:
308 proc = run_cmd(arg)
309 self.assertEqual(proc.stdout.rstrip(), ascii(arg))
310
311 @unittest.skipUnless((sys.platform == 'darwin' or
312 support.is_android), 'test specific to Mac OS X and Android')

Callers

nothing calls this directly

Calls 5

asciiFunction · 0.85
subTestMethod · 0.80
encodeMethod · 0.45
assertEqualMethod · 0.45
rstripMethod · 0.45

Tested by

no test coverage detected