From 6d90005456448f098ee90fa6bc19c21ecdeeac1a Mon Sep 17 00:00:00 2001 From: barneygale Date: Tue, 8 Oct 2024 21:13:36 +0100 Subject: [PATCH 1/4] GH-125069: Fix inconsistent joining in `WindowsPath(PosixPath(...))` `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. --- Lib/pathlib/_local.py | 8 +++++--- Lib/test/test_pathlib/test_pathlib.py | 9 +++++++++ .../2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 1c02e4168d3a9e..59d5df817e0de5 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -119,11 +119,13 @@ 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 self.parser: + paths.extend(arg._raw_paths) + elif arg.parser is ntpath: # GH-103631: Convert separators for backwards compatibility. - paths.extend(path.replace('\\', '/') for path in arg._raw_paths) + paths.append(str(arg).replace('\\', '/')) else: - paths.extend(arg._raw_paths) + paths.append(str(arg)) else: try: path = os.fspath(arg) diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index b47b4a194cfaa9..c7104bfda90f6c 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -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)) diff --git a/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst b/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst new file mode 100644 index 00000000000000..5dfaba5f4dac77 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst @@ -0,0 +1,4 @@ +Fix 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. From e0799ae912dc5444fb4438164671d55a0f3ac8a6 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Fri, 11 Oct 2024 19:18:38 +0100 Subject: [PATCH 2/4] Update Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst b/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst index 5dfaba5f4dac77..9f1fd871e1d0b5 100644 --- a/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst +++ b/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst @@ -1,4 +1,4 @@ -Fix issue where providing a :class:`pathlib.PurePath` object as an +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. From 39641afeece1c97fcaaa034fb6c9444152aecad7 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 13 Oct 2024 18:16:47 +0100 Subject: [PATCH 3/4] May as well use as_posix() --- Lib/pathlib/_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 59d5df817e0de5..a0808133fa1151 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -123,7 +123,7 @@ def __init__(self, *args): paths.extend(arg._raw_paths) elif arg.parser is ntpath: # GH-103631: Convert separators for backwards compatibility. - paths.append(str(arg).replace('\\', '/')) + paths.append(arg.as_posix()) else: paths.append(str(arg)) else: From fe68d84ce6949f936dc523ee29848c8913b51fe1 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 13 Oct 2024 18:19:15 +0100 Subject: [PATCH 4/4] Simplify --- Lib/pathlib/_local.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index a0808133fa1151..a78997179820b1 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -119,13 +119,11 @@ def __init__(self, *args): paths = [] for arg in args: if isinstance(arg, PurePath): - if arg.parser is self.parser: - paths.extend(arg._raw_paths) - elif arg.parser is ntpath: + if arg.parser is not self.parser: # GH-103631: Convert separators for backwards compatibility. paths.append(arg.as_posix()) else: - paths.append(str(arg)) + paths.extend(arg._raw_paths) else: try: path = os.fspath(arg)