Skip to content

Commit

Permalink
pythonGH-103631: Fix PurePosixPath(PureWindowsPath(...)) separator …
Browse files Browse the repository at this point in the history
…handling (pythonGH-104949)

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>
  • Loading branch information
2 people authored and miss-islington committed May 26, 2023
1 parent bb1e57e commit e94c100
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lib/pathlib.py
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_pathlib.py
Expand Up @@ -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
Expand Down
@@ -0,0 +1,2 @@
Fix ``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` not converting
path separators to restore 3.11 compatible behavior.

0 comments on commit e94c100

Please sign in to comment.