Skip to content

urllib.request.urlopen('file:...').url incorrect value #127090

Closed
@barneygale

Description

@barneygale

Bug report

Bug description:

When a file: URL is given to urllib.request.urlopen(), it returns an addinfourl object. This object's url attribute (and its deprecated geturl() method) usually return incorrect results. For example:

>>> from urllib.request import urlopen
>>> urlopen('file:Doc/requirements.txt').url
'file://Doc/requirements.txt'   # expected: 'file:Doc/requirements.txt'
>>> urlopen('file:C:/requirements.txt').url
'file://C:/requirements.txt'  # expected: 'file:C:/requirements.txt' or 'file:///C:/requirements.txt'

The code always prepends file://, but this might be too many slashes or too few depending on the path's drive and root.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions