-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Path.from_uri()
doesn't work if the URI contains host component
#123599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Accepting a host component, other than It might be that UNC Windows file paths even further. I tested this on Windows and there this usage makes sense: >>> p = Path(r'\\host\path')
>>> print(p.as_uri())
file://host/path/
>>> p == Path.from_uri(p.as_uri())
True Perhaps |
All URIs with non-empty, non- path = Path.from_uri('file://server/share/foo.txt')
if path.as_posix().startswith('//'):
raise ValueError('Non-local file URI') We could add a |
Explicitly rejecting paths would certainly be better than returning invalid paths. Including |
This probably would anyway require special handling on Windows. On POSIX something like |
We should try to be consistent across OSs unless we really must diverge IMO.
Paths starting with two slashes are valid on both Windows and POSIX. On Windows they're UNC paths, whereas on POSIX they're implementation-defined (ref). |
It can be hard to be totally consistent across OSes. On Windows There's already other functionality in I should have used "incorrect" instead of "invalid" in my earlier comment. Although |
…()` on POSIX Raise `ValueError` in `pathlib.Path.from_uri()` if the given `file:` URI specifies a non-empty, non-`localhost` authority, and we're running on a platform without support for UNC paths.
Upon further consideration, I've adjusted my PR so that:
This is basically the same logic as |
Call `urllib.request.url2pathname()` from `pathlib.Path.from_uri()` rather than re-implementing it. This paves the way for solving the main issue (ignoring local authorities and rejecting non-local ones) in urllib, not pathlib.
Call `urllib.request.pathname2url()` from `pathlib.Path.as_uri()`, and deprecate the duplicate implementation in `PurePath`.
Call `urllib.request.url2pathname()` from `pathlib.Path.from_uri()` rather than re-implementing it. This paves the way for solving the main issue (ignoring local authorities and rejecting non-local ones) in urllib, not pathlib.
Call `urllib.request.pathname2url()` from `pathlib.Path.as_uri()`, and deprecate the duplicate implementation in `PurePath`. Co-authored-by: Adam Turner <[email protected]>
…26844) In `urllib.request.url2pathname()`, if the authority resolves to the current host, discard it. If an authority is present but resolves somewhere else, then on Windows we return a UNC path (as before), and on other platforms we raise `URLError`. Affects `pathlib.Path.from_uri()` in the same way. Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
Fixed in 3.14 |
A new test added in #126844 fails on FreeBSD: https://buildbot.python.org/#/builders/1255/builds/457/steps/6/logs/stdio
Can reproduce locally in a VM. |
>>> socket.gethostbyname(socket.gethostname())
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
socket.gethostbyname(socket.gethostname())
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 8] Name does not resolve |
…in urllib In `_is_local_authority()`, return early if the authority matches the machine hostname from `socket.gethostname()`, rather than resolving the names and matching IP addresses.
The above test failure was fixed in #132523 |
…thon#127237) Call `urllib.request.url2pathname()` from `pathlib.Path.from_uri()` rather than re-implementing it. This paves the way for solving the main issue (ignoring local authorities and rejecting non-local ones) in urllib, not pathlib.
…ython#127380) Call `urllib.request.pathname2url()` from `pathlib.Path.as_uri()`, and deprecate the duplicate implementation in `PurePath`. Co-authored-by: Adam Turner <[email protected]>
…RL (python#126844) In `urllib.request.url2pathname()`, if the authority resolves to the current host, discard it. If an authority is present but resolves somewhere else, then on Windows we return a UNC path (as before), and on other platforms we raise `URLError`. Affects `pathlib.Path.from_uri()` in the same way. Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
Re-opening to discuss this point from Serhiy on #126844:
Having given it more thought, I reckon we should call Thoughts / 👍 / 👎? |
You just read my mind. I was thinking about suggesting adding a keyword argument for resolving hostname, but then I thought that But adding an option is safer. In future we can even change its default value, but the users will be able to request the old behavior. |
…fault Follow-up to 0879ebc. Add *resolve_netloc* keyword-only argument to `url2pathname()`, defaulting to false. When set to true, we call `socket.gethostbyname()` to resolve the URL authority (netloc).
…fault Follow-up to 0879ebc. Add *resolve_netloc* keyword-only argument to `url2pathname()`, defaulting to false. When set to true, we call `socket.gethostbyname()` to resolve the URL authority (netloc).
Also FYI, I'm seeing |
…132610) Follow-up to 66cdb2b. Add *resolve_host* keyword-only argument to `url2pathname()`, defaulting to false. When set to true, we call `socket.gethostbyname()` to resolve the URL hostname. Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Steve Dower <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
Path.from_uri()
introduced in Python 3.13 doesn't work properly if the URI contains a host component other thanlocalhost
. Following examples are run with Python 3.13 rc 1 on Linux with a machine having host namekone
:According to RFC 8089 including the host component as a fully qualified name is fine so this looks like a bug to me.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
pathlib.Path.from_uri()
on POSIX #123650url2pathname()
: handle authority section in file URL #126844url2pathname()
implementation #127237pathname2url()
implementation #127380file:
URL hostname against machine hostname in urllib #132523url2pathname()
: don't callgethostbyname()
by default #132610The text was updated successfully, but these errors were encountered: