Skip to content

Commit 66816c5

Browse files
barneygalemiss-islington
authored andcommitted
Improve pathname2url() and url2pathname() docs (pythonGH-127125)
These functions have long sown confusion among Python developers. The existing documentation says they deal with URL path components, but that doesn't fit the evidence on Windows: >>> pathname2url(r'C:\foo') '///C:/foo' >>> pathname2url(r'\\server\share') '////server/share' # or '//server/share' as of quite recently If these were URL path components, they would imply complete URLs like `file://///C:/foo` and `file://////server/share`. Clearly this isn't right. Yet the implementation in `nturl2path` is deliberate, and the `url2pathname()` function correctly inverts it. On non-Windows platforms, the behaviour until quite recently is to simply quote/unquote the path without adding or removing any leading slashes. This behaviour is compatible with *both* interpretations -- 1) the value is a URL path component (existing docs), and 2) the value is everything following `file:` (this commit) The conclusion I draw is that these functions operate on everything after the `file:` prefix, which may include an authority section. This is the only explanation that fits both the Windows and non-Windows behaviour. It's also a better match for the function names. (cherry picked from commit 307c633) Co-authored-by: Barney Gale <[email protected]>
1 parent e26ba96 commit 66816c5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Doc/library/urllib.request.rst

+19-7
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,28 @@ The :mod:`urllib.request` module defines the following functions:
160160

161161
.. function:: pathname2url(path)
162162

163-
Convert the pathname *path* from the local syntax for a path to the form used in
164-
the path component of a URL. This does not produce a complete URL. The return
165-
value will already be quoted using the :func:`~urllib.parse.quote` function.
163+
Convert the given local path to a ``file:`` URL. This function uses
164+
:func:`~urllib.parse.quote` function to encode the path. For historical
165+
reasons, the return value omits the ``file:`` scheme prefix. This example
166+
shows the function being used on Windows::
166167

168+
>>> from urllib.request import pathname2url
169+
>>> path = 'C:\\Program Files'
170+
>>> 'file:' + pathname2url(path)
171+
'file:///C:/Program%20Files'
167172

168-
.. function:: url2pathname(path)
169173

170-
Convert the path component *path* from a percent-encoded URL to the local syntax for a
171-
path. This does not accept a complete URL. This function uses
172-
:func:`~urllib.parse.unquote` to decode *path*.
174+
.. function:: url2pathname(url)
175+
176+
Convert the given ``file:`` URL to a local path. This function uses
177+
:func:`~urllib.parse.unquote` to decode the URL. For historical reasons,
178+
the given value *must* omit the ``file:`` scheme prefix. This example shows
179+
the function being used on Windows::
180+
181+
>>> from urllib.request import url2pathname
182+
>>> url = 'file:///C:/Program%20Files'
183+
>>> url2pathname(url.removeprefix('file:'))
184+
'C:\\Program Files'
173185

174186
.. function:: getproxies()
175187

0 commit comments

Comments
 (0)