Skip to content

Commit

Permalink
pythonGH-125069: Fix inconsistent joining in `WindowsPath(PosixPath(.…
Browse files Browse the repository at this point in the history
…..))` (python#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>
  • Loading branch information
barneygale and picnixz authored Oct 13, 2024
1 parent c6d7b64 commit cb8e599
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def __init__(self, *args):
paths = []
for arg in args:
if isinstance(arg, PurePath):
if arg.parser is ntpath and self.parser is posixpath:
if arg.parser is not self.parser:
# 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
9 changes: 9 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ def test_constructor_nested(self):
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
self.assertEqual(P(P('./a:b')), P('./a:b'))

@needs_windows
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 _check_parse_path(self, raw_path, *expected):
sep = self.parser.sep
actual = self.cls._parse_path(raw_path.replace('/', sep))
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 cb8e599

Please sign in to comment.