mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
GH-127236: pathname2url()
: generate RFC 1738 URL for absolute POSIX 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.
This commit is contained in:
parent
a2ee899682
commit
5bb059fe60
5 changed files with 33 additions and 20 deletions
|
@ -1667,9 +1667,11 @@ else:
|
|||
def pathname2url(pathname):
|
||||
"""OS-specific conversion from a file system path to a relative URL
|
||||
of the 'file' scheme; not recommended for general use."""
|
||||
if pathname[:2] == '//':
|
||||
# Add explicitly empty authority to avoid interpreting the path
|
||||
# as authority.
|
||||
if pathname[:1] == '/':
|
||||
# Add explicitly empty authority to absolute path. If the path
|
||||
# starts with exactly one slash then this change is mostly
|
||||
# cosmetic, but if it begins with two or more slashes then this
|
||||
# avoids interpreting the path as a URL authority.
|
||||
pathname = '//' + pathname
|
||||
encoding = sys.getfilesystemencoding()
|
||||
errors = sys.getfilesystemencodeerrors()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue