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: diff --git a/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.abCDef.rst b/Misc/NEWS.d/next/Library/2026-08-29-11-58-00.gh-issue-156510.abCDef.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.abCDef.rst @@ -0,0 +1 @@ +Fix urllib.request.urlretrieve() with a local file:// URL and no explicit filename on Windows.