Skip to content

Commit

Permalink
[3.12] pythonGH-125069: Fix inconsistent joining in `WindowsPath(Posi…
Browse files Browse the repository at this point in the history
…xPath(...))` (pythonGH-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 <10796600+picnixz@users.noreply.github.com>.
(cherry picked from commit cb8e599)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
  • Loading branch information
barneygale committed Oct 13, 2024
1 parent 243a8a9 commit ea2eafe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ def __init__(self, *args):
paths = []
for arg in args:
if isinstance(arg, PurePath):
if arg._flavour is ntpath and self._flavour is posixpath:
if arg._flavour is not self._flavour:
# GH-103631: Convert separators for backwards compatibility.
paths.extend(path.replace('\\', '/') for path in arg._raw_paths)
paths.append(arg.as_posix())
else:
paths.extend(arg._raw_paths)
else:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,14 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
],
})

def test_constructor_nested_foreign_flavour(self):
# See GH-125069.
p1 = pathlib.PurePosixPath('b/c:\\d')
p2 = pathlib.PurePosixPath('b/', 'c:\\d')
self.assertEqual(p1, p2)
self.assertEqual(self.cls(p1), self.cls('b/c:/d'))
self.assertEqual(self.cls(p2), self.cls('b/c:/d'))

def test_drive_root_parts(self):
check = self._check_drive_root_parts
# First part is anchored.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix an issue where providing a :class:`pathlib.PurePath` object as an
initializer argument to a second :class:`~pathlib.PurePath` object with a
different :attr:`~pathlib.PurePath.parser` resulted in arguments to the
former object's initializer being joined by the latter object's parser.

0 comments on commit ea2eafe

Please sign in to comment.