]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-113528: Deoptimise `pathlib._abc.PurePathBase.name` (#113531)
authorBarney Gale <barney.gale@gmail.com>
Sat, 6 Jan 2024 20:50:25 +0000 (20:50 +0000)
committerGitHub <noreply@github.com>
Sat, 6 Jan 2024 20:50:25 +0000 (20:50 +0000)
Replace usage of `_from_parsed_parts()` with `with_segments()` in
`with_name()`, and take a similar approach in `name` for consistency's
sake.

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

index 115ccf78e3befeea012cb5b6eb709efa21fecc12..765a1425d0305a6c9b6e2b6d9e075fdeb4519b56 100644 (file)
@@ -166,6 +166,25 @@ class PurePath(_abc.PurePathBase):
             return NotImplemented
         return self._parts_normcase >= other._parts_normcase
 
+    @property
+    def name(self):
+        """The final path component, if any."""
+        tail = self._tail
+        if not tail:
+            return ''
+        return tail[-1]
+
+    def with_name(self, name):
+        """Return a new path with the file name changed."""
+        m = self.pathmod
+        if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.':
+            raise ValueError(f"Invalid name {name!r}")
+        tail = self._tail.copy()
+        if not tail:
+            raise ValueError(f"{self!r} has an empty name")
+        tail[-1] = name
+        return self._from_parsed_parts(self.drive, self.root, tail)
+
     def relative_to(self, other, /, *_deprecated, walk_up=False):
         """Return the relative path to another path identified by the passed
         arguments.  If the operation is not possible (because this is not
index b1204e88044a3ff1d46832970f3e97c80945c2d1..1ce371308827fda04362c2780650b93b72162c81 100644 (file)
@@ -313,10 +313,10 @@ class PurePathBase:
     @property
     def name(self):
         """The final path component, if any."""
-        tail = self._tail
-        if not tail:
+        path_str = str(self)
+        if not path_str or path_str == '.':
             return ''
-        return tail[-1]
+        return self.pathmod.basename(path_str)
 
     @property
     def suffix(self):
@@ -360,11 +360,10 @@ class PurePathBase:
         m = self.pathmod
         if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.':
             raise ValueError(f"Invalid name {name!r}")
-        tail = self._tail.copy()
-        if not tail:
+        parent, old_name = m.split(str(self))
+        if not old_name or old_name == '.':
             raise ValueError(f"{self!r} has an empty name")
-        tail[-1] = name
-        return self._from_parsed_parts(self.drive, self.root, tail)
+        return self.with_segments(parent, name)
 
     def with_stem(self, stem):
         """Return a new path with the stem changed."""