]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
issue27186: fix fsencode/fsdecode and update tests; patch by Jelle Zijlstra
authorEthan Furman <ethan@stoneleaf.us>
Sat, 4 Jun 2016 20:47:39 +0000 (13:47 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Sat, 4 Jun 2016 20:47:39 +0000 (13:47 -0700)
Lib/os.py
Lib/test/test_os.py

index e7d089ed35b32325a0ced57e90242408e78b4a4b..56a2f7187b55a8a985213699a50a2d61bd00f4ff 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -889,7 +889,7 @@ def _fscodec():
             return filename.encode(encoding, errors)
         else:
             raise TypeError("expected str, bytes or os.PathLike object, not "
-                            + path_type.__name__)
+                            + type(filename).__name__)
 
     def fsdecode(filename):
         """
@@ -905,7 +905,7 @@ def _fscodec():
             return filename.decode(encoding, errors)
         else:
             raise TypeError("expected str, bytes or os.PathLike object, not "
-                            + path_type.__name__)
+                            + type(filename).__name__)
 
     return fsencode, fsdecode
 
index e740edf644e796373002c8d31a06bd65f1086983..6853ebbd7fff2d09135f5e61548591e99ca7a896 100644 (file)
@@ -3107,29 +3107,36 @@ class TestPEP519(unittest.TestCase):
             self.assertEqual(s, os.fspath(s))
 
     def test_fsencode_fsdecode_return_pathlike(self):
-        class Pathlike:
+        class PathLike:
             def __init__(self, path):
                 self.path = path
-
             def __fspath__(self):
                 return self.path
 
         for p in "path/like/object", b"path/like/object":
-            pathlike = Pathlike(p)
+            pathlike = PathLike(p)
 
             self.assertEqual(p, os.fspath(pathlike))
             self.assertEqual(b"path/like/object", os.fsencode(pathlike))
             self.assertEqual("path/like/object", os.fsdecode(pathlike))
 
     def test_fspathlike(self):
-        class PathLike(object):
+        class PathLike:
+            def __init__(self, path=''):
+                self.path = path
             def __fspath__(self):
-                return '#feelthegil'
+                return self.path
 
-        self.assertEqual('#feelthegil', os.fspath(PathLike()))
+        self.assertEqual('#feelthegil', os.fspath(PathLike('#feelthegil')))
         self.assertTrue(issubclass(PathLike, os.PathLike))
         self.assertTrue(isinstance(PathLike(), os.PathLike))
 
+        message = 'expected str, bytes or os.PathLike object, not'
+        for fn in (os.fsencode, os.fsdecode):
+            for obj in PathLike(None), None:
+                with self.assertRaisesRegex(TypeError, message):
+                    fn(obj)
+
     def test_garbage_in_exception_out(self):
         vapor = type('blah', (), {})
         for o in int, type, os, vapor():