summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_os.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2012-10-30 01:17:38 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2012-10-30 01:17:38 (GMT)
commit292c835548df618983043b9698d0dc8c34adea12 (patch)
tree11ae17b994b6eaaaf5625d05839ad16af03443f9 /Lib/test/test_os.py
parent76df43de30f40b5cc1de9d36a5a083dd8bd8cb27 (diff)
downloadcpython-292c835548df618983043b9698d0dc8c34adea12.zip
cpython-292c835548df618983043b9698d0dc8c34adea12.tar.gz
cpython-292c835548df618983043b9698d0dc8c34adea12.tar.bz2
Issue #15478: Raising an OSError doesn't decode or encode the filename anymore
Pass the original filename argument to OSError constructor, instead of trying to encode it to or decode it from the filesystem encoding. This change avoids an additionnal UnicodeDecodeError on Windows if the filename cannot be decoded from the filesystem encoding (ANSI code page).
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r--Lib/test/test_os.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 7d6b377..e5864ed 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -2046,6 +2046,76 @@ class TermsizeTests(unittest.TestCase):
self.assertEqual(expected, actual)
+class OSErrorTests(unittest.TestCase):
+ def setUp(self):
+ class Str(str):
+ pass
+
+ self.filenames = []
+ if support.TESTFN_UNENCODABLE is not None:
+ decoded = support.TESTFN_UNENCODABLE
+ else:
+ decoded = support.TESTFN
+ self.filenames.append(decoded)
+ self.filenames.append(Str(decoded))
+ if support.TESTFN_UNDECODABLE is not None:
+ encoded = support.TESTFN_UNDECODABLE
+ else:
+ encoded = os.fsencode(support.TESTFN)
+ self.filenames.append(encoded)
+ self.filenames.append(memoryview(encoded))
+
+ def test_oserror_filename(self):
+ funcs = [
+ (os.chdir,),
+ (os.chmod, 0o777),
+ (os.chown, 0, 0),
+ (os.lchown, 0, 0),
+ (os.listdir,),
+ (os.lstat,),
+ (os.open, os.O_RDONLY),
+ (os.rename, "dst"),
+ (os.replace, "dst"),
+ (os.rmdir,),
+ (os.stat,),
+ (os.truncate, 0),
+ (os.unlink,),
+ ]
+ if sys.platform == "win32":
+ funcs.extend((
+ (os._getfullpathname,),
+ (os._isdir,),
+ ))
+ if hasattr(os, "chflags"):
+ funcs.extend((
+ (os.chflags, 0),
+ (os.lchflags, 0),
+ ))
+ if hasattr(os, "chroot"):
+ funcs.append((os.chroot,))
+ if hasattr(os, "link"):
+ funcs.append((os.link, "dst"))
+ if hasattr(os, "listxattr"):
+ funcs.extend((
+ (os.listxattr,),
+ (os.getxattr, "user.test"),
+ (os.setxattr, "user.test", b'user'),
+ (os.removexattr, "user.test"),
+ ))
+ if hasattr(os, "lchmod"):
+ funcs.append((os.lchmod, 0o777))
+ if hasattr(os, "readlink"):
+ funcs.append((os.readlink,))
+
+ for func, *func_args in funcs:
+ for name in self.filenames:
+ try:
+ func(name, *func_args)
+ except FileNotFoundError as err:
+ self.assertIs(err.filename, name)
+ else:
+ self.fail("No exception thrown by {}".format(func))
+
@support.reap_threads
def test_main():
support.run_unittest(
@@ -2074,6 +2144,7 @@ def test_main():
ExtendedAttributeTests,
Win32DeprecatedBytesAPI,
TermsizeTests,
+ OSErrorTests,
)
if __name__ == "__main__":