@@ -75,14 +75,25 @@ def _preferred_browser():
75
75
if running on Linux and there is no BROWSER env var, otherwise return None.
76
76
"""
77
77
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 )
80
81
and sys .platform == "linux" # On Linux, only Edge will have CA support
81
82
and os .path .exists (browser_path )): # Edge is usually installed here
82
83
try :
83
84
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 ))
86
97
return browser_name
87
98
except ImportError :
88
99
pass # We may still proceed
0 commit comments