Skip to content

Commit 4b55837

Browse files
bpo-44689: ctypes.util.find_library() now finds macOS 11+ system libraries when built on older macOS systems (GH-27251) (GH-28053)
Previously, when built on older macOS systems, `find_library` was not able to find macOS system libraries when running on Big Sur due to changes in how system libraries are stored. (cherry picked from commit 71853a7) Co-authored-by: Tobias Bergkvist <[email protected]>
1 parent d0f94ab commit 4b55837

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:meth:`ctypes.util.find_library` now works correctly on macOS 11 Big Sur
2+
even if Python is built on an older version of macOS. Previously, when
3+
built on older macOS systems, ``find_library`` was not able to find
4+
macOS system libraries when running on Big Sur due to changes in
5+
how system libraries are stored.

Modules/_ctypes/callproc.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,14 +1449,37 @@ copy_com_pointer(PyObject *self, PyObject *args)
14491449
return r;
14501450
}
14511451
#else
1452-
1452+
#ifdef __APPLE__
14531453
#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
1454+
#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
1455+
__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
1456+
#else
1457+
// Support the deprecated case of compiling on an older macOS version
1458+
static void *libsystem_b_handle;
1459+
static bool (*_dyld_shared_cache_contains_path)(const char *path);
1460+
1461+
__attribute__((constructor)) void load_dyld_shared_cache_contains_path(void) {
1462+
libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY);
1463+
if (libsystem_b_handle != NULL) {
1464+
_dyld_shared_cache_contains_path = dlsym(libsystem_b_handle, "_dyld_shared_cache_contains_path");
1465+
}
1466+
}
1467+
1468+
__attribute__((destructor)) void unload_dyld_shared_cache_contains_path(void) {
1469+
if (libsystem_b_handle != NULL) {
1470+
dlclose(libsystem_b_handle);
1471+
}
1472+
}
1473+
#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
1474+
_dyld_shared_cache_contains_path != NULL
1475+
#endif
1476+
14541477
static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
14551478
{
14561479
PyObject *name, *name2;
14571480
char *name_str;
14581481

1459-
if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
1482+
if (HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME) {
14601483
int r;
14611484

14621485
if (!PyArg_ParseTuple(args, "O", &name))
@@ -1999,7 +2022,7 @@ PyMethodDef _ctypes_module_methods[] = {
19992022
{"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
20002023
{"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
20012024
#endif
2002-
#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
2025+
#ifdef __APPLE__
20032026
{"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
20042027
#endif
20052028
{"alignment", align_func, METH_O, alignment_doc},

0 commit comments

Comments
 (0)