]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.12] Improve `pathname2url()` and `url2pathname()` docs (GH-127125) (#127233)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 24 Nov 2024 17:48:12 +0000 (18:48 +0100)
committerGitHub <noreply@github.com>
Sun, 24 Nov 2024 17:48:12 +0000 (17:48 +0000)
commitb52ab48a10276ea332cd80e5fcc0c0dd2a4b7e02
tree83de6ce8033d315893485a477c87e6f0a4bb6832
parente26ba9670f31df3db471365ddef7b0c293ef6871
[3.12] Improve `pathname2url()` and `url2pathname()` docs (GH-127125) (#127233)

Improve `pathname2url()` and `url2pathname()` docs (GH-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 307c63358681d669ae39e5ecd814bded4a93443a)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
Doc/library/urllib.request.rst