summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2016-06-10 19:20:49 (GMT)
committerBrett Cannon <brett@python.org>2016-06-10 19:20:49 (GMT)
commit568be63248614a2cdd7666a67ddfd16e817f7db9 (patch)
treef59d0822885c5185a7dfb464cd443ea8d5696109 /Lib/pathlib.py
parentf41b82fb19d1b91f99ab657e4c6a751c44152f12 (diff)
downloadcpython-568be63248614a2cdd7666a67ddfd16e817f7db9.zip
cpython-568be63248614a2cdd7666a67ddfd16e817f7db9.tar.gz
cpython-568be63248614a2cdd7666a67ddfd16e817f7db9.tar.bz2
Issue #27186: Add os.PathLike support to pathlib.
This adds support both to pathlib.PurePath's constructor as well as implementing __fspath__(). This removes the provisional status for pathlib. Initial patch by Dusty Phillips.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 1480e2f..a06676f 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -634,13 +634,16 @@ class PurePath(object):
for a in args:
if isinstance(a, PurePath):
parts += a._parts
- elif isinstance(a, str):
- # Force-cast str subclasses to str (issue #21127)
- parts.append(str(a))
else:
- raise TypeError(
- "argument should be a path or str object, not %r"
- % type(a))
+ a = os.fspath(a)
+ if isinstance(a, str):
+ # Force-cast str subclasses to str (issue #21127)
+ parts.append(str(a))
+ else:
+ raise TypeError(
+ "argument should be a str object or an os.PathLike "
+ "object returning str, not %r"
+ % type(a))
return cls._flavour.parse_parts(parts)
@classmethod
@@ -693,6 +696,9 @@ class PurePath(object):
self._parts) or '.'
return self._str
+ def __fspath__(self):
+ return str(self)
+
def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
@@ -943,6 +949,10 @@ class PurePath(object):
return False
return True
+# Can't subclass os.PathLike from PurePath and keep the constructor
+# optimizations in PurePath._parse_args().
+os.PathLike.register(PurePath)
+
class PurePosixPath(PurePath):
_flavour = _posix_flavour