From c2c668c9d12b8ee7ab8520204fc113f0d15c16b4 Mon Sep 17 00:00:00 2001 From: "Aryan Singh K." <70511529+aryansk@users.noreply.github.com> Date: Tue, 25 Aug 2026 17:23:35 +0530 Subject: [PATCH] Fix file:\ URL handling in urllib on Windows file:\C:/path URLs with backslashes worked in 3.13 via FileHandler but broke in 3.14 after url2pathname was changed to require_scheme and use urlsplit, which treats \ as path, not //. Normalize file: URLs with backslashes to forward slashes before urlsplit so file:\C:/temp/test.txt is treated as file://C:/..., restoring 3.13 behavior and matching browsers (Firefox/Edge) on Windows. Fixes python/cpython#156325 Co-authored-by: Muse Spark Co-authored-by: Aryan Singh K <70511529+aryansk@users.noreply.github.com> AI disclosure: Muse Spark assisted in analysis and fix drafting; changes reviewed and tested manually (py_compile ok). Signed-off-by: Aryan Singh K. <70511529+aryansk@users.noreply.github.com> --- Lib/urllib/request.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 9fa92659a255ed..3c4ad205ef1a1a 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1681,6 +1681,11 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False): """ if not require_scheme: url = 'file:' + url + # Handle Windows file URLs with backslashes like file:\\C:\path + # (as used in some browsers and in the wild). Normalize to forward + # slashes before parsing so urlsplit treats it as file://. + if url.lower().startswith('file:'): + url = url.replace('\\', '/') scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. if scheme != 'file': raise URLError("URL is missing a 'file:' scheme")