summaryrefslogtreecommitdiffstats
path: root/Lib/urllib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-25 19:59:20 (GMT)
committerGitHub <noreply@github.com>2024-11-25 19:59:20 (GMT)
commit5bb059fe606983814a445e4dcf9e96fd7cb4951a (patch)
tree676161387dee7f7b757b947199db07f8b29da54f /Lib/urllib
parenta2ee89968299fc4f0da4b5a4165025b941213ba5 (diff)
downloadcpython-5bb059fe606983814a445e4dcf9e96fd7cb4951a.zip
cpython-5bb059fe606983814a445e4dcf9e96fd7cb4951a.tar.gz
cpython-5bb059fe606983814a445e4dcf9e96fd7cb4951a.tar.bz2
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.
Diffstat (limited to 'Lib/urllib')
-rw-r--r--Lib/urllib/request.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 9e55543..1fcaa89 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -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()