Skip to content

Commit 871455f

Browse files
committed
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.
1 parent 30a06fd commit 871455f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Lib/webbrowser.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
class Error(Exception):
1414
pass
1515

16-
_browsers = {} # Dictionary of available browser controllers
17-
_tryorder = [] # Preference order of available browsers
16+
_browsers = {} # Dictionary of available browser controllers
17+
_tryorder = [] # Preference order of available browsers
18+
_os_preferred_browser = None # The preferred browser
1819

1920
def register(name, klass, instance=None, *, preferred=False):
2021
"""Register a browser connector."""
2122
_browsers[name.lower()] = [klass, instance]
22-
if preferred:
23+
24+
# Preferred browsers go to the front of the list.
25+
# Need to match to the default browser returned by xdg-settings, which
26+
# may be of the form e.g. "firefox.desktop".
27+
if preferred or (_os_preferred_browser and name in _os_preferred_browser):
2328
_tryorder.insert(0, name)
2429
else:
2530
_tryorder.append(name)
@@ -484,6 +489,14 @@ def register_X_browsers():
484489

485490
# Prefer X browsers if present
486491
if os.environ.get("DISPLAY"):
492+
try:
493+
cmd = "xdg-settings get default-web-browser".split()
494+
result = subprocess.check_output(cmd).decode().strip()
495+
except (FileNotFoundError, subprocess.CalledProcessError):
496+
pass
497+
else:
498+
_os_preferred_browser = result
499+
487500
register_X_browsers()
488501

489502
# Also try console browsers

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,7 @@ Quentin Stafford-Fraser
14581458
Frank Stajano
14591459
Joel Stanley
14601460
Anthony Starks
1461+
David Steele
14611462
Oliver Steele
14621463
Greg Stein
14631464
Marek Stepniowski

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ Library
433433
- Issue #23262: The webbrowser module now supports Firefox 36+ and derived
434434
browsers. Based on patch by Oleg Broytman.
435435

436+
- Issue #24241: The webbrowser in an X environment now prefers using the
437+
default browser directly. Also, the webbrowser register() function now has
438+
a documented 'preferred' argument, to specify browsers to be returned by
439+
get() with no arguments. Patch by David Steele
440+
436441
- Issue #27939: Fixed bugs in tkinter.ttk.LabeledScale and tkinter.Scale caused
437442
by representing the scale as float value internally in Tk. tkinter.IntVar
438443
now works if float value is set to underlying Tk variable.

0 commit comments

Comments
 (0)