From ff3dc92e62165a5ce943fd4ad377e8634b673891 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 24 Jul 2025 00:40:23 +0100 Subject: [PATCH 1/2] GH-137059: `url2pathname()`: fix support for drive letter in netloc Support file URLs like `file://c:/foo` in `urllib.request.url2pathname()` on Windows. This restores behaviour from 3.13. --- Lib/test/test_urllib.py | 2 ++ Lib/urllib/request.py | 5 ++++- .../Library/2025-07-24-00-38-07.gh-issue-137059.fr64oW.rst | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-07-24-00-38-07.gh-issue-137059.fr64oW.rst diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index c30fb5e27eea8a..d0567352c9111a 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1590,6 +1590,8 @@ def test_url2pathname_resolve_host(self): def test_url2pathname_win(self): fn = urllib.request.url2pathname self.assertEqual(fn('/C:/'), 'C:\\') + self.assertEqual(fn('//C:/'), 'C:\\') + self.assertEqual(fn('//C:\\'), 'C:\\') self.assertEqual(fn("///C|"), 'C:') self.assertEqual(fn("///C:"), 'C:') self.assertEqual(fn('///C:/'), 'C:\\') diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index c1c373d08815c1..af93d4cd75dbef 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1660,7 +1660,10 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False): if scheme != 'file': raise URLError("URL is missing a 'file:' scheme") if os.name == 'nt': - if not _is_local_authority(authority, resolve_host): + if authority[1:2] == ':': + # e.g. file://c:/file.txt + url = authority + url + elif not _is_local_authority(authority, resolve_host): # e.g. file://server/share/file.txt url = '//' + authority + url elif url[:3] == '///': diff --git a/Misc/NEWS.d/next/Library/2025-07-24-00-38-07.gh-issue-137059.fr64oW.rst b/Misc/NEWS.d/next/Library/2025-07-24-00-38-07.gh-issue-137059.fr64oW.rst new file mode 100644 index 00000000000000..8c63c1f1af8bf5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-24-00-38-07.gh-issue-137059.fr64oW.rst @@ -0,0 +1,3 @@ +Fix handling of file URLs with a Windows drive letter in the URL authority +by :func:`urllib.request.url2pathname`. This fixes a regression in earlier +pre-releases of Python 3.14. From 0b2f5a76ab849eeb8ff1e6c72c164f7ef9fcea87 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 27 Jul 2025 12:18:33 +0100 Subject: [PATCH 2/2] Test cases suggested by Serhiy --- Lib/test/test_urllib.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index d0567352c9111a..ae524c5ffba6b1 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1590,8 +1590,10 @@ def test_url2pathname_resolve_host(self): def test_url2pathname_win(self): fn = urllib.request.url2pathname self.assertEqual(fn('/C:/'), 'C:\\') + self.assertEqual(fn('//C:'), 'C:') self.assertEqual(fn('//C:/'), 'C:\\') self.assertEqual(fn('//C:\\'), 'C:\\') + self.assertEqual(fn('//C:80/'), 'C:80\\') self.assertEqual(fn("///C|"), 'C:') self.assertEqual(fn("///C:"), 'C:') self.assertEqual(fn('///C:/'), 'C:\\')