From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:12:57 +0000 (+0200) Subject: [3.13] GH-125069: Fix inconsistent joining in `WindowsPath(PosixPath(...))` (GH-12515... X-Git-Tag: v3.13.1~310 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=094d95f62c12d2773c9bf116d449e31ede2681b1;p=thirdparty%2FPython%2Fcpython.git [3.13] GH-125069: Fix inconsistent joining in `WindowsPath(PosixPath(...))` (GH-125156) (#125409) `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 cb8e5995d89d9b90e83cf43310ec50e177484e70) Co-authored-by: Barney Gale Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index fbc8db78b86e..0188e7c7722b 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -118,9 +118,9 @@ class PurePath(PurePathBase): 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: diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index ff054e76efcd..093746c0c150 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -107,6 +107,15 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest): 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 000000000000..9f1fd871e1d0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-08-21-17-16.gh-issue-125069.0RP0Mx.rst @@ -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.