Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Lib/ctypes/macholib/dyld.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from ctypes.macholib.framework import framework_info
from ctypes.macholib.dylib import dylib_info
from itertools import *

try:
from _ctypes import _dyld_shared_cache_contains_path
from _ctypes import shared_library_is_loadable
except ImportError:
def _dyld_shared_cache_contains_path(*args):
def shared_library_is_loadable(path):
raise NotImplementedError

__all__ = [
Expand Down Expand Up @@ -122,19 +123,19 @@ def dyld_find(name, executable_path=None, env=None):
"""
Find a library or framework using dyld semantics
"""
for path in dyld_image_suffix_search(chain(
dyld_override_search(name, env),
dyld_executable_path_search(name, executable_path),
dyld_default_search(name, env),
), env):
paths = list(dyld_image_suffix_search(chain(
dyld_override_search(name, env),
dyld_executable_path_search(name, executable_path),
dyld_default_search(name, env),
), env))

for path in paths:
if os.path.isfile(path):
return path
try:
if _dyld_shared_cache_contains_path(path):
return path
except NotImplementedError:
pass

for path in reversed(paths):
if shared_library_is_loadable(path):
return path

raise ValueError("dylib %s could not be found" % (name,))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You no longer need to compile Python on the same version of MacOS that you will be running it on (Catalina vs Big Sur) in order for ctypes.util.find_library to work.
Comment thread
bergkvist marked this conversation as resolved.
Outdated
60 changes: 23 additions & 37 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@
#include "ctypes_dlfcn.h"
#endif

#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif

#ifdef MS_WIN32
#include <malloc.h>
#endif
Expand Down Expand Up @@ -1443,40 +1439,31 @@ copy_com_pointer(PyObject *self, PyObject *args)
}
#else

#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
{
PyObject *name, *name2;
char *name_str;

if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
int r;

if (!PyArg_ParseTuple(args, "O", &name))
return NULL;

if (name == Py_None)
Py_RETURN_FALSE;

if (PyUnicode_FSConverter(name, &name2) == 0)
return NULL;
name_str = PyBytes_AS_STRING(name2);
#ifndef MS_WIN32
static PyObject *py_shared_library_is_loadable(PyObject *self, PyObject *args) {
PyObject *name, *name2;
char *name_str;
void* handle;
Comment thread
bergkvist marked this conversation as resolved.
Outdated

r = _dyld_shared_cache_contains_path(name_str);
Py_DECREF(name2);
if (!PyArg_ParseTuple(args, "O", &name))
return NULL;

if (r) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
if (name == Py_None)
Py_RETURN_FALSE;

} else {
PyErr_SetString(PyExc_NotImplementedError, "_dyld_shared_cache_contains_path symbol is missing");
return NULL;
}
if (PyUnicode_FSConverter(name, &name2) == 0)
return NULL;
Comment thread
bergkvist marked this conversation as resolved.
Outdated

}
name_str = PyBytes_AS_STRING(name2);
handle = dlopen(name_str, RTLD_LAZY);
Py_DECREF(name2);
if (handle == NULL) {
Py_RETURN_FALSE;
} else {
Comment thread
bergkvist marked this conversation as resolved.
Outdated
dlclose(handle);
Py_RETURN_TRUE;
}
}
#endif

static PyObject *py_dl_open(PyObject *self, PyObject *args)
Expand Down Expand Up @@ -1991,9 +1978,8 @@ PyMethodDef _ctypes_module_methods[] = {
"dlopen(name, flag={RTLD_GLOBAL|RTLD_LOCAL}) open a shared library"},
{"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
{"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
#endif
#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
{"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
{"shared_library_is_loadable", py_shared_library_is_loadable, METH_VARARGS,
"check if shared library exists/is loadable with dlopen"},
#endif
{"alignment", align_func, METH_O, alignment_doc},
{"sizeof", sizeof_func, METH_O, sizeof_doc},
Expand Down