Skip to content

Commit f49221a

Browse files
barneygalepicnixz
andauthored
[3.12] GH-125069: Fix inconsistent joining in WindowsPath(PosixPath(...)) (GH-125156) (#125410)
`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: Bénédikt Tran <[email protected]>
1 parent 243a8a9 commit f49221a

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ def __init__(self, *args):
359359
paths = []
360360
for arg in args:
361361
if isinstance(arg, PurePath):
362-
if arg._flavour is ntpath and self._flavour is posixpath:
362+
if arg._flavour is not self._flavour:
363363
# GH-103631: Convert separators for backwards compatibility.
364-
paths.extend(path.replace('\\', '/') for path in arg._raw_paths)
364+
paths.append(arg.as_posix())
365365
else:
366366
paths.extend(arg._raw_paths)
367367
else:

Lib/test/test_pathlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,14 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
831831
],
832832
})
833833

834+
def test_constructor_nested_foreign_flavour(self):
835+
# See GH-125069.
836+
p1 = pathlib.PurePosixPath('b/c:\\d')
837+
p2 = pathlib.PurePosixPath('b/', 'c:\\d')
838+
self.assertEqual(p1, p2)
839+
self.assertEqual(self.cls(p1), self.cls('b/c:/d'))
840+
self.assertEqual(self.cls(p2), self.cls('b/c:/d'))
841+
834842
def test_drive_root_parts(self):
835843
check = self._check_drive_root_parts
836844
# First part is anchored.
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 flavour resulted in arguments to the former object's initializer
4+
being joined by the latter object's flavour.

0 commit comments

Comments
 (0)