Skip to content

urllib.request.url2pathname() mishandles colons in URLs on Windows #126367

Closed

Description

@barneygale

Bug report

Bug description:

On Windows, urllib.request.url2pathname() has overly simplistic handling of DOS drives in URLs.

Per @eryksun:

>>> from urllib.request import url2pathname
>>> url2pathname('//host/share/spam.txt:eggs')
'T:\\eggs'  # expected: r'\\host\share\spam.txt:eggs'

Also:

>>> url2pathname('///c:/spam.txt:eggs')
OSError: Bad URL: ///c|/spam.txt|eggs  # expected: r'c:\spam.txt:eggs'

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Nov 3, 2024
  2. samtstephens commented on Nov 3, 2024

    @samtstephens

    Hi, could this be assigned to me? I'm an undergrad working to make an open source contribution to a software library. Thanks!

  3. barneygale commented on Nov 4, 2024

    @barneygale
    ContributorAuthor

    Hey, yes that's absolutely fine, thank you for offering! A couple of pointers:

  4. barneygale commented on Nov 4, 2024

    @barneygale
    ContributorAuthor

    I should also note the Python Dev Guide's Lifecycle of a Pull Request guide :)

  5. samtstephens commented on Nov 7, 2024

    @samtstephens

    Thank you! Will check it out.

  6. barneygale commented on Nov 8, 2024

    @barneygale
    ContributorAuthor

    Just FYI, the PR I mentioned previously has now landed, so feel free to have a crack at solving this issue! Let me know if you need a hand.

  7. barneygale commented on Nov 24, 2024

    @barneygale
    ContributorAuthor

    Hey @samtstephens, if you need a hint, you could look at this code in pathlib:

    if path[:3] == '///':
    # Remove empty authority
    path = path[2:]
    elif path[:12] == '//localhost/':
    # Remove 'localhost' authority
    path = path[11:]
    if path[:3] == '///' or (path[:1] == '/' and path[2:3] in ':|'):
    # Remove slash before DOS device/UNC path
    path = path[1:]
    if path[1:2] == '|':
    # Replace bar with colon in DOS drive
    path = path[:1] + ':' + path[2:]

    I think it does roughly what we want to happen in nturl2path.url2pathname() :)

  8. samtstephens commented on Nov 25, 2024

    @samtstephens

    Thanks! Me and my partner are trying this out!

  9. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 8, 2024
  10. added 2 commits that reference this issue on Mar 18, 2025
  11. barneygale commented on Mar 18, 2025

    @barneygale
    ContributorAuthor

    Fixed in 3.14. Given this was reported internally and the fix is somewhat risky, I'll skip backporting. Happy to revisit if anyone wants.

  12. added a commit that references this issue on Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions