Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 228c636

Browse files
committed
issue27186: fix fsencode/fsdecode and update tests; patch by Jelle Zijlstra
1 parent db780cf commit 228c636

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ def fsencode(filename):
889889
return filename.encode(encoding, errors)
890890
else:
891891
raise TypeError("expected str, bytes or os.PathLike object, not "
892-
+ path_type.__name__)
892+
+ type(filename).__name__)
893893

894894
def fsdecode(filename):
895895
"""
@@ -905,7 +905,7 @@ def fsdecode(filename):
905905
return filename.decode(encoding, errors)
906906
else:
907907
raise TypeError("expected str, bytes or os.PathLike object, not "
908-
+ path_type.__name__)
908+
+ type(filename).__name__)
909909

910910
return fsencode, fsdecode
911911

Lib/test/test_os.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,29 +3107,36 @@ def test_return_string(self):
31073107
self.assertEqual(s, os.fspath(s))
31083108

31093109
def test_fsencode_fsdecode_return_pathlike(self):
3110-
class Pathlike:
3110+
class PathLike:
31113111
def __init__(self, path):
31123112
self.path = path
3113-
31143113
def __fspath__(self):
31153114
return self.path
31163115

31173116
for p in "path/like/object", b"path/like/object":
3118-
pathlike = Pathlike(p)
3117+
pathlike = PathLike(p)
31193118

31203119
self.assertEqual(p, os.fspath(pathlike))
31213120
self.assertEqual(b"path/like/object", os.fsencode(pathlike))
31223121
self.assertEqual("path/like/object", os.fsdecode(pathlike))
31233122

31243123
def test_fspathlike(self):
3125-
class PathLike(object):
3124+
class PathLike:
3125+
def __init__(self, path=''):
3126+
self.path = path
31263127
def __fspath__(self):
3127-
return '#feelthegil'
3128+
return self.path
31283129

3129-
self.assertEqual('#feelthegil', os.fspath(PathLike()))
3130+
self.assertEqual('#feelthegil', os.fspath(PathLike('#feelthegil')))
31303131
self.assertTrue(issubclass(PathLike, os.PathLike))
31313132
self.assertTrue(isinstance(PathLike(), os.PathLike))
31323133

3134+
message = 'expected str, bytes or os.PathLike object, not'
3135+
for fn in (os.fsencode, os.fsdecode):
3136+
for obj in PathLike(None), None:
3137+
with self.assertRaisesRegex(TypeError, message):
3138+
fn(obj)
3139+
31333140
def test_garbage_in_exception_out(self):
31343141
vapor = type('blah', (), {})
31353142
for o in int, type, os, vapor():

0 commit comments

Comments
 (0)