]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling (GH-104949)
authorBarney Gale <barney.gale@gmail.com>
Fri, 26 May 2023 18:05:43 +0000 (19:05 +0100)
committerGitHub <noreply@github.com>
Fri, 26 May 2023 18:05:43 +0000 (18:05 +0000)
For backwards compatibility, accept backslashes as path separators in
`PurePosixPath` if an instance of `PureWindowsPath` is supplied.
This restores behaviour from Python 3.11.

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst [new file with mode: 0644]

index c8931687a3c6dceae9a4a6a574a0025a64a021c9..8cb5279d735a300c92a7aa6c11940f70c35c91f2 100644 (file)
@@ -300,6 +300,9 @@ class PurePath(os.PathLike):
         for arg in args:
             if isinstance(arg, PurePath):
                 path = arg._raw_path
+                if arg._flavour is ntpath and self._flavour is posixpath:
+                    # GH-103631: Convert separators for backwards compatibility.
+                    path = path.replace('\\', '/')
             else:
                 try:
                     path = os.fspath(arg)
index ef202b751e44e91db240e83005952411fdbbbe44..01615e20945600e412e23c21f4d5bcdc51273594 100644 (file)
@@ -789,6 +789,12 @@ class PurePosixPathTest(_BasePurePathTest, unittest.TestCase):
         pp = P('//a') / '/c'
         self.assertEqual(pp, P('/c'))
 
+    def test_parse_windows_path(self):
+        P = self.cls
+        p = P('c:', 'a', 'b')
+        pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
+        self.assertEqual(p, pp)
+
 
 class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
     cls = pathlib.PureWindowsPath
diff --git a/Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst b/Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst
new file mode 100644 (file)
index 0000000..d1eb2d3
--- /dev/null
@@ -0,0 +1,2 @@
+Fix ``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` not converting
+path separators to restore 3.11 compatible behavior.