summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2016-06-04 19:06:26 (GMT)
committerEthan Furman <ethan@stoneleaf.us>2016-06-04 19:06:26 (GMT)
commit410ef8e23088ab2b8bd92ac70a8176f71da2b931 (patch)
treedceeb23e4a2dcddecc72e0bc6faa15a1177ab633 /Lib
parentc55014f3717ce8a810a7e934c8ad5b519dbf88c5 (diff)
downloadcpython-410ef8e23088ab2b8bd92ac70a8176f71da2b931.zip
cpython-410ef8e23088ab2b8bd92ac70a8176f71da2b931.tar.gz
cpython-410ef8e23088ab2b8bd92ac70a8176f71da2b931.tar.bz2
issue27186: add C version of os.fspath(); patch by Jelle Zijlstra
Diffstat (limited to 'Lib')
-rw-r--r--Lib/os.py35
-rw-r--r--Lib/test/test_os.py7
2 files changed, 25 insertions, 17 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 1318de6..0131ed8 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -1104,23 +1104,24 @@ def fdopen(fd, *args, **kwargs):
import io
return io.open(fd, *args, **kwargs)
-# Supply os.fspath()
-def fspath(path):
- """Return the string representation of the path.
+# Supply os.fspath() if not defined in C
+if not _exists('fspath'):
+ def fspath(path):
+ """Return the string representation of the path.
- If str or bytes is passed in, it is returned unchanged.
- """
- if isinstance(path, (str, bytes)):
- return path
+ If str or bytes is passed in, it is returned unchanged.
+ """
+ if isinstance(path, (str, bytes)):
+ return path
- # Work from the object's type to match method resolution of other magic
- # methods.
- path_type = type(path)
- try:
- return path_type.__fspath__(path)
- except AttributeError:
- if hasattr(path_type, '__fspath__'):
- raise
+ # Work from the object's type to match method resolution of other magic
+ # methods.
+ path_type = type(path)
+ try:
+ return path_type.__fspath__(path)
+ except AttributeError:
+ if hasattr(path_type, '__fspath__'):
+ raise
- raise TypeError("expected str, bytes or os.PathLike object, not "
- + path_type.__name__)
+ raise TypeError("expected str, bytes or os.PathLike object, not "
+ + path_type.__name__)
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 84ef150..bf06438 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3121,6 +3121,13 @@ class TestPEP519(unittest.TestCase):
self.assertEqual(b"path/like/object", os.fsencode(pathlike))
self.assertEqual("path/like/object", os.fsdecode(pathlike))
+ def test_fspathlike(self):
+ class PathLike(object):
+ def __fspath__(self):
+ return '#feelthegil'
+
+ self.assertEqual('#feelthegil', os.fspath(PathLike()))
+
def test_garbage_in_exception_out(self):
vapor = type('blah', (), {})
for o in int, type, os, vapor():