Skip to content

urllib.request.pathname2url(): generate RFC 1738 URLs where possible #127236

Description

@barneygale

Feature or enhancement

Proposal:

urllib.request.pathname2url currently generates RFC 1738-compliant file: URIs in the following cases:

This function cannot generate RFC 1738-compliant URLs for:

  • True relative paths, because of differing requirements for the first character of the path.

That leaves one case where the function could generate RFC 1738-compatible URLs, but doesn't:

  • Paths beginning with exactly one slash (both POSIX and Windows)
>>> from urllib.request import pathname2url
>>> pathname2url('/etc/hosts')
'/etc/hosts'  # expected: '///etc/hosts'

For consistency with pathname2url()'s handling of other paths, and consistency with pathlib.Path.as_uri(), I propose we prepend two slashes to any path beginning with precisely one slash to produce a URL authority section with a zero-length authority.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

#125866 (comment)

Linked PRs

Activity

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

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions