Skip to content

urllib.request.pathname2url() and url2pathname() remove slashes on Windows #126212

Closed
@barneygale

Description

@barneygale

Bug report

Bug description:

urllib.request.pathname2url() unexpectedly removes doubled and trailing slashes, but only on Windows, and only for DOS device paths (not UNC paths):

>>> from urllib.request import pathname2url, url2pathname
>>> pathname2url('C:\\')
'///C:'  # This is *not* the same path!
>>> pathname2url('C:\\foo\\')
'///C:/foo'
>>> pathname2url('C:\\foo\\\\bar')
'///C:/foo/bar'

url2pathname() does almost the same thing, except that it specially preserves a slash immediately after a DOS drive (but not elsewhere):

>>> url2pathname('///c:/')
'C:\\'  # OK
>>> url2pathname('///c:/foo/')
'C:\\foo'
>>> url2pathname('///c:/foo//bar')
'C:\\foo\\bar'

This behaviour makes no sense and has no equivalent on POSIX, where all slashes are preserved.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions