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:
Barney Gale 2024-11-25 19:59:20 +00:00 committed by GitHub
parent a2ee899682
commit 5bb059fe60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 20 deletions

View file

@ -159,12 +159,14 @@ The :mod:`urllib.request` module defines the following functions:
'file:///C:/Program%20Files'
.. versionchanged:: 3.14
Windows drive letters are no longer converted to uppercase.
Paths beginning with a slash are converted to URLs with authority
sections. For example, the path ``/etc/hosts`` is converted to
the URL ``///etc/hosts``.
.. versionchanged:: 3.14
On Windows, ``:`` characters not following a drive letter are quoted. In
previous versions, :exc:`OSError` was raised if a colon character was
found in any position other than the second character.
Windows drive letters are no longer converted to uppercase, and ``:``
characters not following a drive letter no longer cause an
:exc:`OSError` exception to be raised on Windows.
.. function:: url2pathname(url)