Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
barneygale opened this issue Nov 24, 2024 · 0 comments
Closed

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

barneygale opened this issue Nov 24, 2024 · 0 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Nov 24, 2024

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

@barneygale barneygale added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Nov 24, 2024
barneygale added a commit that referenced this issue Nov 25, 2024
… path (#127194)

When handed an absolute Windows path such as `C:\foo` or `//server/share`,
the `urllib.request.pathname2url()` function returns a URL with an
authority section, such as `///C:/foo` or `//server/share` (or before
GH-126205, `////server/share`). Only the `file:` prefix is omitted.

But when handed an absolute POSIX path such as `/etc/hosts`, or a Windows
path of the same form (rooted but lacking a drive), the function returns a
URL without an authority section, such as `/etc/hosts`.

This patch corrects the discrepancy by adding a `//` prefix before
drive-less, rooted paths when generating URLs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant