Skip to content
Merged
13 changes: 11 additions & 2 deletions Doc/library/webbrowser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ If the environment variable :envvar:`BROWSER` exists, it is interpreted as the
:data:`os.pathsep`-separated list of browsers to try ahead of the platform
defaults. When the value of a list part contains the string ``%s``, then it is
interpreted as a literal browser command line to be used with the argument URL
substituted for ``%s``; if the part does not contain ``%s``, it is simply
interpreted as the name of the browser to launch. [1]_
substituted for ``%s``; if the value is a single word that refers to one of the
already registered browsers this browser is added to the front of the search list;
if the part does not contain ``%s``, it is simply interpreted as the name of the
browser to launch. [1]_

.. versionchanged:: 3.13

The :envvar:`BROWSER` variable can now also be used to reorder the list of
platform defaults. This is particularly useful on macOS where the platform
defaults do not refer to command-line tools on :envvar:`PATH`.


For non-Unix platforms, or when a remote browser is available on Unix, the
controlling process will not wait for the user to finish with the browser, but
Expand Down
17 changes: 15 additions & 2 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,10 @@ def register_standard_browsers():

if sys.platform == 'darwin':
register("MacOSX", None, MacOSXOSAScript('default'))
register("chrome", None, MacOSXOSAScript('chrome'))
register("chrome", None, MacOSXOSAScript('google chrome'))
register("firefox", None, MacOSXOSAScript('firefox'))
register("safari", None, MacOSXOSAScript('safari'))
# OS X can use below Unix support (but we prefer using the OS X
# macOS can use below Unix support (but we prefer using the macOS
# specific stuff)

if sys.platform == "serenityos":
Expand Down Expand Up @@ -539,6 +539,19 @@ def register_standard_browsers():
# Treat choices in same way as if passed into get() but do register
# and prepend to _tryorder
for cmdline in userchoices:
if all(x not in cmdline for x in " \t"):
# Assume this is the name of a registered command, use
# that unless it is a GenericBrowser.
try:
command = _browsers[cmdline.lower()]
except KeyError:
pass

else:
if not isinstance(command[1], GenericBrowser):
_tryorder.insert(0, cmdline.lower())
continue

if cmdline != '':
cmd = _synthesize(cmdline, preferred=True)
if cmd[1] is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:mod:`webbrowser`: Names in the :envvar:`BROWSER` environment variable can now
refer to already registered web browsers, instead of always generating a new
browser command.

This makes it possible to set :envvar:`BROWSER` to the value of one of the
supported browsers on macOS.