Skip to content

Commit 30a06fd

Browse files
committed
webbrowser.register: Add 'preferred' argument
Replace the existing undocumented tri-state 'try_order' parameter with the boolean keyword-only 'preferred' parameter. Setting it to True places the browser at the front of the list, preferring it as the return to a subsequent get() call. 'preferred' is added to the documentation.
1 parent 132ac38 commit 30a06fd

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Doc/library/webbrowser.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,19 @@ The following functions are defined:
8383
caller's environment.
8484

8585

86-
.. function:: register(name, constructor, instance=None)
86+
.. function:: register(name, constructor, instance=None, *, preferred=False)
8787

8888
Register the browser type *name*. Once a browser type is registered, the
8989
:func:`get` function can return a controller for that browser type. If
9090
*instance* is not provided, or is ``None``, *constructor* will be called without
9191
parameters to create an instance when needed. If *instance* is provided,
9292
*constructor* will never be called, and may be ``None``.
9393

94-
This entry point is only useful if you plan to either set the :envvar:`BROWSER`
95-
variable or call :func:`get` with a nonempty argument matching the name of a
96-
handler you declare.
94+
Setting *preferred* to ``True`` makes this browser a preferred result for
95+
a :func:`get` call with no argument. Otherwise, this entry point is only
96+
useful if you plan to either set the :envvar:`BROWSER` variable or call
97+
:func:`get` with a nonempty argument matching the name of a handler you
98+
declare.
9799

98100
A number of browser types are predefined. This table gives the type names that
99101
may be passed to the :func:`get` function and the corresponding instantiations

Lib/webbrowser.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class Error(Exception):
1616
_browsers = {} # Dictionary of available browser controllers
1717
_tryorder = [] # Preference order of available browsers
1818

19-
def register(name, klass, instance=None, update_tryorder=1):
20-
"""Register a browser connector and, optionally, connection."""
19+
def register(name, klass, instance=None, *, preferred=False):
20+
"""Register a browser connector."""
2121
_browsers[name.lower()] = [klass, instance]
22-
if update_tryorder > 0:
23-
_tryorder.append(name)
24-
elif update_tryorder < 0:
22+
if preferred:
2523
_tryorder.insert(0, name)
24+
else:
25+
_tryorder.append(name)
2626

2727
def get(using=None):
2828
"""Return a browser launcher instance appropriate for the environment."""
@@ -610,10 +610,10 @@ def open(self, url, new=0, autoraise=True):
610610

611611
# Don't clear _tryorder or _browsers since OS X can use above Unix support
612612
# (but we prefer using the OS X specific stuff)
613-
register("safari", None, MacOSXOSAScript('safari'), -1)
614-
register("firefox", None, MacOSXOSAScript('firefox'), -1)
615-
register("chrome", None, MacOSXOSAScript('chrome'), -1)
616-
register("MacOSX", None, MacOSXOSAScript('default'), -1)
613+
register("safari", None, MacOSXOSAScript('safari'), preferred=True)
614+
register("firefox", None, MacOSXOSAScript('firefox'), preferred=True)
615+
register("chrome", None, MacOSXOSAScript('chrome'), preferred=True)
616+
register("MacOSX", None, MacOSXOSAScript('default'), preferred=True)
617617

618618

619619
# OK, now that we know what the default preference orders for each
@@ -628,7 +628,7 @@ def open(self, url, new=0, autoraise=True):
628628
if cmdline != '':
629629
cmd = _synthesize(cmdline, -1)
630630
if cmd[1] is None:
631-
register(cmdline, None, GenericBrowser(cmdline), -1)
631+
register(cmdline, None, GenericBrowser(cmdline), preferred=True)
632632
cmdline = None # to make del work if _userchoices was empty
633633
del cmdline
634634
del _userchoices

0 commit comments

Comments
 (0)