From 4328338e98100ecd69ac22460c310c0837535cf8 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 25 May 2023 23:39:12 +0100 Subject: [PATCH 1/2] GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling For backwards compatibility, accept backslashes as path separators in `PurePosixPath` if an instance of `PureWindowsPath` is supplied. --- Lib/pathlib.py | 3 +++ Lib/test/test_pathlib.py | 6 ++++++ .../Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst | 2 ++ 3 files changed, 11 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index fb78939dcc31ba..789402116be518 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 8b68cdc9b7d003..16f01445b7f48e 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 00000000000000..036ed54bc3e451 --- /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. From 6a612f4187620358fef9f6c99f0017a1c9dcbe2a Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 26 May 2023 10:38:35 -0700 Subject: [PATCH 2/2] mention this matches 3.11 in the news entry --- .../next/Library/2023-05-25-23-34-54.gh-issue-103631.x5Urye.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 036ed54bc3e451..d1eb2d3ed6191f 100644 --- 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 @@ -1,2 +1,2 @@ Fix ``pathlib.PurePosixPath(pathlib.PureWindowsPath(...))`` not converting -path separators. +path separators to restore 3.11 compatible behavior.