diff options
author | Barney Gale <barney.gale@gmail.com> | 2024-01-06 20:50:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-06 20:50:25 (GMT) |
commit | 1e914ad89de707b8a69b03d7b36f5022f4c07c78 (patch) | |
tree | dcd3295578b634ff4e8756bec06c6b839fb9d90a /Lib/pathlib | |
parent | 2205510e7b32eb4c7bbe1982d7b5dfcbfb85cffe (diff) | |
download | cpython-1e914ad89de707b8a69b03d7b36f5022f4c07c78.zip cpython-1e914ad89de707b8a69b03d7b36f5022f4c07c78.tar.gz cpython-1e914ad89de707b8a69b03d7b36f5022f4c07c78.tar.bz2 |
GH-113528: Deoptimise `pathlib._abc.PurePathBase.name` (#113531)
Replace usage of `_from_parsed_parts()` with `with_segments()` in
`with_name()`, and take a similar approach in `name` for consistency's
sake.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r-- | Lib/pathlib/__init__.py | 19 | ||||
-rw-r--r-- | 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 115ccf7..765a142 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -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 diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index b1204e8..1ce3713 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -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.""" |