Skip to content

Commit 2d0894d

Browse files
committed
Only perform webbrowse.register() when necessary
1 parent 449c250 commit 2d0894d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

msal/application.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,25 @@ def _preferred_browser():
7575
if running on Linux and there is no BROWSER env var, otherwise return None.
7676
"""
7777
browser_path = "/usr/bin/microsoft-edge" # Use a full path owned by sys admin
78-
browser_name = "microsoft-edge" # Use a generic meaningful name
79-
if ("BROWSER" not in os.environ # Customize it when end user has no preference
78+
user_has_no_preference = "BROWSER" not in os.environ
79+
user_opted_in_for_edge = "microsoft-edge" in os.environ.get("BROWSER", "")
80+
if ((user_opted_in_for_edge or user_has_no_preference)
8081
and sys.platform == "linux" # On Linux, only Edge will have CA support
8182
and os.path.exists(browser_path)): # Edge is usually installed here
8283
try:
8384
import webbrowser # Lazy import. Some distro may not have this.
84-
webbrowser.register(
85-
browser_name, None, webbrowser.BackgroundBrowser(browser_path))
85+
browser_name = "msal-edge" # A unique name rather than "microsoft-edge"
86+
# otherwise `BROWSER="microsoft-edge"; webbrowser.get("microsoft-edge")`
87+
# would return a GenericBrowser instance which won't work.
88+
try:
89+
registration_available = isinstance(
90+
webbrowser.get(browser_name), webbrowser.BackgroundBrowser)
91+
except webbrowser.Error:
92+
registration_available = False
93+
if not registration_available:
94+
logger.debug("Register %s with %s", browser_name, browser_path)
95+
webbrowser.register( # Even double-register happens to work fine
96+
browser_name, None, webbrowser.BackgroundBrowser(browser_path))
8697
return browser_name
8798
except ImportError:
8899
pass # We may still proceed

0 commit comments

Comments
 (0)