8000 [3.12] GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator… · python/cpython@305d78b · GitHub
[go: up one dir, main page]

Skip to content

Commit 305d78b

Browse files
miss-islingtonbarneygalegpshead
authored
[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 328422c) Co-authored-by: Barney Gale <barney.gale@gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent eca102d commit 305d78b

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/pathlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ def __init__(self, *args):
300300
for arg in args:
301301
if isinstance(arg, PurePath):
302302
path = arg._raw_path
303+
if arg._flavour is ntpath and self._flavour is posixpath:
304+
# GH-103631: Convert separators for backwards compatibility.
305+
path = path.replace('\\', '/')
303306
else:
304307
try:
305308
path = os.fspath(arg)

Lib/test/test_pathlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,12 @@ def test_div(self):
789789
pp = P('//a') / '/c'
790790
self.assertEqual(pp, P('/c'))
791791

792+
def test_parse_windows_path(self):
793+
P = self.cls
794+
p = P('c:', 'a', 'b')
795+
pp = P(pathlib.PureWindowsPath('c:\\a\\b'))
796+
self.assertEqual(p, pp)
797+
792798

793799
class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
794800
cls = pathlib.PureWindowsPath
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` not converting
2+
path separators to restore 3.11 compatible behavior.

0 commit comments

Comments
 (0)
0