From 41719acc7c934f52d51c0b1b547db6bbb5c72a75 Mon Sep 17 00:00:00 2001 From: vinit Date: Sat, 29 Aug 2026 11:52:58 +0530 Subject: [PATCH 1/3] gh-156510: Fix urlretrieve() file URL path on Windows --- Lib/test/test_urllib.py | 11 +++++++++++ Lib/urllib/request.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index ab59727a8fd182..350cf4a73366c0 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -761,6 +761,17 @@ def hooktester(block_count, block_read_size, file_size, _report=report): self.assertEqual(report[1][1], 8192) self.assertEqual(report[2][1], 8192) + def test_file_url_no_filename(self): + # GH-156510: urlretrieve() with a file:// URL and no filename should + # return a valid local path, not a URL-formatted path with leading + # slashes that normpath() mishandles on Windows. + url = self.constructLocalFileUrl(os_helper.TESTFN) + filename, headers = urllib.request.urlretrieve(url) + self.assertEqual( + filename, + urllib.request.url2pathname(url, require_scheme=True, resolve_host=True), + ) + class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin): """Test urllib.urlretrieve() using fake http connections""" diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 9fa92659a255ed..3d09370348432d 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -215,7 +215,7 @@ def urlretrieve(url, filename=None, reporthook=None, data=None): # Just return the local path and the "headers" for file:// # URLs. No sense in performing a copy unless requested. if url_type == "file" and not filename: - return os.path.normpath(path), headers + return url2pathname(url, require_scheme=True, resolve_host=True), headers # Handle temporary file setup. if filename: From 92347fb6d0f6441cd7f9c6afc0bddba08f1117b8 Mon Sep 17 00:00:00 2001 From: vinit Date: Sat, 29 Aug 2026 11:59:53 +0530 Subject: [PATCH 2/3] gh-156510: Add NEWS entry --- Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst b/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst new file mode 100644 index 00000000000000..9aeaa8350273af --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst @@ -0,0 +1 @@ +Fix urllib.request.urlretrieve() with a local file:// URL and no explicit filename on Windows. From 832f428a41901241833685035888b057a474c96f Mon Sep 17 00:00:00 2001 From: vinit Date: Sat, 29 Aug 2026 12:01:52 +0530 Subject: [PATCH 3/3] gh-156510: Fix NEWS entry filename --- ...-156510.rst => 2026-08-29-11-58-00.gh-issue-156510.abCDef.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2026-08-29-11-58-00.gh-issue-156510.rst => 2026-08-29-11-58-00.gh-issue-156510.abCDef.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst b/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.abCDef.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.rst rename to Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.abCDef.rst