https://github.com/python/cpython/commit/25e49841e3c943d5746f2eb57375a7460651d088
commit: 25e49841e3c943d5746f2eb57375a7460651d088
branch: main
author: Ronald Oussoren <ronaldousso...@mac.com>
committer: hugovk <1324225+hug...@users.noreply.github.com>
date: 2025-04-25T14:36:18+03:00
summary:

gh-113539: Enable using ``$BROWSER`` to reorder default seach order in 
webbrowser.py (#113561)

Co-authored-by: Hugo van Kemenade <hug...@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hug...@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst
M Doc/library/webbrowser.rst
M Doc/whatsnew/3.14.rst
M Lib/webbrowser.py

diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst
index baccc791b227fd..cbb2b06c2a0f9d 100644
--- a/Doc/library/webbrowser.rst
+++ b/Doc/library/webbrowser.rst
@@ -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:: next
+
+   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
diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index 3893060b153210..bc7b12c5be21d0 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -1421,6 +1421,17 @@ uuid
   (Contributed by Simon Legner in :gh:`131236`.)
 
 
+webbrowser
+----------
+
+* Names in the :envvar:`BROWSER` environment variable can now refer to already
+  registered browsers for the :mod:`webbrowser` module, 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.
+
+
 zipinfo
 -------
 
diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
index 232d3c3a9c5938..ab50ec1ee95f9e 100644
--- a/Lib/webbrowser.py
+++ b/Lib/webbrowser.py
@@ -482,10 +482,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 == "ios":
@@ -559,6 +559,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:
diff --git 
a/Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst 
b/Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst
new file mode 100644
index 00000000000000..c2c3a2d17c14aa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to