-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
webbrowser.open with file: URLs may launch editor instead of browser #128540
Comments
I made a PR #130031 |
Thanks for the issue. And for the PR. But I don't think the issue correctly describes the current behavior and I'm not sure how we could change it without creating incompatibilities. I am most familiar with the macOS behavior. As an example, the Python IDLE app on macOS uses
And, of course, the user has some control over LaunchServices file association settings via the Finder. The macOS I would imagine there are similar gotcha's on other platforms. The current documentation for webbrowser.open states:
So there are definitely programs out there expecting to use Other opinions? |
We at least have many years of people being confused and surprised and frustrated by the current behavior in Jupyter. Plus, there is the factual inaccuracy in the name "webbrowser.open" doesn't open a webbrowser when you give it a URL. I think the linked note in the docs mentions "filename", not "file:// url", which to me is a different thing, but I could be wrong. If indeed this case is really that file:// URLs are "unsupported" as that bit of the docs suggests, then consider this a feature request to support them. |
I agree that the documentation is ambiguous: I assumed that I also agree that we should explicitly support As far as whether we should or realistically can ensure that |
I opened a discussion and implemented a prototype that works on all 3 platforms, at least in my tests. I think it may still be correct and preferable to disallow |
Thanks for doing both. @ronaldoussoren would be the best person to review the macOS part of your prototype. |
I've looked at the Mac bit of the prototype, it uses the A way to to this while doing all work in AppleScript: diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
index d2efc72113a..6eeabe2587b 100644
--- a/Lib/webbrowser.py
+++ b/Lib/webbrowser.py
@@ -598,6 +598,25 @@ def open(self, url, new=0, autoraise=True):
url = url.replace('"', '%22')
if self.name == 'default':
- script = f'open location "{url}"' # opens in default browser
+ script = f'''
+ use framework "AppKit"
+ use AppleScript version "2.4"
+ use scripting additions
+
+ property NSWorkspace : a reference to current application's NSWorkspace
+ property NSURL : a reference to current application's NSURL
+
+ set wurl to NSURL's URLWithString:"https://www.apple.com"
+ set thisBrowser to (NSWorkspace's sharedWorkspace)'s ¬
+ URLForApplicationToOpenURL:wurl
+ set appname to (thisBrowser's absoluteString)'s lastPathComponent()'s ¬
+ stringByDeletingPathExtension() as text
+
+ tell application appname
+ activate
+ open location "{url}"
+ end tell
+ '''
else:
script = f'''
tell application "{self.name}" This has two (minor) advantages over the prototype:
BTW. I would have preferred either a PR or an explanation of what the prototype does and why. |
Thanks! I didn't know you could make the appkit calls directly from AppleScript, I'll update it, that is indeed far simpler. FWIW, the full description of the prototype is in the Python-ideas post, as best I could do it. My impression was that was preferred before a PR. |
Bug report
Transferred to https://discuss.python.org/t/support-for-file-urls-in-webbrowser-open/81612 until consensus is reached.
Bug description:
In the code:
It is common for
xdg-open
(on linux) oropen location
(on mac) oros.startfile
(on windows) to be selected as the default browser. This is great and simple forhttp[s]
urls, but when these are passedfile://
urls, all of the above select the default opener for HTML files, which may not be a browser at all (commonly a text editor for developer systems).I don't know if there's a way to indicate that Browser entries can handle given URL schemes, but when selecting a browser from the try list, ideally these generic 'open' mechanisms should be skipped for
file:
URLs. Even better, use standard APIs to lookup the default browsers forhttp[s]
and invoke that explicitly, rather than relying on an interpretation of "open URL" which is only valid for http URLs.I believe URLForApplicationToOpenURL is the current macOS API to look up the application for
http:
, which could be used to launch explicitly with a browser. I guessxdg-settings get
is already the equivalent for most linux situations and preferred if available, which is great. I'm not sure there's a more general way to discover whatxdg-open
will do withhttp://
, but we have occasional reports wherexdg-settings get default-web-browser
doesn't work andxdg-open
is invoked and doesn't launch a browser (or launches a different browser). I've no idea what the Windows call would be, but this looks like the same question.This appears to have been opened long ago as #37540 and erroneously closed as an Apple bug (all platforms exhibit this behavior, and I think the bug is pretty clearly in webbrowser's assumption that "open file://path.html" means "open file://path.html with a web browser" which does not appear to be what any of the platforms mean by the chosen API).
CPython versions tested on:
3.9, 3.10, 3.11, 3.12
Operating systems tested on:
Linux, macOS, Windows
Linked PRs
webbrowser.open
withfile:
URLs may launching editor instead of browser #130031The text was updated successfully, but these errors were encountered: