From 5ebade0e5c06852823debf88ae4d58dca4504b36 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Mon, 1 Nov 2021 08:29:17 +0100 Subject: [PATCH 1/3] bpo-45598: Remove ffi header header libffi supports ffi_prep_ciff, ffi_prep_closure_loc, and ffi_closure_alloc for more than a decade. Assume that the feature exists on supported platforms and only keep checks for old macOS libffi. Signed-off-by: Christian Heimes --- .../2021-11-01-08-43-12.bpo-45598.cAHNki.rst | 3 +++ Modules/_ctypes/callbacks.c | 5 +---- Modules/_ctypes/callproc.c | 19 +++++-------------- Modules/_ctypes/malloc_closure.c | 4 ---- setup.py | 16 ---------------- 5 files changed, 9 insertions(+), 38 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2021-11-01-08-43-12.bpo-45598.cAHNki.rst diff --git a/Misc/NEWS.d/next/Build/2021-11-01-08-43-12.bpo-45598.cAHNki.rst b/Misc/NEWS.d/next/Build/2021-11-01-08-43-12.bpo-45598.cAHNki.rst new file mode 100644 index 00000000000000..c1212dd111d605 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2021-11-01-08-43-12.bpo-45598.cAHNki.rst @@ -0,0 +1,3 @@ +ctypes now assumes that libffi provides ffi_prep_ciff, ffi_prep_closure_loc, +and ffi_closure_alloc functions, except when compiling with older macOS +libffi. diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 0f7789a973e8f2..67fbff0659d30f 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -417,7 +417,6 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable, "ffi_prep_cif failed with %d", result); goto error; } -#if HAVE_FFI_PREP_CLOSURE_LOC # if USING_APPLE_OS_LIBFFI # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) # else @@ -427,9 +426,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable, result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn, p, p->pcl_exec); - } else -#endif - { + } else { #if USING_APPLE_OS_LIBFFI && defined(__arm64__) PyErr_Format(PyExc_NotImplementedError, "ffi_prep_closure_loc() is missing"); goto error; diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index e2204961070dbf..94748725535e38 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -828,10 +828,8 @@ static int _call_function_pointer(int flags, # if USING_APPLE_OS_LIBFFI # define HAVE_FFI_PREP_CIF_VAR_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) -# elif HAVE_FFI_PREP_CIF_VAR -# define HAVE_FFI_PREP_CIF_VAR_RUNTIME true # else -# define HAVE_FFI_PREP_CIF_VAR_RUNTIME false +# define HAVE_FFI_PREP_CIF_VAR_RUNTIME true # endif /* Even on Apple-arm64 the calling convention for variadic functions coincides @@ -843,16 +841,12 @@ static int _call_function_pointer(int flags, (void) is_variadic; #if defined(__APPLE__) && defined(__arm64__) - if (is_variadic) { - if (HAVE_FFI_PREP_CIF_VAR_RUNTIME) { - } else { - PyErr_SetString(PyExc_NotImplementedError, "ffi_prep_cif_var() is missing"); - return -1; - } + if (is_variadic && !HAVE_FFI_PREP_CIF_VAR_RUNTIME) { + PyErr_SetString(PyExc_NotImplementedError, "ffi_prep_cif_var() is missing"); + return -1; } #endif -#if HAVE_FFI_PREP_CIF_VAR if (is_variadic) { if (HAVE_FFI_PREP_CIF_VAR_RUNTIME) { if (FFI_OK != ffi_prep_cif_var(&cif, @@ -876,10 +870,7 @@ static int _call_function_pointer(int flags, return -1; } } - } else -#endif - - { + } else { if (FFI_OK != ffi_prep_cif(&cif, cc, argcount, diff --git a/Modules/_ctypes/malloc_closure.c b/Modules/_ctypes/malloc_closure.c index 788bae6a96c7f4..733347a64876d5 100644 --- a/Modules/_ctypes/malloc_closure.c +++ b/Modules/_ctypes/malloc_closure.c @@ -91,7 +91,6 @@ static void more_core(void) /* put the item back into the free list */ void Py_ffi_closure_free(void *p) { -#if HAVE_FFI_CLOSURE_ALLOC #if USING_APPLE_OS_LIBFFI if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) { #endif @@ -99,7 +98,6 @@ void Py_ffi_closure_free(void *p) return; #if USING_APPLE_OS_LIBFFI } -#endif #endif ITEM *item = (ITEM *)p; item->next = free_list; @@ -109,14 +107,12 @@ void Py_ffi_closure_free(void *p) /* return one item from the free list, allocating more if needed */ void *Py_ffi_closure_alloc(size_t size, void** codeloc) { -#if HAVE_FFI_CLOSURE_ALLOC #if USING_APPLE_OS_LIBFFI if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) { #endif return ffi_closure_alloc(size, codeloc); #if USING_APPLE_OS_LIBFFI } -#endif #endif ITEM *item; if (!free_list) diff --git a/setup.py b/setup.py index 22c5ccf0a073a9..25e889844b08e1 100644 --- a/setup.py +++ b/setup.py @@ -238,14 +238,6 @@ def is_macosx_sdk_path(path): or path.startswith('/System/iOSSupport') ) -def grep_headers_for(function, headers): - for header in headers: - with open(header, 'r', errors='surrogateescape') as f: - if function in f.read(): - return True - return False - - def find_file(filename, std_dirs, paths): """Searches for the directory where a given file is located, and returns a possibly-empty list of additional directories, or None @@ -2280,14 +2272,6 @@ def detect_ctypes(self): break if ffi_inc and ffi_lib: - ffi_headers = glob(os.path.join(ffi_inc, '*.h')) - if grep_headers_for('ffi_prep_cif_var', ffi_headers): - ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1") - if grep_headers_for('ffi_prep_closure_loc', ffi_headers): - ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1") - if grep_headers_for('ffi_closure_alloc', ffi_headers): - ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1") - ext.include_dirs.append(ffi_inc) ext.libraries.append(ffi_lib) self.use_system_libffi = True From d67f0137bf98b35b66d664da105dd65e3834fe75 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 3 Nov 2021 14:05:20 +0100 Subject: [PATCH 2/3] Try Windows builds with some functions disabled --- Modules/_ctypes/callbacks.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 67fbff0659d30f..213d92946f129f 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -419,6 +419,8 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable, } # if USING_APPLE_OS_LIBFFI # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) +# elif defined(MS_WIN32) +# define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME 0 # else # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME 1 # endif From 7298cc38b8b46aea0a138e7c4a5940f617499d5e Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 3 Nov 2021 14:21:54 +0100 Subject: [PATCH 3/3] HAVE_FFI_PREP_CIF_VAR_RUNTIME --- Modules/_ctypes/callproc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 94748725535e38..95f4f23aa3afe4 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -828,6 +828,8 @@ static int _call_function_pointer(int flags, # if USING_APPLE_OS_LIBFFI # define HAVE_FFI_PREP_CIF_VAR_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) +# elif defined(MS_WIN32) +# define HAVE_FFI_PREP_CIF_VAR_RUNTIME false # else # define HAVE_FFI_PREP_CIF_VAR_RUNTIME true # endif