Skip to content

Commit 094d95f

Browse files
miss-islingtonbarneygalepicnixz
authored
[3.13] GH-125069: Fix inconsistent joining in WindowsPath(PosixPath(...)) (GH-125156) (#125409)
`PurePath.__init__()` incorrectly uses the `_raw_paths` of a given `PurePath` object with a different flavour, even though the procedure to join path segments can differ between flavours. This change makes the `_raw_paths`-enabled deferred joining apply _only_ when the path flavours match. (cherry picked from commit cb8e599) Co-authored-by: Barney Gale <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent cff627a commit 094d95f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/pathlib/_local.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def __init__(self, *args):
118118
paths = []
119119
for arg in args:
120120
if isinstance(arg, PurePath):
121-
if arg.parser is ntpath and self.parser is posixpath:
121+
if arg.parser is not self.parser:
122122
# GH-103631: Convert separators for backwards compatibility.
123-
paths.extend(path.replace('\\', '/') for path in arg._raw_paths)
123+
paths.append(arg.as_posix())
124124
else:
125125
paths.extend(arg._raw_paths)
126126
else:

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ def test_constructor_nested(self):
107107
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
108108
self.assertEqual(P(P('./a:b')), P('./a:b'))
109109

110+
@needs_windows
111+
def test_constructor_nested_foreign_flavour(self):
112+
# See GH-125069.
113+
p1 = pathlib.PurePosixPath('b/c:\\d')
114+
p2 = pathlib.PurePosixPath('b/', 'c:\\d')
115+
self.assertEqual(p1, p2)
116+
self.assertEqual(self.cls(p1), self.cls('b/c:/d'))
117+
self.assertEqual(self.cls(p2), self.cls('b/c:/d'))
118+
110119
def _check_parse_path(self, raw_path, *expected):
111120
sep = self.parser.sep
112121
actual = self.cls._parse_path(raw_path.replace('/', sep))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix an issue where providing a :class:`pathlib.PurePath` object as an
2+
initializer argument to a second :class:`~pathlib.PurePath` object with a
3+
different :attr:`~pathlib.PurePath.parser` resulted in arguments to the
4+
former object's initializer being joined by the latter object's parser.

0 commit comments

Comments
 (0)