Skip to content

Commit

Permalink
Simplify Path construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed May 15, 2021
1 parent 0897d9d commit 4e918e4
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def __new__(cls, *args):
def __reduce__(self):
# Using the parts tuple helps share interned path parts
# when pickling related paths.
return (self.__class__, tuple(self._parts))
return (type(self), tuple(self._parts))

@classmethod
def _parse_args(cls, args):
Expand Down Expand Up @@ -398,10 +398,8 @@ def _parse_parts(cls, parts):

@classmethod
def _from_parts(cls, args):
# We need to call _parse_args on the instance, so as to get the
# right flavour.
drv, root, parts = cls._parse_args(args)
self = object.__new__(cls)
drv, root, parts = self._parse_args(args)
self._drv = drv
self._root = root
self._parts = parts
Expand All @@ -428,7 +426,8 @@ def _make_child(self, args):
self._drv, self._root, self._parts, drv, root, parts)
return self._from_parsed_parts(drv, root, parts)

def _join_parsed_parts(self, drv, root, parts, drv2, root2, parts2):
@classmethod
def _join_parsed_parts(cls, drv, root, parts, drv2, root2, parts2):
"""
Join the two paths represented by the respective
(drive, root, parts) tuples. Return a new (drive, root, parts) tuple.
Expand All @@ -437,7 +436,7 @@ def _join_parsed_parts(self, drv, root, parts, drv2, root2, parts2):
if not drv2 and drv:
return drv, root2, [drv + root2] + parts2[1:]
elif drv2:
if drv2 == drv or self._casefold(drv2) == self._casefold(drv):
if drv2 == drv or cls._casefold(drv2) == cls._casefold(drv):
# Same drive => second path is relative to the first
return drv, root, parts + parts2[1:]
else:
Expand Down Expand Up @@ -481,7 +480,7 @@ def __bytes__(self):
return os.fsencode(self)

def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
return "{}({!r})".format(type(self).__name__, self.as_posix())

def as_uri(self):
"""Return the path as a 'file' URI."""
Expand Down Expand Up @@ -1114,7 +1113,7 @@ def write_text(self, data, encoding=None, errors=None, newline=None):
"""
if not isinstance(data, str):
raise TypeError('data must be str, not %s' %
data.__class__.__name__)
type(data).__name__)
encoding = io.text_encoding(encoding)
with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:
return f.write(data)
Expand Down Expand Up @@ -1197,7 +1196,7 @@ def rename(self, target):
Returns the new Path instance pointing to the target path.
"""
self._accessor.rename(self, target)
return self.__class__(target)
return self._from_parts((target,))

def replace(self, target):
"""
Expand All @@ -1210,7 +1209,7 @@ def replace(self, target):
Returns the new Path instance pointing to the target path.
"""
self._accessor.replace(self, target)
return self.__class__(target)
return self._from_parts((target,))

def symlink_to(self, target, target_is_directory=False):
"""
Expand Down

0 comments on commit 4e918e4

Please sign in to comment.