mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Issue #16414: Add support.FS_NONASCII and support.TESTFN_NONASCII
These constants are used to test functions with non-ASCII data, especially filenames.
This commit is contained in:
parent
df1d940c7c
commit
8b219b2936
5 changed files with 42 additions and 12 deletions
|
@ -603,6 +603,32 @@ else:
|
||||||
# module name.
|
# module name.
|
||||||
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
|
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
|
||||||
|
|
||||||
|
# FS_NONASCII: non-ASCII character encodable by os.fsencode(),
|
||||||
|
# or None if there is no such character.
|
||||||
|
FS_NONASCII = None
|
||||||
|
for character in (
|
||||||
|
# U+00E6 (Latin small letter AE): Encodable to cp1252, cp1254, cp1257, iso-8859-1
|
||||||
|
'\u00E6',
|
||||||
|
# U+0141 (Latin capital letter L with stroke): Encodable to cp1250, cp1257
|
||||||
|
'\u0141',
|
||||||
|
# U+041A (Cyrillic capital letter KA): Encodable to cp932, cp950, cp1251
|
||||||
|
'\u041A',
|
||||||
|
# U+05D0 (Hebrew Letter Alef): Encodable to cp424, cp1255
|
||||||
|
'\u05D0',
|
||||||
|
# U+06A9 (Arabic letter KEHEH): Encodable to cp1256
|
||||||
|
'\u06A9',
|
||||||
|
# U+03A9 (Greek capital letter OMEGA): Encodable to cp932, cp950, cp1253
|
||||||
|
'\u03A9',
|
||||||
|
# U+0E01 (Thai character KO KAI): Encodable to cp874
|
||||||
|
'\u0E01',
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
os.fsdecode(os.fsencode(character))
|
||||||
|
except UnicodeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
FS_NONASCII = character
|
||||||
|
break
|
||||||
|
|
||||||
# TESTFN_UNICODE is a non-ascii filename
|
# TESTFN_UNICODE is a non-ascii filename
|
||||||
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
|
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
|
||||||
|
@ -658,6 +684,11 @@ for name in (b'abc\xff', b'\xe7w\xf0'):
|
||||||
TESTFN_UNDECODABLE = name
|
TESTFN_UNDECODABLE = name
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if FS_NONASCII:
|
||||||
|
TESTFN_NONASCII = TESTFN + '- ' + FS_NONASCII
|
||||||
|
else:
|
||||||
|
TESTFN_NONASCII = None
|
||||||
|
|
||||||
# Save the initial cwd
|
# Save the initial cwd
|
||||||
SAVEDCWD = os.getcwd()
|
SAVEDCWD = os.getcwd()
|
||||||
|
|
||||||
|
|
|
@ -93,15 +93,15 @@ class CmdLineTest(unittest.TestCase):
|
||||||
# All good if execution is successful
|
# All good if execution is successful
|
||||||
assert_python_ok('-c', 'pass')
|
assert_python_ok('-c', 'pass')
|
||||||
|
|
||||||
@unittest.skipIf(sys.getfilesystemencoding() == 'ascii',
|
@unittest.skipUnless(test.support.FS_NONASCII, 'need support.FS_NONASCII')
|
||||||
'need a filesystem encoding different than ASCII')
|
|
||||||
def test_non_ascii(self):
|
def test_non_ascii(self):
|
||||||
# Test handling of non-ascii data
|
# Test handling of non-ascii data
|
||||||
if test.support.verbose:
|
if test.support.verbose:
|
||||||
import locale
|
import locale
|
||||||
print('locale encoding = %s, filesystem encoding = %s'
|
print('locale encoding = %s, filesystem encoding = %s'
|
||||||
% (locale.getpreferredencoding(), sys.getfilesystemencoding()))
|
% (locale.getpreferredencoding(), sys.getfilesystemencoding()))
|
||||||
command = "assert(ord('\xe9') == 0xe9)"
|
command = ("assert(ord(%r) == %s)"
|
||||||
|
% (test.support.FS_NONASCII, ord(test.support.FS_NONASCII)))
|
||||||
assert_python_ok('-c', command)
|
assert_python_ok('-c', command)
|
||||||
|
|
||||||
# On Windows, pass bytes to subprocess doesn't test how Python decodes the
|
# On Windows, pass bytes to subprocess doesn't test how Python decodes the
|
||||||
|
|
|
@ -363,19 +363,12 @@ 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):
|
||||||
# Issue #16218
|
# Issue #16218
|
||||||
# non-ascii filename encodable to cp1252, cp932, latin1 and utf8
|
# non-ascii filename encodable to cp1252, cp932, latin1 and utf8
|
||||||
filename = support.TESTFN + '\xa3'
|
|
||||||
try:
|
|
||||||
os.fsencode(filename)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
self.skipTest(
|
|
||||||
"Filesystem encoding %r cannot encode "
|
|
||||||
"the filename: %a"
|
|
||||||
% (sys.getfilesystemencoding(), filename))
|
|
||||||
source = 'print(ascii(__file__))\n'
|
source = 'print(ascii(__file__))\n'
|
||||||
script_name = _make_test_script(os.curdir, filename, source)
|
script_name = _make_test_script(os.curdir, support.TESTFN_NONASCII, 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(
|
||||||
|
|
|
@ -313,6 +313,8 @@ class CommonTest(GenericTest):
|
||||||
def test_nonascii_abspath(self):
|
def test_nonascii_abspath(self):
|
||||||
if support.TESTFN_UNDECODABLE:
|
if support.TESTFN_UNDECODABLE:
|
||||||
name = support.TESTFN_UNDECODABLE
|
name = support.TESTFN_UNDECODABLE
|
||||||
|
elif support.TESTFN_NONASCII:
|
||||||
|
name = support.TESTFN_NONASCII
|
||||||
else:
|
else:
|
||||||
name = b'a\xffb\xe7w\xf0'
|
name = b'a\xffb\xe7w\xf0'
|
||||||
|
|
||||||
|
|
|
@ -1243,6 +1243,8 @@ if sys.platform != 'win32':
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
if support.TESTFN_UNENCODABLE:
|
if support.TESTFN_UNENCODABLE:
|
||||||
self.dir = support.TESTFN_UNENCODABLE
|
self.dir = support.TESTFN_UNENCODABLE
|
||||||
|
elif support.TESTFN_NONASCII:
|
||||||
|
self.dir = support.TESTFN_NONASCII
|
||||||
else:
|
else:
|
||||||
self.dir = support.TESTFN
|
self.dir = support.TESTFN
|
||||||
self.bdir = os.fsencode(self.dir)
|
self.bdir = os.fsencode(self.dir)
|
||||||
|
@ -1257,6 +1259,8 @@ if sys.platform != 'win32':
|
||||||
add_filename(support.TESTFN_UNICODE)
|
add_filename(support.TESTFN_UNICODE)
|
||||||
if support.TESTFN_UNENCODABLE:
|
if support.TESTFN_UNENCODABLE:
|
||||||
add_filename(support.TESTFN_UNENCODABLE)
|
add_filename(support.TESTFN_UNENCODABLE)
|
||||||
|
if support.TESTFN_NONASCII:
|
||||||
|
add_filename(support.TESTFN_NONASCII)
|
||||||
if not bytesfn:
|
if not bytesfn:
|
||||||
self.skipTest("couldn't create any non-ascii filename")
|
self.skipTest("couldn't create any non-ascii filename")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue