Skip to content

Commit cb8e599

Browse files
barneygalepicnixz
andauthored
GH-125069: Fix inconsistent joining in WindowsPath(PosixPath(...)) (#125156)
`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. Co-authored-by: Bénédikt Tran <[email protected]>
1 parent c6d7b64 commit cb8e599

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
@@ -119,9 +119,9 @@ def __init__(self, *args):
119119
paths = []
120120
for arg in args:
121121
if isinstance(arg, PurePath):
122-
if arg.parser is ntpath and self.parser is posixpath:
122+
if arg.parser is not self.parser:
123123
# GH-103631: Convert separators for backwards compatibility.
124-
paths.extend(path.replace('\\', '/') for path in arg._raw_paths)
124+
paths.append(arg.as_posix())
125125
else:
126126
paths.extend(arg._raw_paths)
127127
else:

Lib/test/test_pathlib/test_pathlib.py

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

134+
@needs_windows
135+
def test_constructor_nested_foreign_flavour(self):
136+
# See GH-125069.
137+
p1 = pathlib.PurePosixPath('b/c:\\d')
138+
p2 = pathlib.PurePosixPath('b/', 'c:\\d')
139+
self.assertEqual(p1, p2)
140+
self.assertEqual(self.cls(p1), self.cls('b/c:/d'))
141+
self.assertEqual(self.cls(p2), self.cls('b/c:/d'))
142+
134143
def _check_parse_path(self, raw_path, *expected):
135144
sep = self.parser.sep
136145
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)