Skip to content

Commit d91df1a

Browse files
author
Victor Stinner
committed
Improve PEP 383 tests (in test_os)
* Use the current filesystem encoding instead of always using utf-8 * Enable the test on Mac OS X * Use TESTFN_UNENCODABLE and TESTFN_UNICODE instead of arbitrary filenames * To decode a filename, use strict error handler instead surrogateescape for mbcs encoding (on Windows) * Use TESTFN_UNENCODABLE (if available) for the directory name Skip the test if no non-ascii filename can be created.
1 parent 6c00c14 commit d91df1a

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

Lib/test/support.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,8 @@ def fcmp(x, y): # fuzzy comparison function
377377
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
378378

379379

380-
# Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding()
381-
# TESTFN_UNICODE is a filename that can be encoded using the
382-
# file system encoding, but *not* with the default (ascii) encoding
383-
TESTFN_UNICODE = TESTFN + "-\xe0\xf2"
380+
# TESTFN_UNICODE is a non-ascii filename
381+
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
384382
TESTFN_ENCODING = sys.getfilesystemencoding()
385383

386384
# TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be

Lib/test/test_os.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -895,29 +895,55 @@ def test_setregid_neg1(self):
895895
sys.executable, '-c',
896896
'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
897897

898-
@unittest.skipIf(sys.platform == 'darwin', "tests don't apply to OS X")
899898
class Pep383Tests(unittest.TestCase):
900-
filenames = [b'foo\xf6bar', 'foo\xf6bar'.encode("utf-8")]
901-
902899
def setUp(self):
903-
self.fsencoding = sys.getfilesystemencoding()
904-
sys.setfilesystemencoding("utf-8")
905-
self.dir = support.TESTFN
906-
self.bdir = self.dir.encode("utf-8", "surrogateescape")
900+
def fsdecode(filename):
901+
encoding = sys.getfilesystemencoding()
902+
if encoding == 'mbcs':
903+
errors = 'strict'
904+
else:
905+
errors = 'surrogateescape'
906+
return filename.decode(encoding, errors)
907+
908+
if support.TESTFN_UNENCODABLE:
909+
self.dir = support.TESTFN_UNENCODABLE
910+
else:
911+
self.dir = support.TESTFN
912+
self.bdir = os.fsencode(self.dir)
913+
914+
bytesfn = []
915+
def add_filename(fn):
916+
try:
917+
fn = os.fsencode(fn)
918+
except UnicodeEncodeError:
919+
return
920+
bytesfn.append(fn)
921+
add_filename(support.TESTFN_UNICODE)
922+
if support.TESTFN_UNENCODABLE:
923+
add_filename(support.TESTFN_UNENCODABLE)
924+
if not bytesfn:
925+
self.skipTest("couldn't create any non-ascii filename")
926+
927+
self.unicodefn = set()
907928
os.mkdir(self.dir)
908-
self.unicodefn = []
909-
for fn in self.filenames:
910-
f = open(os.path.join(self.bdir, fn), "w")
911-
f.close()
912-
self.unicodefn.append(fn.decode("utf-8", "surrogateescape"))
929+
try:
930+
for fn in bytesfn:
931+
f = open(os.path.join(self.bdir, fn), "w")
932+
f.close()
933+
fn = fsdecode(fn)
934+
if fn in self.unicodefn:
935+
raise ValueError("duplicate filename")
936+
self.unicodefn.add(fn)
937+
except:
938+
shutil.rmtree(self.dir)
939+
raise
913940

914941
def tearDown(self):
915942
shutil.rmtree(self.dir)
916-
sys.setfilesystemencoding(self.fsencoding)
917943

918944
def test_listdir(self):
919-
expected = set(self.unicodefn)
920-
found = set(os.listdir(support.TESTFN))
945+
expected = self.unicodefn
946+
found = set(os.listdir(self.dir))
921947
self.assertEquals(found, expected)
922948

923949
def test_open(self):

0 commit comments

Comments
 (0)