Kaydet (Commit) 65eb453d authored tarafından Florent Xicluna's avatar Florent Xicluna

Use PEP-3151 exceptions for test_pep277.

üst 7581cef6
...@@ -38,8 +38,8 @@ if sys.platform != 'darwin': ...@@ -38,8 +38,8 @@ if sys.platform != 'darwin':
'17_\u2001\u2001\u2001A', '17_\u2001\u2001\u2001A',
'18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A') '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
'19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') == '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
# NFKC('\u2001') == NFKC('\u2003') # NFKC('\u2001') == NFKC('\u2003')
]) ])
# Is it Unicode-friendly? # Is it Unicode-friendly?
...@@ -71,7 +71,7 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -71,7 +71,7 @@ class UnicodeFileTests(unittest.TestCase):
def setUp(self): def setUp(self):
try: try:
os.mkdir(support.TESTFN) os.mkdir(support.TESTFN)
except OSError: except FileExistsError:
pass pass
files = set() files = set()
for name in self.files: for name in self.files:
...@@ -90,15 +90,16 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -90,15 +90,16 @@ class UnicodeFileTests(unittest.TestCase):
return normalize(self.normal_form, s) return normalize(self.normal_form, s)
return s return s
def _apply_failure(self, fn, filename, expected_exception, def _apply_failure(self, fn, filename,
check_fn_in_exception = True): expected_exception=FileNotFoundError,
check_filename=True):
with self.assertRaises(expected_exception) as c: with self.assertRaises(expected_exception) as c:
fn(filename) fn(filename)
exc_filename = c.exception.filename exc_filename = c.exception.filename
# the "filename" exception attribute may be encoded # the "filename" exception attribute may be encoded
if isinstance(exc_filename, bytes): if isinstance(exc_filename, bytes):
filename = filename.encode(sys.getfilesystemencoding()) filename = filename.encode(sys.getfilesystemencoding())
if check_fn_in_exception: if check_filename:
self.assertEqual(exc_filename, filename, "Function '%s(%a) failed " self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
"with bad filename in the exception: %a" % "with bad filename in the exception: %a" %
(fn.__name__, filename, exc_filename)) (fn.__name__, filename, exc_filename))
...@@ -107,13 +108,13 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -107,13 +108,13 @@ class UnicodeFileTests(unittest.TestCase):
# Pass non-existing Unicode filenames all over the place. # Pass non-existing Unicode filenames all over the place.
for name in self.files: for name in self.files:
name = "not_" + name name = "not_" + name
self._apply_failure(open, name, IOError) self._apply_failure(open, name)
self._apply_failure(os.stat, name, OSError) self._apply_failure(os.stat, name)
self._apply_failure(os.chdir, name, OSError) self._apply_failure(os.chdir, name)
self._apply_failure(os.rmdir, name, OSError) self._apply_failure(os.rmdir, name)
self._apply_failure(os.remove, name, OSError) self._apply_failure(os.remove, name)
# listdir may append a wildcard to the filename, so dont check # listdir may append a wildcard to the filename, so dont check
self._apply_failure(os.listdir, name, OSError, False) self._apply_failure(os.listdir, name, check_filename=False)
def test_open(self): def test_open(self):
for name in self.files: for name in self.files:
...@@ -121,12 +122,13 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -121,12 +122,13 @@ class UnicodeFileTests(unittest.TestCase):
f.write((name+'\n').encode("utf-8")) f.write((name+'\n').encode("utf-8"))
f.close() f.close()
os.stat(name) os.stat(name)
self._apply_failure(os.listdir, name, NotADirectoryError)
# Skip the test on darwin, because darwin does normalize the filename to # Skip the test on darwin, because darwin does normalize the filename to
# NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC, # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
# NFKD in Python is useless, because darwin will normalize it later and so # NFKD in Python is useless, because darwin will normalize it later and so
# open(), os.stat(), etc. don't raise any exception. # open(), os.stat(), etc. don't raise any exception.
@unittest.skipIf(sys.platform == 'darwin', 'irrevelant test on Mac OS X') @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
def test_normalize(self): def test_normalize(self):
files = set(self.files) files = set(self.files)
others = set() others = set()
...@@ -134,18 +136,18 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -134,18 +136,18 @@ class UnicodeFileTests(unittest.TestCase):
others |= set(normalize(nf, file) for file in files) others |= set(normalize(nf, file) for file in files)
others -= files others -= files
for name in others: for name in others:
self._apply_failure(open, name, IOError) self._apply_failure(open, name)
self._apply_failure(os.stat, name, OSError) self._apply_failure(os.stat, name)
self._apply_failure(os.chdir, name, OSError) self._apply_failure(os.chdir, name)
self._apply_failure(os.rmdir, name, OSError) self._apply_failure(os.rmdir, name)
self._apply_failure(os.remove, name, OSError) self._apply_failure(os.remove, name)
# listdir may append a wildcard to the filename, so dont check # listdir may append a wildcard to the filename, so dont check
self._apply_failure(os.listdir, name, OSError, False) self._apply_failure(os.listdir, name, False)
# Skip the test on darwin, because darwin uses a normalization different # Skip the test on darwin, because darwin uses a normalization different
# than Python NFD normalization: filenames are different even if we use # than Python NFD normalization: filenames are different even if we use
# Python NFD normalization. # Python NFD normalization.
@unittest.skipIf(sys.platform == 'darwin', 'irrevelant test on Mac OS X') @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
def test_listdir(self): def test_listdir(self):
sf0 = set(self.files) sf0 = set(self.files)
f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment