Skip to content

Commit 6f9fd60

Browse files
committed
Move PurePosixPath._splitroot(), is_absolute() and is_reserved() into PurePath
These are all reasonable default implementations for `AbstractPath`
1 parent 22942e4 commit 6f9fd60

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

Lib/pathlib.py

+20-26
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class PurePath(object):
329329
'_drv', '_root', '_parts',
330330
'_str', '_hash', '_pparts', '_cached_cparts',
331331
)
332+
_case_insensitive = False
332333

333334
def __new__(cls, *args):
334335
"""Construct a PurePath from one or several strings and or existing
@@ -345,6 +346,23 @@ def __reduce__(self):
345346
# when pickling related paths.
346347
return (type(self), tuple(self._parts))
347348

349+
@classmethod
350+
def _splitroot(cls, part):
351+
sep = cls._pathmod.sep
352+
if part and part[0] == sep:
353+
stripped_part = part.lstrip(sep)
354+
# According to POSIX path resolution:
355+
# http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11
356+
# "A pathname that begins with two successive slashes may be
357+
# interpreted in an implementation-defined manner, although more
358+
# than two leading slashes shall be treated as a single slash".
359+
if len(part) - len(stripped_part) == 2:
360+
return '', sep * 2, stripped_part
361+
else:
362+
return '', sep, stripped_part
363+
else:
364+
return '', '', part
365+
348366
@classmethod
349367
def _parse_args(cls, args):
350368
# This is useful when you don't want to create an instance, just
@@ -724,12 +742,12 @@ def parents(self):
724742
def is_absolute(self):
725743
"""True if the path is absolute (has both a root and, if applicable,
726744
a drive)."""
727-
raise NotImplementedError
745+
return bool(self._root)
728746

729747
def is_reserved(self):
730748
"""Return True if the path contains one of the special names reserved
731749
by the system, if any."""
732-
raise NotImplementedError
750+
return False
733751

734752
def match(self, path_pattern):
735753
"""
@@ -768,32 +786,8 @@ class PurePosixPath(PurePath):
768786
However, you can also instantiate it directly on any system.
769787
"""
770788
_pathmod = posixpath
771-
_case_insensitive = False
772789
__slots__ = ()
773790

774-
@classmethod
775-
def _splitroot(cls, part):
776-
sep = cls._pathmod.sep
777-
if part and part[0] == sep:
778-
stripped_part = part.lstrip(sep)
779-
# According to POSIX path resolution:
780-
# http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11
781-
# "A pathname that begins with two successive slashes may be
782-
# interpreted in an implementation-defined manner, although more
783-
# than two leading slashes shall be treated as a single slash".
784-
if len(part) - len(stripped_part) == 2:
785-
return '', sep * 2, stripped_part
786-
else:
787-
return '', sep, stripped_part
788-
else:
789-
return '', '', part
790-
791-
def is_absolute(self):
792-
return bool(self._root)
793-
794-
def is_reserved(self):
795-
return False
796-
797791
def as_uri(self):
798792
# We represent the path using the local filesystem encoding,
799793
# for portability to other applications.

0 commit comments

Comments
 (0)