Skip to content

Commit 8b219b2

Browse files
committed
Issue #16414: Add support.FS_NONASCII and support.TESTFN_NONASCII
These constants are used to test functions with non-ASCII data, especially filenames.
1 parent df1d940 commit 8b219b2

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

Lib/test/support.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,32 @@ def _is_ipv6_enabled():
603603
# module name.
604604
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
605605

606+
# FS_NONASCII: non-ASCII character encodable by os.fsencode(),
607+
# or None if there is no such character.
608+
FS_NONASCII = None
609+
for character in (
610+
# U+00E6 (Latin small letter AE): Encodable to cp1252, cp1254, cp1257, iso-8859-1
611+
'\u00E6',
612+
# U+0141 (Latin capital letter L with stroke): Encodable to cp1250, cp1257
613+
'\u0141',
614+
# U+041A (Cyrillic capital letter KA): Encodable to cp932, cp950, cp1251
615+
'\u041A',
616+
# U+05D0 (Hebrew Letter Alef): Encodable to cp424, cp1255
617+
'\u05D0',
618+
# U+06A9 (Arabic letter KEHEH): Encodable to cp1256
619+
'\u06A9',
620+
# U+03A9 (Greek capital letter OMEGA): Encodable to cp932, cp950, cp1253
621+
'\u03A9',
622+
# U+0E01 (Thai character KO KAI): Encodable to cp874
623+
'\u0E01',
624+
):
625+
try:
626+
os.fsdecode(os.fsencode(character))
627+
except UnicodeError:
628+
pass
629+
else:
630+
FS_NONASCII = character
631+
break
606632

607633
# TESTFN_UNICODE is a non-ascii filename
608634
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
@@ -658,6 +684,11 @@ def _is_ipv6_enabled():
658684
TESTFN_UNDECODABLE = name
659685
break
660686

687+
if FS_NONASCII:
688+
TESTFN_NONASCII = TESTFN + '- ' + FS_NONASCII
689+
else:
690+
TESTFN_NONASCII = None
691+
661692
# Save the initial cwd
662693
SAVEDCWD = os.getcwd()
663694

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ def test_run_code(self):
9393
# All good if execution is successful
9494
assert_python_ok('-c', 'pass')
9595

96-
@unittest.skipIf(sys.getfilesystemencoding() == 'ascii',
97-
'need a filesystem encoding different than ASCII')
96+
@unittest.skipUnless(test.support.FS_NONASCII, 'need support.FS_NONASCII')
9897
def test_non_ascii(self):
9998
# Test handling of non-ascii data
10099
if test.support.verbose:
101100
import locale
102101
print('locale encoding = %s, filesystem encoding = %s'
103102
% (locale.getpreferredencoding(), sys.getfilesystemencoding()))
104-
command = "assert(ord('\xe9') == 0xe9)"
103+
command = ("assert(ord(%r) == %s)"
104+
% (test.support.FS_NONASCII, ord(test.support.FS_NONASCII)))
105105
assert_python_ok('-c', command)
106106

107107
# On Windows, pass bytes to subprocess doesn't test how Python decodes the

Lib/test/test_cmd_line_script.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,19 +363,12 @@ def test_pep_409_verbiage(self):
363363
self.assertTrue(text[1].startswith(' File '))
364364
self.assertTrue(text[3].startswith('NameError'))
365365

366+
@unittest.skipUnless(support.TESTFN_NONASCII, 'need support.TESTFN_NONASCII')
366367
def test_non_ascii(self):
367368
# Issue #16218
368369
# non-ascii filename encodable to cp1252, cp932, latin1 and utf8
369-
filename = support.TESTFN + '\xa3'
370-
try:
371-
os.fsencode(filename)
372-
except UnicodeEncodeError:
373-
self.skipTest(
374-
"Filesystem encoding %r cannot encode "
375-
"the filename: %a"
376-
% (sys.getfilesystemencoding(), filename))
377370
source = 'print(ascii(__file__))\n'
378-
script_name = _make_test_script(os.curdir, filename, source)
371+
script_name = _make_test_script(os.curdir, support.TESTFN_NONASCII, source)
379372
self.addCleanup(support.unlink, script_name)
380373
rc, stdout, stderr = assert_python_ok(script_name)
381374
self.assertEqual(

Lib/test/test_genericpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ def test_abspath_issue3426(self):
313313
def test_nonascii_abspath(self):
314314
if support.TESTFN_UNDECODABLE:
315315
name = support.TESTFN_UNDECODABLE
316+
elif support.TESTFN_NONASCII:
317+
name = support.TESTFN_NONASCII
316318
else:
317319
name = b'a\xffb\xe7w\xf0'
318320

Lib/test/test_os.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,8 @@ class Pep383Tests(unittest.TestCase):
12431243
def setUp(self):
12441244
if support.TESTFN_UNENCODABLE:
12451245
self.dir = support.TESTFN_UNENCODABLE
1246+
elif support.TESTFN_NONASCII:
1247+
self.dir = support.TESTFN_NONASCII
12461248
else:
12471249
self.dir = support.TESTFN
12481250
self.bdir = os.fsencode(self.dir)
@@ -1257,6 +1259,8 @@ def add_filename(fn):
12571259
add_filename(support.TESTFN_UNICODE)
12581260
if support.TESTFN_UNENCODABLE:
12591261
add_filename(support.TESTFN_UNENCODABLE)
1262+
if support.TESTFN_NONASCII:
1263+
add_filename(support.TESTFN_NONASCII)
12601264
if not bytesfn:
12611265
self.skipTest("couldn't create any non-ascii filename")
12621266

0 commit comments

Comments
 (0)