[Python-checkins] [3.12] GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling (GH-104949) (GH-104991)

barneygale webhook-mailer at python.org
Fri May 26 14:42:43 EDT 2023


https://github.com/python/cpython/commit/305d78b71481e309051b1b88f363805d8c0ad34a
commit: 305d78b71481e309051b1b88f363805d8c0ad34a
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: barneygale <barney.gale at gmail.com>
date: 2023-05-26T18:42:36Z
summary:

[3.12] GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling (GH-104949) (GH-104991)

For backwards compatibility, accept backslashes as path separators in
`PurePosixPath` if an instance of `PureWindowsPath` is supplied.
This restores behaviour from Python 3.11.

(cherry picked from commit 328422ce6162eb18735a2c0de12f8a696be97d0c)

Co-authored-by: Barney Gale <barney.gale at gmail.com>
Co-authored-by: Gregory P. Smith <greg at krypto.org>

files:
A Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index bfe26e1e1679..a42085e3a3f8 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -300,6 +300,9 @@ def __init__(self, *args):
         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)
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index bc2947ec95b3..bf4decf9f97a 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -789,6 +789,12 @@ def test_div(self):
         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
index 000000000000..d1eb2d3ed619
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst
@@ -0,0 +1,2 @@
+Fix ``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` not converting
+path separators to restore 3.11 compatible behavior.



More information about the Python-checkins mailing list