Skip to content

Commit 5c7bd0e

Browse files
authored
GH-113528: Deoptimise pathlib._abc.PurePathBase.parts (#113883)
Implement `parts` using `_stack`, which itself calls `pathmod.split()` repeatedly. This avoids use of `_tail`, which will be moved to `PurePath` shortly.
1 parent 623b338 commit 5c7bd0e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Lib/pathlib/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ def __ge__(self, other):
195195
return NotImplemented
196196
return self._parts_normcase >= other._parts_normcase
197197

198+
@property
199+
def parts(self):
200+
"""An object providing sequence-like access to the
201+
components in the filesystem path."""
202+
if self.drive or self.root:
203+
return (self.drive + self.root,) + tuple(self._tail)
204+
else:
205+
return tuple(self._tail)
206+
198207
@property
199208
def parent(self):
200209
"""The logical parent of the path."""

Lib/pathlib/_abc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ def is_relative_to(self, other):
381381
def parts(self):
382382
"""An object providing sequence-like access to the
383383
components in the filesystem path."""
384-
if self.drive or self.root:
385-
return (self.drive + self.root,) + tuple(self._tail)
386-
else:
387-
return tuple(self._tail)
384+
anchor, parts = self._stack
385+
if anchor:
386+
parts.append(anchor)
387+
return tuple(reversed(parts))
388388

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

0 commit comments

Comments
 (0)