nturl2path.pathname2url() doesn't handle forward slashes #120423
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Paths with forward slashes aren't handled the same as paths with backslashes:
>>> from nturl2path import pathname2url
>>> pathname2url('//?/unc/server/share/dir')
'//%3F/unc/server/share/dir' # NOK
>>> pathname2url(r'\\?\unc\server\share\dir')
'//server/share/dir' # OKCPython versions tested on:
3.13
Operating systems tested on:
Windows
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
nineteendo commentedon Jun 12, 2024
Quoting @eryksun:
eryksun commentedon Jun 12, 2024
While using an empty authority (i.e. hostname or namespace) for a UNC path is valid according to RFC8089 (e.g. "file:////spam/eggs/foo"), the two-slash form with an authority is preferred on Windows (e.g. "file://spam/eggs/foo"). For DOS drive paths, the form with an empty authority is preferred (e.g. "file:///C:/eggs/foo"). But using an explicit authority is also supported (e.g. "file://localhost/C:/eggs/foo", or sometimes "file://./C:/eggs/foo").
barneygale commentedon Jun 12, 2024
On POSIX,
urllib.request.pathname2url()simply callsquote(), so you might expect to use it like this:But if we try the same thing using the Windows version we get unusual results:
I don't think there's a way to use it correctly. IMHO we should deprecate + remove it.
barneygale commentedon Jun 12, 2024
We can recommend
Path.as_uri()to users as a replacement. It's been available since pathlib was added to the standard library.It's tempting to also deprecate and remove
url2pathname(), but:pathname2url()), andPath.from_uri()was only added in 3.13, and if we want to be kind to package maintainers, I think we ought to wait until 3.12 drops out of support.pygeek commentedon Jun 13, 2024
If we want to encourage usage of
Path.from_urioverurl2pathnamewouldn't that justify a depreciation notice, at least a recommendation in the docs?barneygale commentedon Oct 30, 2024
#126148 will fix this
nineteendo commentedon Oct 30, 2024
Thanks, I was already wondering if that was the case.
pythonGH-120423: `pathname2url()`: handle forward slashes in Windows …
barneygale commentedon Nov 8, 2024
I decided to solve URL parsing/generation problems piecemeal rather than doing everything in #126148
Specific PR available: #126593
GH-120423: `pathname2url()`: handle forward slashes in Windows paths (#…
pythonGH-120423: `pathname2url()`: handle forward slashes in Windows …
pythonGH-120423: `pathname2url()`: handle forward slashes in Windows …
[3.12] GH-120423: `pathname2url()`: handle forward slashes in Windows…
[3.13] GH-120423: `pathname2url()`: handle forward slashes in Windows…
barneygale commentedon Nov 12, 2024
Fixed! Thanks for your input, all.
pythonGH-120423: `pathname2url()`: handle forward slashes in Windows …
pythonGH-120423: `pathname2url()`: handle forward slashes in Windows …