8000 gh-113539: Enable using ``$BROWSER`` to reorder default seach order i… · python/cpython@25e4984 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25e4984

Browse files
gh-113539: Enable using $BROWSER to reorder default seach order in webbrowser.py (#113561)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 1a70f66 commit 25e4984

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

Doc/library/webbrowser.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ If the environment variable :envvar:`BROWSER` exists, it is interpreted as the
2424
:data:`os.pathsep`-separated list of browsers to try ahead of the platform
2525
defaults. When the value of a list part contains the string ``%s``, then it is
2626
interpreted as a literal browser command line to be used with the argument URL
27-
substituted for ``%s``; if the part does not contain ``%s``, it is simply
28-
interpreted as the name of the browser to launch. [1]_
27+
substituted for ``%s``; if the value is a single word that refers to one of the
28+
already registered browsers this browser is added to the front of the search list;
29+
if the part does not contain ``%s``, it is simply interpreted as the name of the
30+
browser to launch. [1]_
31+
32+
.. versionchanged:: next
33+
34+
The :envvar:`BROWSER` variable can now also be used to reorder the list of
35+
platform defaults. This is particularly useful on macOS where the platform
36+
defaults do not refer to command-line tools on :envvar:`PATH`.
37+
2938

3039
For non-Unix platforms, or when a remote browser is available on Unix, the
3140
controlling process will not wait for the user to finish with the browser, but

Doc/whatsnew/3.14.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,17 @@ uuid
14211421
(Contributed by Simon Legner in :gh:`131236`.)
14221422

14231423

1424+
webbrowser
1425+
----------
1426+
1427+
* Names in the :envvar:`BROWSER` environment variable can now refer to already
1428+
registered browsers for the :mod:`webbrowser` module, instead of always
1429+
generating a new browser command.
1430+
1431+
This makes it possible to set :envvar:`BROWSER` to the value of one of the
1432+
supported browsers on macOS.
1433+
1434+
14241435
zipinfo
14251436
-------
14261437

Lib/webbrowser.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,10 @@ def register_standard_browsers():
482482

483483
if sys.platform == 'darwin':
484484
register("MacOSX", None, MacOSXOSAScript('default'))
485-
register("chrome", None, MacOSXOSAScript('chrome'))
485+
register("chrome", None, MacOSXOSAScript('google chrome'))
486486
register("firefox", None, MacOSXOSAScript('firefox'))
487487
register("safari", None, MacOSXOSAScript('safari'))
488-
# OS X can use below Unix support (but we prefer using the OS X
488+
# macOS can use below Unix support (but we prefer using the macOS
489489
# specific stuff)
490490

491491
if sys.platform == "ios":
@@ -559,6 +559,19 @@ def register_standard_browsers():
559559
# Treat choices in same way as if passed into get() but do register
560560
# and prepend to _tryorder
561561
for cmdline in userchoices:
562+
if all(x not in cmdline for x in " \t"):
563+
# Assume this is the name of a registered command, use
564+
# that unless it is a GenericBrowser.
565+
try:
566+
command = _browsers[cmdline.lower()]
567+
except KeyError:
568+
pass
569+
570+
else:
571+
if not isinstance(command[1], GenericBrowser):
572+
_tryorder.insert(0, cmdline.lower())
573+
continue
574+
562575
if cmdline != '':
563576
cmd = _synthesize(cmdline, preferred=True)
564577
if cmd[1] is None:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:mod:`webbrowser`: Names in the :envvar:`BROWSER` environment variable can now
2+
refer to already registered web browsers, instead of always generating a new
3+
browser command.
4+
5+
This makes it possible to set :envvar:`BROWSER` to the value of one of the
6+
supported browsers on macOS.

0 commit comments

Comments
 (0)
0