Skip to content

Commit 24f2513

Browse files
committed
Fall back to loading the ObjC runtime by name
ctypes.util.find_library() resolves a system library through the dyld shared cache, which requires _dyld_shared_cache_contains_path(). Modules/_ctypes/ callproc.c guards that symbol with __builtin_available(iOS 14.0), so on iOS 13 - the oldest release CPython's iOS support targets - find_library() returns None for any library that exists only in the cache, and importing _ios_support fails outright rather than degrading. dyld resolves a bare library name against the cache by itself, so use that when find_library() comes up empty. A genuine load failure is still reported as ImportError.
1 parent e8158d1 commit 24f2513

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

Lib/_ios_support.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
else:
1111
# ctypes is available. Load the ObjC library, and wrap the objc_getClass,
1212
# sel_registerName methods
13-
lib = util.find_library("objc")
14-
if lib is None:
15-
# Failed to load the objc library
16-
raise ImportError("ObjC runtime library couldn't be loaded")
13+
# find_library() resolves a system library through the dyld shared cache,
14+
# which needs _dyld_shared_cache_contains_path(); that is unavailable before
15+
# iOS 14, so fall back to the bare name, which dyld resolves by itself.
16+
lib = util.find_library("objc") or "libobjc.dylib"
1717

18-
objc = cdll.LoadLibrary(lib)
18+
try:
19+
objc = cdll.LoadLibrary(lib)
20+
except OSError:
21+
raise ImportError("ObjC runtime library couldn't be loaded")
1922
objc.objc_getClass.restype = c_void_p
2023
objc.objc_getClass.argtypes = [c_char_p]
2124
objc.sel_registerName.restype = c_void_p

0 commit comments

Comments
 (0)