diff options
author | Barney Gale <barney.gale@gmail.com> | 2024-01-09 22:46:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-09 22:46:50 (GMT) |
commit | 5c7bd0e39839b27bc524e1790fe4936d987f384a (patch) | |
tree | 45c3dfcb0deac02d44a3c935ac5c7a3ca08101fd /Lib/pathlib | |
parent | 623b338adf2645b09c546e7a17f2648d3a900620 (diff) | |
download | cpython-5c7bd0e39839b27bc524e1790fe4936d987f384a.zip cpython-5c7bd0e39839b27bc524e1790fe4936d987f384a.tar.gz cpython-5c7bd0e39839b27bc524e1790fe4936d987f384a.tar.bz2 |
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.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r-- | Lib/pathlib/__init__.py | 9 | ||||
-rw-r--r-- | Lib/pathlib/_abc.py | 8 |
2 files changed, 13 insertions, 4 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 749c68d..26e14b3 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -196,6 +196,15 @@ class PurePath(_abc.PurePathBase): 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.""" drv = self.drive diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index caa84fc..c16beca 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -381,10 +381,10 @@ class PurePathBase: 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 |