mirror of https://github.com/python/cpython.git
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.
This commit is contained in:
parent
6c00c1464f
commit
d91df1a7a9
|
@ -377,10 +377,8 @@ def fcmp(x, y): # fuzzy comparison function
|
||||||
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
|
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
|
||||||
|
|
||||||
|
|
||||||
# Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding()
|
# TESTFN_UNICODE is a non-ascii filename
|
||||||
# TESTFN_UNICODE is a filename that can be encoded using the
|
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
|
||||||
# file system encoding, but *not* with the default (ascii) encoding
|
|
||||||
TESTFN_UNICODE = TESTFN + "-\xe0\xf2"
|
|
||||||
TESTFN_ENCODING = sys.getfilesystemencoding()
|
TESTFN_ENCODING = sys.getfilesystemencoding()
|
||||||
|
|
||||||
# TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be
|
# TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be
|
||||||
|
|
|
@ -895,29 +895,55 @@ def test_setregid_neg1(self):
|
||||||
sys.executable, '-c',
|
sys.executable, '-c',
|
||||||
'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
|
'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
|
||||||
|
|
||||||
@unittest.skipIf(sys.platform == 'darwin', "tests don't apply to OS X")
|
|
||||||
class Pep383Tests(unittest.TestCase):
|
class Pep383Tests(unittest.TestCase):
|
||||||
filenames = [b'foo\xf6bar', 'foo\xf6bar'.encode("utf-8")]
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.fsencoding = sys.getfilesystemencoding()
|
def fsdecode(filename):
|
||||||
sys.setfilesystemencoding("utf-8")
|
encoding = sys.getfilesystemencoding()
|
||||||
|
if encoding == 'mbcs':
|
||||||
|
errors = 'strict'
|
||||||
|
else:
|
||||||
|
errors = 'surrogateescape'
|
||||||
|
return filename.decode(encoding, errors)
|
||||||
|
|
||||||
|
if support.TESTFN_UNENCODABLE:
|
||||||
|
self.dir = support.TESTFN_UNENCODABLE
|
||||||
|
else:
|
||||||
self.dir = support.TESTFN
|
self.dir = support.TESTFN
|
||||||
self.bdir = self.dir.encode("utf-8", "surrogateescape")
|
self.bdir = os.fsencode(self.dir)
|
||||||
|
|
||||||
|
bytesfn = []
|
||||||
|
def add_filename(fn):
|
||||||
|
try:
|
||||||
|
fn = os.fsencode(fn)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return
|
||||||
|
bytesfn.append(fn)
|
||||||
|
add_filename(support.TESTFN_UNICODE)
|
||||||
|
if support.TESTFN_UNENCODABLE:
|
||||||
|
add_filename(support.TESTFN_UNENCODABLE)
|
||||||
|
if not bytesfn:
|
||||||
|
self.skipTest("couldn't create any non-ascii filename")
|
||||||
|
|
||||||
|
self.unicodefn = set()
|
||||||
os.mkdir(self.dir)
|
os.mkdir(self.dir)
|
||||||
self.unicodefn = []
|
try:
|
||||||
for fn in self.filenames:
|
for fn in bytesfn:
|
||||||
f = open(os.path.join(self.bdir, fn), "w")
|
f = open(os.path.join(self.bdir, fn), "w")
|
||||||
f.close()
|
f.close()
|
||||||
self.unicodefn.append(fn.decode("utf-8", "surrogateescape"))
|
fn = fsdecode(fn)
|
||||||
|
if fn in self.unicodefn:
|
||||||
|
raise ValueError("duplicate filename")
|
||||||
|
self.unicodefn.add(fn)
|
||||||
|
except:
|
||||||
|
shutil.rmtree(self.dir)
|
||||||
|
raise
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
shutil.rmtree(self.dir)
|
shutil.rmtree(self.dir)
|
||||||
sys.setfilesystemencoding(self.fsencoding)
|
|
||||||
|
|
||||||
def test_listdir(self):
|
def test_listdir(self):
|
||||||
expected = set(self.unicodefn)
|
expected = self.unicodefn
|
||||||
found = set(os.listdir(support.TESTFN))
|
found = set(os.listdir(self.dir))
|
||||||
self.assertEquals(found, expected)
|
self.assertEquals(found, expected)
|
||||||
|
|
||||||
def test_open(self):
|
def test_open(self):
|
||||||
|
|
Loading…
Reference in New Issue