Fix file:\\ URL handling in urllib on Windows - #156356
Draft
aryansk wants to merge 1 commit into
Draft
Conversation
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#156325 Co-authored-by: Muse Spark <muse-spark@users.noreply.github.com> 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>
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Member
|
@aryansk this is the third PR you have opened as a draft, and you've yet to sign the CLA? Until you sign it, we can't review. |
2 tasks
edvilme
reviewed
Aug 25, 2026
| # (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('\\', '/') |
Contributor
There was a problem hiding this comment.
I think it would be better to just replace file:\\ with file:// on Windows only to avoid accepting invalid uris like file://Users/edvilme\somepath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
urlopen("file:\\C:/temp/test.txt")with backslashes worked in 3.13 but fails in 3.14:Same URL works in
webbrowser, Firefox, Edge on Windows 11. Regression when upgrading 3.13→3.14.Fixes #156325
Change
In
Lib/urllib/request.py:url2pathname(), normalizefile:URLs with backslashes to forward slashes beforeurlsplitwhenrequire_schemeis used (asFileHandler.open_local_filenow does viaurl2pathname(req.full_url, require_scheme=True)).Before
urlsplit, ifurl.lower().startswith('file:'), replace\\with/:Then
file:\\C:\temp\test.txtbecomesfile://C:/temp/test.txtand is correctly parsed asfileURL with driveC:.Single-file, 5-line fix.
Why this approach
3.14 changed
FileHandler.open_local_filefrom_splittype(req.full_url)[1]+url2pathname(filename)tourl2pathname(req.full_url, require_scheme=True)which usesurlsplit.urlsplittreats\\as path, not//, sofile:\\was no longer recognized asfile://. Normalizing backslashes before split restores 3.13 tolerance and matches browser behavior on Windows. No change for POSIX or for already-correctfile://URLs.Testing
Documentation and release impact
Review notes
AI disclosure
Muse Spark assisted in analysis and fix drafting; all changes reviewed and tested manually. Co-authored-by trailers included for Pair Extraordinaire.
Co-authored-by: Muse Spark muse-spark@users.noreply.github.com
Co-authored-by: Aryan Singh K 70511529+aryansk@users.noreply.github.com