]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-113528: Deoptimise `pathlib._abc.PurePathBase.parts` (#113883)
authorBarney Gale <barney.gale@gmail.com>
Tue, 9 Jan 2024 22:46:50 +0000 (22:46 +0000)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 22:46:50 +0000 (22:46 +0000)
Implement `parts` using `_stack`, which itself calls `pathmod.split()`
repeatedly. This avoids use of `_tail`, which will be moved to `PurePath`
shortly.

Lib/pathlib/__init__.py
Lib/pathlib/_abc.py

index 749c68d2999bdc40d8ef25b1f21c0d9e508064c2..26e14b3f7b20051837ba5f5c76d0b8e04ac7d644 100644 (file)
@@ -195,6 +195,15 @@ class PurePath(_abc.PurePathBase):
             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."""
index caa84fc40559f7aef035afc50785df60817cad9d..c16beca71aa7c7cc2bca1b608041297615807901 100644 (file)
@@ -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