From 372a37872e6cde0517b3602fba342c5336d6b172 Mon Sep 17 00:00:00 2001 From: Sam Stephens Date: Sun, 8 Dec 2024 22:58:46 -0500 Subject: [PATCH 1/2] fixed colon handling for windows urls --- Lib/nturl2path.py | 9 +++++++-- Lib/test/test_urllib.py | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py index 7e13ae3128333d..5f487598e8aff6 100644 --- a/Lib/nturl2path.py +++ b/Lib/nturl2path.py @@ -26,8 +26,13 @@ def url2pathname(url): # Skip past extra slash before UNC drive in URL path. url = url[1:] # Windows itself uses ":" even in URLs. - url = url.replace(':', '|') - if not '|' in url: + url = url.replace(':', '|', 1) + if not '|' in url or ('|' in url and not ( + url.startswith('|') or + '/|' in url or + (len(url) > 2 and url[0] == '/' and url[2] == '|' and url[1].isalpha())) + ): + url = url.replace('|', ':', 1) # No drive specifier, just convert slashes # make sure not to convert quoted slashes :-) return urllib.parse.unquote(url.replace('/', '\\')) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 042d3b35b77022..f72b95b6133611 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1509,6 +1509,9 @@ def test_url2pathname_win(self): # Percent-encoded forward slashes are preserved for backwards compatibility self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar') self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar') + # Colon in filenames, both with drive specifier and without + self.assertEqual(fn('//host/path/spam.foo:bar'), '\\\\host\\path\\spam.foo:bar') + self.assertEqual(fn('///c:/path/spam.foo:bar'), 'c:\\path\\spam.foo:bar') # Round-tripping paths = ['C:', r'\C\test\\', From 69a8e01d2792f595b5b149fc6c20439160438f24 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 9 Dec 2024 04:02:42 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-12-09-04-02-41.gh-issue-126367.OsTibm.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-09-04-02-41.gh-issue-126367.OsTibm.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-09-04-02-41.gh-issue-126367.OsTibm.rst b/Misc/NEWS.d/next/Library/2024-12-09-04-02-41.gh-issue-126367.OsTibm.rst new file mode 100644 index 00000000000000..1bf8e71d83c3f2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-09-04-02-41.gh-issue-126367.OsTibm.rst @@ -0,0 +1 @@ +Modified the :func:`~urllib.request.url2pathname` function in :mod:`urllib.request` to correctly handle DOS drive paths in URLs on Windows. Previously, the function produced incorrect results for UNC paths and raised OS errors for some paths.