Skip to content

Commit 918fb3a

Browse files
authored
gh-156112: Load the ObjC runtime by name when find_library() cannot resolve it (#156113)
Provides a fallback name for the ObjC runtime library on iOS, and improved error handling when that library cannot be loaded.
1 parent d856402 commit 918fb3a

2 files changed

Lines changed: 12 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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`!_ios_support` no longer fails to import when
2+
:func:`ctypes.util.find_library` cannot resolve the ObjC runtime through the
3+
dyld shared cache, as is the case on iOS 13. The runtime is now loaded by name,
4+
which dyld resolves against the cache directly.

0 commit comments

Comments
 (0)