Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
bpo-24241: xdg-settings for preferred X browser
The traditional first entry in the tryorder queue is xdg-open, which
doesn't support new window. Make the default browser first
in the try list, to properly support these options.
  • Loading branch information
davesteele committed Feb 24, 2017
commit 871455f7591527b37889d6e359683608a0f4c33d
19 changes: 16 additions & 3 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
class Error(Exception):
pass

_browsers = {} # Dictionary of available browser controllers
_tryorder = [] # Preference order of available browsers
_browsers = {} # Dictionary of available browser controllers
_tryorder = [] # Preference order of available browsers
_os_preferred_browser = None # The preferred browser

def register(name, klass, instance=None, *, preferred=False):
"""Register a browser connector."""
_browsers[name.lower()] = [klass, instance]
if preferred:

# Preferred browsers go to the front of the list.
# Need to match to the default browser returned by xdg-settings, which
# may be of the form e.g. "firefox.desktop".
if preferred or (_os_preferred_browser and name in _os_preferred_browser):
_tryorder.insert(0, name)
else:
_tryorder.append(name)
Expand Down Expand Up @@ -484,6 +489,14 @@ def register_X_browsers():

# Prefer X browsers if present
if os.environ.get("DISPLAY"):
try:
cmd = "xdg-settings get default-web-browser".split()
result = subprocess.check_output(cmd).decode().strip()
except (FileNotFoundError, subprocess.CalledProcessError):
pass
else:
_os_preferred_browser = result

register_X_browsers()

# Also try console browsers
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ Quentin Stafford-Fraser
Frank Stajano
Joel Stanley
Anthony Starks
David Steele
Oliver Steele
Greg Stein
Marek Stepniowski
Expand Down
5 changes: 5 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ Library
- Issue #23262: The webbrowser module now supports Firefox 36+ and derived
browsers. Based on patch by Oleg Broytman.

- Issue #24241: The webbrowser in an X environment now prefers using the
default browser directly. Also, the webbrowser register() function now has
a documented 'preferred' argument, to specify browsers to be returned by
get() with no arguments. Patch by David Steele

- Issue #27939: Fixed bugs in tkinter.ttk.LabeledScale and tkinter.Scale caused
by representing the scale as float value internally in Tk. tkinter.IntVar
now works if float value is set to underlying Tk variable.
Expand Down