GH-136874: url2pathname(): discard query and fragment components (#136875)

In `urllib.request.url2pathname()`, ignore any query or fragment components
in the given URL.
This commit is contained in:
Barney Gale 2025-07-21 18:33:20 +01:00 committed by GitHub
parent 4b68289ca6
commit 80b2d60a51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 5 deletions

View file

@ -1654,11 +1654,11 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False):
The URL authority may be resolved with gethostbyname() if
*resolve_host* is set to true.
"""
if require_scheme:
scheme, url = _splittype(url)
if scheme != 'file':
raise URLError("URL is missing a 'file:' scheme")
authority, url = _splithost(url)
if not require_scheme:
url = 'file:' + url
scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment.
if scheme != 'file':
raise URLError("URL is missing a 'file:' scheme")
if os.name == 'nt':
if not _is_local_authority(authority, resolve_host):
# e.g. file://server/share/file.txt