Check if data is decoded by os.fsdecode() (filesystem encoding with surrogateescape error handler, PEP 383), not by UTF-8 or the filesystem encoding in strict mode. Use TESTFN_UNDECODABLE in test_cmd_line_script.test_non_ascii() on UNIX.
This commit is contained in:
parent
ef3971d3e5
commit
ff3d515952
|
@ -692,17 +692,29 @@ elif sys.platform != 'darwin':
|
||||||
|
|
||||||
# TESTFN_UNDECODABLE is a filename (bytes type) that should *not* be able to be
|
# TESTFN_UNDECODABLE is a filename (bytes type) that should *not* be able to be
|
||||||
# decoded from the filesystem encoding (in strict mode). It can be None if we
|
# decoded from the filesystem encoding (in strict mode). It can be None if we
|
||||||
# cannot generate such filename.
|
# cannot generate such filename (ex: the latin1 encoding can decode any byte
|
||||||
|
# sequence). On UNIX, TESTFN_UNDECODABLE can be decoded by os.fsdecode() thanks
|
||||||
|
# to the surrogateescape error handler (PEP 383), but not from the filesystem
|
||||||
|
# encoding in strict mode.
|
||||||
TESTFN_UNDECODABLE = None
|
TESTFN_UNDECODABLE = None
|
||||||
# b'\xff' is not decodable by os.fsdecode() with code page 932. Windows
|
for name in (
|
||||||
# accepts it to create a file or a directory, or don't accept to enter to
|
# b'\xff' is not decodable by os.fsdecode() with code page 932. Windows
|
||||||
# such directory (when the bytes name is used). So test b'\xe7' first: it is
|
# accepts it to create a file or a directory, or don't accept to enter to
|
||||||
# not decodable from cp932.
|
# such directory (when the bytes name is used). So test b'\xe7' first: it is
|
||||||
for name in (b'\xe7w\xf0', b'abc\xff'):
|
# not decodable from cp932.
|
||||||
|
b'\xe7w\xf0',
|
||||||
|
# undecodable from ASCII, UTF-8
|
||||||
|
b'\xff',
|
||||||
|
# undecodable from iso8859-3, iso8859-6, iso8859-7, cp424, iso8859-8, cp856
|
||||||
|
# and cp857
|
||||||
|
b'\xae\xd5'
|
||||||
|
# undecodable from UTF-8 (UNIX and Mac OS X)
|
||||||
|
b'\xed\xb2\x80', b'\xed\xb4\x80',
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
os.fsdecode(name)
|
name.decode(TESTFN_ENCODING)
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
TESTFN_UNDECODABLE = name
|
TESTFN_UNDECODABLE = os.fsencode(TESTFN) + name
|
||||||
break
|
break
|
||||||
|
|
||||||
if FS_NONASCII:
|
if FS_NONASCII:
|
||||||
|
|
|
@ -363,11 +363,21 @@ class CmdLineTest(unittest.TestCase):
|
||||||
self.assertTrue(text[1].startswith(' File '))
|
self.assertTrue(text[1].startswith(' File '))
|
||||||
self.assertTrue(text[3].startswith('NameError'))
|
self.assertTrue(text[3].startswith('NameError'))
|
||||||
|
|
||||||
@unittest.skipUnless(support.TESTFN_NONASCII, 'need support.TESTFN_NONASCII')
|
|
||||||
def test_non_ascii(self):
|
def test_non_ascii(self):
|
||||||
|
# Mac OS X denies the creation of a file with an invalid UTF-8 name.
|
||||||
|
# Windows allows to create a name with an arbitrary bytes name, but
|
||||||
|
# Python cannot a undecodable bytes argument to a subprocess.
|
||||||
|
if (support.TESTFN_UNDECODABLE
|
||||||
|
and sys.platform not in ('win32', 'darwin')):
|
||||||
|
name = os.fsdecode(support.TESTFN_UNDECODABLE)
|
||||||
|
elif support.TESTFN_NONASCII:
|
||||||
|
name = support.TESTFN_NONASCII
|
||||||
|
else:
|
||||||
|
self.skipTest("need support.TESTFN_NONASCII")
|
||||||
|
|
||||||
# Issue #16218
|
# Issue #16218
|
||||||
source = 'print(ascii(__file__))\n'
|
source = 'print(ascii(__file__))\n'
|
||||||
script_name = _make_test_script(os.curdir, support.TESTFN_NONASCII, source)
|
script_name = _make_test_script(os.curdir, name, source)
|
||||||
self.addCleanup(support.unlink, script_name)
|
self.addCleanup(support.unlink, script_name)
|
||||||
rc, stdout, stderr = assert_python_ok(script_name)
|
rc, stdout, stderr = assert_python_ok(script_name)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
|
@ -309,26 +309,17 @@ class CommonTest(GenericTest):
|
||||||
self.assertIsInstance(abspath(path), str)
|
self.assertIsInstance(abspath(path), str)
|
||||||
|
|
||||||
def test_nonascii_abspath(self):
|
def test_nonascii_abspath(self):
|
||||||
# Test non-ASCII in the path
|
if (support.TESTFN_UNDECODABLE
|
||||||
if sys.platform in ('win32', 'darwin'):
|
# Mac OS X denies the creation of a directory with an invalid
|
||||||
if support.TESTFN_NONASCII:
|
# UTF-8 name. Windows allows to create a directory with an
|
||||||
name = support.TESTFN_NONASCII
|
# arbitrary bytes name, but fails to enter this directory
|
||||||
else:
|
# (when the bytes name is used).
|
||||||
# Mac OS X denies the creation of a directory with an invalid
|
and sys.platform not in ('win32', 'darwin')):
|
||||||
# UTF-8 name. Windows allows to create a directory with an
|
name = support.TESTFN_UNDECODABLE
|
||||||
# arbitrary bytes name, but fails to enter this directory
|
elif support.TESTFN_NONASCII:
|
||||||
# (when the bytes name is used).
|
name = support.TESTFN_NONASCII
|
||||||
self.skipTest("need support.TESTFN_NONASCII")
|
|
||||||
else:
|
else:
|
||||||
if support.TESTFN_UNDECODABLE:
|
self.skipTest("need support.TESTFN_NONASCII")
|
||||||
name = support.TESTFN_UNDECODABLE
|
|
||||||
elif support.TESTFN_NONASCII:
|
|
||||||
name = support.TESTFN_NONASCII
|
|
||||||
else:
|
|
||||||
# On UNIX, the surrogateescape error handler is used to
|
|
||||||
# decode paths, so any byte is allowed, it does not depend
|
|
||||||
# on the locale
|
|
||||||
name = b'a\xffb\xe7w\xf0'
|
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter("ignore", DeprecationWarning)
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
|
|
Loading…
Reference in New Issue