cpython: 00991aa5fdb5 (original) (raw)
Mercurial > cpython
changeset 101698:00991aa5fdb5
issue27182: update fsencode and fsdecode for os.path(); patch by Dusty Phillips [#27182]
Ethan Furman ethan@stoneleaf.us | |
---|---|
date | Sat, 04 Jun 2016 10:19:27 -0700 |
parents | 7fda2d76d947 |
children | 416441e4afab |
files | Lib/os.py Lib/test/test_os.py |
diffstat | 2 files changed, 29 insertions(+), 8 deletions(-)[+] [-] Lib/os.py 22 Lib/test/test_os.py 15 |
line wrap: on
line diff
--- a/Lib/os.py +++ b/Lib/os.py @@ -877,29 +877,35 @@ def _fscodec(): def fsencode(filename): """
Encode filename to the filesystem encoding with 'surrogateescape' error[](#l1.7)
handler, return bytes unchanged. On Windows, use 'strict' error handler if[](#l1.8)
the file system encoding is 'mbcs' (which is the default encoding).[](#l1.9)
Encode filename (an os.PathLike, bytes, or str) to the filesystem[](#l1.10)
encoding with 'surrogateescape' error handler, return bytes unchanged.[](#l1.11)
On Windows, use 'strict' error handler if the file system encoding is[](#l1.12)
'mbcs' (which is the default encoding).[](#l1.13) """[](#l1.14)
filename = fspath(filename)[](#l1.15) if isinstance(filename, bytes):[](#l1.16) return filename[](#l1.17) elif isinstance(filename, str):[](#l1.18) return filename.encode(encoding, errors)[](#l1.19) else:[](#l1.20)
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)[](#l1.21)
raise TypeError("expected str, bytes or os.PathLike object, not "[](#l1.22)
+ path_type.__name__)[](#l1.23)
Decode filename from the filesystem encoding with 'surrogateescape' error[](#l1.27)
handler, return str unchanged. On Windows, use 'strict' error handler if[](#l1.28)
the file system encoding is 'mbcs' (which is the default encoding).[](#l1.29)
Decode filename (an os.PathLike, bytes, or str) from the filesystem[](#l1.30)
encoding with 'surrogateescape' error handler, return str unchanged. On[](#l1.31)
Windows, use 'strict' error handler if the file system encoding is[](#l1.32)
'mbcs' (which is the default encoding).[](#l1.33) """[](#l1.34)
filename = fspath(filename)[](#l1.35) if isinstance(filename, str):[](#l1.36) return filename[](#l1.37) elif isinstance(filename, bytes):[](#l1.38) return filename.decode(encoding, errors)[](#l1.39) else:[](#l1.40)
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)[](#l1.41)
raise TypeError("expected str, bytes or os.PathLike object, not "[](#l1.42)
+ path_type.__name__)[](#l1.43)
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3106,6 +3106,21 @@ class TestPEP519(unittest.TestCase): for s in 'hello', 'goodbye', 'some/path/and/file': self.assertEqual(s, os.fspath(s))
- def test_fsencode_fsdecode_return_pathlike(self):
class Pathlike:[](#l2.8)
def __init__(self, path):[](#l2.9)
self.path = path[](#l2.10)
def __fspath__(self):[](#l2.12)
return self.path[](#l2.13)
for p in "path/like/object", b"path/like/object":[](#l2.15)
pathlike = Pathlike(p)[](#l2.16)
self.assertEqual(p, os.fspath(pathlike))[](#l2.18)
self.assertEqual(b"path/like/object", os.fsencode(pathlike))[](#l2.19)
self.assertEqual("path/like/object", os.fsdecode(pathlike))[](#l2.20)
+ def test_garbage_in_exception_out(self): vapor = type('blah', (), {}) for o in int, type, os, vapor():