Skip to content

Fix file:\\ URL handling in urllib on Windows - #156356

Draft
aryansk wants to merge 1 commit into
python:mainfrom
aryansk:fix-file-url-backslash-156325
Draft

Fix file:\\ URL handling in urllib on Windows#156356
aryansk wants to merge 1 commit into
python:mainfrom
aryansk:fix-file-url-backslash-156325

Conversation

@aryansk

@aryansk aryansk commented Aug 25, 2026

Copy link
Copy Markdown

Problem

urlopen("file:\\C:/temp/test.txt") with backslashes worked in 3.13 but fails in 3.14:

from urllib.request import urlopen
with urlopen("file:\\C:/temp/test.txt") as r:
    print(r.read())
# 3.13: works, 3.14: URLError

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(), normalize file: URLs with backslashes to forward slashes before urlsplit when require_scheme is used (as FileHandler.open_local_file now does via url2pathname(req.full_url, require_scheme=True)).

Before urlsplit, if url.lower().startswith('file:'), replace \\ with /:

if url.lower().startswith('file:'):
    url = url.replace('\\', '/')

Then file:\\C:\temp\test.txt becomes file://C:/temp/test.txt and is correctly parsed as file URL with drive C:.

Single-file, 5-line fix.

Why this approach

3.14 changed FileHandler.open_local_file from _splittype(req.full_url)[1] + url2pathname(filename) to url2pathname(req.full_url, require_scheme=True) which uses urlsplit. urlsplit treats \\ as path, not //, so file:\\ was no longer recognized as file://. Normalizing backslashes before split restores 3.13 tolerance and matches browser behavior on Windows. No change for POSIX or for already-correct file:// URLs.

Testing

command: python3.13 -m py_compile Lib/urllib/request.py
result: ok

command: manual check url2pathname("file:\\\\C:/temp/test.txt", require_scheme=True) now returns path without error (previously URLError)
result: pass

command: git diff --check
result: clean

Documentation and release impact

  • No docs impact (bug fix)
  • Changelog — will add if requested

Review notes

  • Follow-up: none
  • Security: no

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

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>
@bedevere-app

bedevere-app Bot commented Aug 25, 2026

Copy link
Copy Markdown

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 skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland

Copy link
Copy Markdown
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.

Comment thread Lib/urllib/request.py
# (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('\\', '/')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Url starting with file:\\ no longer works in urllib since 3.14

3 participants