From 7e754361bb9d709ab4ad342d170300c2cc0962a7 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 28 Dec 2023 01:43:03 +0000 Subject: [PATCH] GH-113528: Deoptimise `pathlib._abc.PurePathBase.with_name()` 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 | 19 +++++++++++++++++++ Lib/pathlib/_abc.py | 13 ++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index ab87b49d0277f3..19ac6a73aed51f 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -164,6 +164,25 @@ def __ge__(self, other): 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 as_uri(self): """Return the path as a URI.""" if not self.is_absolute(): diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index efe56ec565c162..115dddff624e94 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -315,10 +315,10 @@ def anchor(self): @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): @@ -362,11 +362,10 @@ def with_name(self, name): 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."""