Skip to content

Commit 8e1eef4

Browse files
committed
gh-137586: Load AppKit before NSWorkspace lookup in _macos_default_browser_bundle_id
NSWorkspace is an AppKit class and is not registered in the ObjC runtime until AppKit is loaded. Without the explicit LoadLibrary call, objc_getClass returns nil for NSWorkspace, causing the entire lookup to silently fall back to /usr/bin/open without -b.
1 parent fdd6649 commit 8e1eef4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Lib/webbrowser.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ def _macos_default_browser_bundle_id():
626626
from ctypes import cdll, c_void_p, c_char_p
627627
from ctypes.util import find_library
628628

629+
# NSWorkspace is an AppKit class; load AppKit to register it.
630+
cdll.LoadLibrary(
631+
'/System/Library/Frameworks/AppKit.framework/AppKit'
632+
)
629633
objc = cdll.LoadLibrary(find_library('objc'))
630634
objc.objc_getClass.restype = c_void_p
631635
objc.sel_registerName.restype = c_void_p
@@ -652,18 +656,26 @@ def sel(name):
652656
NSWorkspace = cls(b'NSWorkspace')
653657
objc.objc_msgSend.argtypes = [c_void_p, c_void_p]
654658
workspace = objc.objc_msgSend(NSWorkspace, sel(b'sharedWorkspace'))
659+
if not workspace:
660+
return None
655661

656662
objc.objc_msgSend.argtypes = [c_void_p, c_void_p, c_void_p]
657663
app_url = objc.objc_msgSend(
658664
workspace, sel(b'URLForApplicationToOpenURL:'), probe_url
659665
)
666+
if not app_url:
667+
return None
660668

661669
# Get bundle identifier from that app's NSBundle
662670
NSBundle = cls(b'NSBundle')
663671
bundle = objc.objc_msgSend(NSBundle, sel(b'bundleWithURL:'), app_url)
672+
if not bundle:
673+
return None
664674

665675
objc.objc_msgSend.argtypes = [c_void_p, c_void_p]
666676
bundle_id_ns = objc.objc_msgSend(bundle, sel(b'bundleIdentifier'))
677+
if not bundle_id_ns:
678+
return None
667679

668680
objc.objc_msgSend.restype = c_char_p
669681
bundle_id_bytes = objc.objc_msgSend(bundle_id_ns, sel(b'UTF8String'))

0 commit comments

Comments
 (0)