Skip to content

Commit

Permalink
pythonGH-113528: Deoptimise pathlib._abc.PurePathBase.parts (python…
Browse files Browse the repository at this point in the history
…#113883)

Implement `parts` using `_stack`, which itself calls `pathmod.split()`
repeatedly. This avoids use of `_tail`, which will be moved to `PurePath`
shortly.
  • Loading branch information
barneygale authored and Glyphack committed Jan 27, 2024
1 parent 0fed9e7 commit 6d9d616
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ def __ge__(self, other):
return NotImplemented
return self._parts_normcase >= other._parts_normcase

@property
def parts(self):
"""An object providing sequence-like access to the
components in the filesystem path."""
if self.drive or self.root:
return (self.drive + self.root,) + tuple(self._tail)
else:
return tuple(self._tail)

@property
def parent(self):
"""The logical parent of the path."""
Expand Down
8 changes: 4 additions & 4 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ def is_relative_to(self, other):
def parts(self):
"""An object providing sequence-like access to the
components in the filesystem path."""
if self.drive or self.root:
return (self.drive + self.root,) + tuple(self._tail)
else:
return tuple(self._tail)
anchor, parts = self._stack
if anchor:
parts.append(anchor)
return tuple(reversed(parts))

def joinpath(self, *pathsegments):
"""Combine this path with one or several arguments, and return a
Expand Down

0 comments on commit 6d9d616

Please sign in to comment.