Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c44abb1

Browse files
author
Victor Stinner
committed
Workaround issue #8611 in test_undecodable_code() of test_sys
Write test.support.workaroundIssue8611() function so it will be easier to remove this workaround from all tests.
1 parent 1b6372a commit c44abb1

3 files changed

Lines changed: 36 additions & 31 deletions

File tree

Lib/test/support.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,3 +1277,11 @@ def strip_python_stderr(stderr):
12771277
"""
12781278
stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
12791279
return stderr
1280+
1281+
def workaroundIssue8611():
1282+
try:
1283+
sys.executable.encode('ascii')
1284+
except UnicodeEncodeError:
1285+
raise unittest.SkipTest(
1286+
"Issue #8611: Python doesn't support ascii locale encoding "
1287+
"with an non-ascii path")

Lib/test/test_os.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,15 +1175,12 @@ def check(encoding, bytesfn, unicodefn):
11751175
self.assertEqual(decoded, repr(unicodefn))
11761176

11771177
check('utf-8', b'\xc3\xa9\x80', '\xe9\udc80')
1178-
try:
1179-
sys.executable.encode("ascii")
1180-
except UnicodeEncodeError:
1181-
# Python doesn't start with ASCII locale if its path is not ASCII,
1182-
# see issue #8611
1183-
pass
1184-
else:
1185-
check('ascii', b'abc\xff', 'abc\udcff')
1186-
check('iso-8859-15', b'\xef\xa4', '\xef\u20ac')
1178+
1179+
# Raise SkipTest() if sys.executable is not encodable to ascii
1180+
support.workaroundIssue8611()
1181+
1182+
check('ascii', b'abc\xff', 'abc\udcff')
1183+
check('iso-8859-15', b'\xef\xa4', '\xef\u20ac')
11871184

11881185

11891186
def test_main():

Lib/test/test_sys.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,16 @@ def __hash__(self):
496496
self.assertRaises(TypeError, sys.intern, S("abc"))
497497

498498
def test_undecodable_code(self):
499-
non_decodable = b"\xff"
499+
# Raise SkipTest() if sys.executable is not encodable to ascii
500+
test.support.workaroundIssue8611()
501+
502+
undecodable = b"\xff"
500503
env = os.environ.copy()
501504
env['LANG'] = 'C'
502-
code = b'import locale; '
503-
code += b'print(ascii("' + non_decodable + b'"), locale.getpreferredencoding())'
505+
code = (
506+
b'import locale; '
507+
b'print(ascii("' + undecodable + b'"), '
508+
b'locale.getpreferredencoding())')
504509
p = subprocess.Popen(
505510
[sys.executable, "-c", code],
506511
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
@@ -587,28 +592,23 @@ def get_fsencoding(env):
587592
env=env)
588593
return output.rstrip().decode('ascii')
589594

595+
# Raise SkipTest() if sys.executable is not encodable to ascii
596+
test.support.workaroundIssue8611()
597+
598+
# Even in C locale
599+
env = os.environ.copy()
600+
env['LANG'] = 'C'
590601
try:
591-
sys.executable.encode('ascii')
592-
except UnicodeEncodeError:
593-
# Python doesn't start with ASCII locale if its path is not ASCII,
594-
# see issue #8611
602+
del env['PYTHONFSENCODING']
603+
except KeyError:
595604
pass
596-
else:
597-
# Even in C locale
598-
env = os.environ.copy()
599-
env['LANG'] = 'C'
600-
try:
601-
del env['PYTHONFSENCODING']
602-
except KeyError:
603-
pass
604-
self.check_fsencoding(get_fsencoding(env), 'ascii')
605-
606-
# Filesystem encoding is hardcoded on Windows and Mac OS X
607-
for encoding in ('ascii', 'cp850', 'iso8859-1', 'utf-8'):
608-
env = os.environ.copy()
609-
env['PYTHONFSENCODING'] = encoding
610-
self.check_fsencoding(get_fsencoding(env), encoding)
605+
self.check_fsencoding(get_fsencoding(env), 'ascii')
611606

607+
# Filesystem encoding is hardcoded on Windows and Mac OS X
608+
for encoding in ('ascii', 'cp850', 'iso8859-1', 'utf-8'):
609+
env = os.environ.copy()
610+
env['PYTHONFSENCODING'] = encoding
611+
self.check_fsencoding(get_fsencoding(env), encoding)
612612

613613
def test_setfilesystemencoding(self):
614614
old = sys.getfilesystemencoding()

0 commit comments

Comments
 (0)