[Python-checkins] cpython: issue27186: fix fsencode/fsdecode and update tests; patch by Jelle Zijlstra

ethan.furman python-checkins at python.org
Sat Jun 4 16:48:07 EDT 2016


https://hg.python.org/cpython/rev/76b2ddaee6bd
changeset: 101723:76b2ddaee6bd
user: Ethan Furman <ethan at stoneleaf.us>
date: Sat Jun 04 13:47:39 2016 -0700
summary:
 issue27186: fix fsencode/fsdecode and update tests; patch by Jelle Zijlstra
files:
 Lib/os.py | 4 ++--
 Lib/test/test_os.py | 21 ++++++++++++++-------
 2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -889,7 +889,7 @@
 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 @@
 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
 
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3107,29 +3107,36 @@
 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'
-
- self.assertEqual('#feelthegil', os.fspath(PathLike()))
+ return self.path
+
+ 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():
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /