From 41d1eaad24a96034a597bf862eba58cf551ef4e6 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 09:49:02 +0100 Subject: [PATCH 1/8] gh-113539: Enable using ``$BROWSER`` to reorder default seach order in webbrowser.py --- Doc/library/webbrowser.rst | 13 +++++++++++-- Lib/webbrowser.py | 15 ++++++++++++++- ...2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst | 6 ++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst index 4667b81e38ada2..e45db37308e218 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: 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 diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index 636e8ca459d109..9bdd404bb51e9f 100755 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -466,7 +466,7 @@ 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 @@ -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 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..76ccd144f8ffe5 --- /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 webbrowsers, 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. From ea54d517bb9af1d1ed216f3ae0bf47e31ae162f0 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 11:14:50 +0100 Subject: [PATCH 2/8] Update Misc/NEWS.d/next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst Co-authored-by: Hugo van Kemenade --- .../next/Library/2023-12-29-09-44-41.gh-issue-113539.YDkv9O.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 76ccd144f8ffe5..c2c3a2d17c14aa 100644 --- 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 @@ -1,5 +1,5 @@ :mod:`webbrowser`: Names in the :envvar:`BROWSER` environment variable can now -refer to already registered webbrowsers, instead of always generating a new +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 From e6ec00d1a24fe1c5485bb085d47abbe2b573c6e6 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 11:15:02 +0100 Subject: [PATCH 3/8] Update Doc/library/webbrowser.rst Co-authored-by: Hugo van Kemenade --- Doc/library/webbrowser.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst index e45db37308e218..bff2395f3110dd 100644 --- a/Doc/library/webbrowser.rst +++ b/Doc/library/webbrowser.rst @@ -29,7 +29,7 @@ already registered browsers this browser is added to the front of the search lis if the part does not contain ``%s``, it is simply interpreted as the name of the browser to launch. [1]_ -.. versionchanged: 3.13 +.. 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 From 348db4da58d454020acebdbf403314082d2d3620 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 11:15:11 +0100 Subject: [PATCH 4/8] Update Lib/webbrowser.py Co-authored-by: Hugo van Kemenade --- Lib/webbrowser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index 9bdd404bb51e9f..bd79327e0c9818 100755 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -540,7 +540,7 @@ def register_standard_browsers(): # and prepend to _tryorder for cmdline in userchoices: if all(x not in cmdline for x in " \t"): - # Assume this the name of a registered command, use + # Assume this is the name of a registered command, use # that unless it is a GenericBrowser. try: command = _browsers[cmdline.lower()] From 20917f60ff18135e38180a8956bbd7e2927a18d9 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 11:15:18 +0100 Subject: [PATCH 5/8] Update Lib/webbrowser.py Co-authored-by: Hugo van Kemenade --- Lib/webbrowser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index bd79327e0c9818..7ff710d3f24404 100755 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -469,7 +469,7 @@ def register_standard_browsers(): 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": From dfd36fb09734076b1365c304ea800b5447305cc9 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 29 Dec 2023 11:16:06 +0100 Subject: [PATCH 6/8] Update Doc/library/webbrowser.rst Co-authored-by: Hugo van Kemenade --- Doc/library/webbrowser.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst index bff2395f3110dd..b0bc095ca3a9ec 100644 --- a/Doc/library/webbrowser.rst +++ b/Doc/library/webbrowser.rst @@ -25,8 +25,8 @@ If the environment variable :envvar:`BROWSER` exists, it is interpreted as the 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 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 +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 From d41811590bbd403e6d24dac37700f5f5743e3c7f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:04:41 +0300 Subject: [PATCH 7/8] Use versionchanged:: next --- Doc/library/webbrowser.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst index 376b1ffdd1ebcc..e5f6aa1436ffb1 100644 --- a/Doc/library/webbrowser.rst +++ b/Doc/library/webbrowser.rst @@ -29,7 +29,7 @@ already registered browsers this browser is added to the front of the search lis if the part does not contain ``%s``, it is simply interpreted as the name of the browser to launch. [1]_ -.. versionchanged:: 3.13 +.. 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 From f72ac250ab6e311a6f342f93946caed48c4f1c1a Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:26:35 +0300 Subject: [PATCH 8/8] Add to What's New --- Doc/whatsnew/3.14.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 5b03bd9e5a8caf..4e3cc4f0c69209 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -1378,6 +1378,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 -------