diff options
author | Barney Gale <barney.gale@gmail.com> | 2023-05-26 19:05:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 18:05:43 +0000 |
commit | 328422ce6162eb18735a2c0de12f8a696be97d0c (patch) | |
tree | 30e21f0cee1c74251f04fd1d9929a7c437d13f67 | |
parent | ad0be361c9922a918c7c3eaf83e1d8f2b019279c (diff) | |
download | cpython-328422ce6162eb18735a2c0de12f8a696be97d0c.tar.gz cpython-328422ce6162eb18735a2c0de12f8a696be97d0c.zip |
GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling (GH-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.
Co-authored-by: Gregory P. Smith <greg@krypto.org>
-rw-r--r-- | Lib/pathlib.py | 3 | ||||
-rw-r--r-- | Lib/test/test_pathlib.py | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst | 2 |
3 files changed, 11 insertions, 0 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py index c8931687a3c..8cb5279d735 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index ef202b751e4..01615e20945 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -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 index 00000000000..d1eb2d3ed61 --- /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. |