Skip to content

Commit 41c87c4

Browse files
bpo-44689: ctypes.util.find_library() now finds macOS 11+ system libraries when built on older macOS systems (GH-27251)
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 66b8202 commit 41c87c4

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
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

+26-3
Original file line numberDiff line numberDiff line change
@@ -1442,14 +1442,37 @@ copy_com_pointer(PyObject *self, PyObject *args)
14421442
return r;
14431443
}
14441444
#else
1445-
1445+
#ifdef __APPLE__
14461446
#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
1447+
#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
1448+
__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
1449+
#else
1450+
// Support the deprecated case of compiling on an older macOS version
1451+
static void *libsystem_b_handle;
1452+
static bool (*_dyld_shared_cache_contains_path)(const char *path);
1453+
1454+
__attribute__((constructor)) void load_dyld_shared_cache_contains_path(void) {
1455+
libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY);
1456+
if (libsystem_b_handle != NULL) {
1457+
_dyld_shared_cache_contains_path = dlsym(libsystem_b_handle, "_dyld_shared_cache_contains_path");
1458+
}
1459+
}
1460+
1461+
__attribute__((destructor)) void unload_dyld_shared_cache_contains_path(void) {
1462+
if (libsystem_b_handle != NULL) {
1463+
dlclose(libsystem_b_handle);
1464+
}
1465+
}
1466+
#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
1467+
_dyld_shared_cache_contains_path != NULL
1468+
#endif
1469+
14471470
static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
14481471
{
14491472
PyObject *name, *name2;
14501473
char *name_str;
14511474

1452-
if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
1475+
if (HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME) {
14531476
int r;
14541477

14551478
if (!PyArg_ParseTuple(args, "O", &name))
@@ -1992,7 +2015,7 @@ PyMethodDef _ctypes_module_methods[] = {
19922015
{"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
19932016
{"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
19942017
#endif
1995-
#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
2018+
#ifdef __APPLE__
19962019
{"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
19972020
#endif
19982021
{"alignment", align_func, METH_O, alignment_doc},

0 commit comments

Comments
 (0)