]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-130614: pathlib ABCs: retain original separator in `with_name()` (#130990)
authorBarney Gale <barney.gale@gmail.com>
Sun, 9 Mar 2025 16:36:20 +0000 (16:36 +0000)
committerGitHub <noreply@github.com>
Sun, 9 Mar 2025 16:36:20 +0000 (16:36 +0000)
In `pathlib.types._JoinablePath.with_name()`, retain any alternative path
separator preceding the old name, rather stripping and replacing it with a
primary separator. As a result, this method changes _only_ the name.

Lib/pathlib/types.py

index cfdbc89644a24a023c99138d7dc4d33e7e2e0be5..c65e4e3ede90b1b17f541e76f9ce7a0c61faa5d5 100644 (file)
@@ -135,7 +135,9 @@ class _JoinablePath(ABC):
         split = self.parser.split
         if split(name)[0]:
             raise ValueError(f"Invalid name {name!r}")
-        return self.with_segments(split(str(self))[0], name)
+        path = str(self)
+        path = path.removesuffix(split(path)[1]) + name
+        return self.with_segments(path)
 
     def with_stem(self, stem):
         """Return a new path with the stem changed."""