Skip to content

Commit ca8b1d0

Browse files
gh-87277: Don't look for X11 browsers on macOS in webbrowser (#24480)
The installation of XQuartz on macOS will unconditionally set the $DISPLAY variable. The X11 server will be launched when a program tries to access the display. This results in launching the X11 server when using the webbrowser module, even though X11 browsers won't be used in practice.
1 parent 723f4d6 commit ca8b1d0

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_webbrowser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,17 @@ def test_register_preferred(self):
272272
self._check_registration(preferred=True)
273273

274274

275+
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test")
276+
def test_no_xdg_settings_on_macOS(self):
277+
# On macOS webbrowser should not use xdg-settings to
278+
# look for X11 based browsers (for those users with
279+
# XQuartz installed)
280+
with mock.patch("subprocess.check_output") as ck_o:
281+
webbrowser.register_standard_browsers()
282+
283+
ck_o.assert_not_called()
284+
285+
275286
class ImportTest(unittest.TestCase):
276287
def test_register(self):
277288
webbrowser = import_helper.import_fresh_module('webbrowser')

Lib/webbrowser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,12 @@ def register_standard_browsers():
495495
register("microsoft-edge", None, Edge("MicrosoftEdge.exe"))
496496
else:
497497
# Prefer X browsers if present
498-
if os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"):
498+
#
499+
# NOTE: Do not check for X11 browser on macOS,
500+
# XQuartz installation sets a DISPLAY environment variable and will
501+
# autostart when someone tries to access the display. Mac users in
502+
# general don't need an X11 browser.
503+
if sys.platform != "darwin" and (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
499504
try:
500505
cmd = "xdg-settings get default-web-browser".split()
501506
raw_result = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
webbrowser: Don't look for X11 browsers on macOS. Those are generally not
2+
used and probing for them can result in starting XQuartz even if it isn't
3+
used otherwise.

0 commit comments

Comments
 (0)