Skip to content

Commit aac89b5

Browse files
gh-125206: Bug in ctypes with old libffi is fixed (#125322)
Workaround for old libffi versions is added. Module ctypes now supports C11 double complex only with libffi >= 3.3.0. Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 54c6fcb commit aac89b5

File tree

10 files changed

+121
-16
lines changed

10 files changed

+121
-16
lines changed

Lib/test/test_ctypes/test_libc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_sqrt(self):
2323
self.assertEqual(lib.my_sqrt(2.0), math.sqrt(2.0))
2424

2525
@unittest.skipUnless(hasattr(ctypes, "c_double_complex"),
26-
"requires C11 complex type")
26+
"requires C11 complex type and libffi >= 3.3.0")
2727
def test_csqrt(self):
2828
lib.my_csqrt.argtypes = ctypes.c_double_complex,
2929
lib.my_csqrt.restype = ctypes.c_double_complex
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Workaround for old libffi versions is added. Module ctypes supports
2+
:c:expr:`double complex` only with libffi >= 3.3.0. Patch by Mikhail Efimov.

Modules/_ctypes/_ctypes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ class _ctypes.c_void_p "PyObject *" "clinic_state_sub()->PyCSimpleType_Type"
17471747
[clinic start generated code]*/
17481748
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=dd4d9646c56f43a9]*/
17491749

1750-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
1750+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
17511751
static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdCEFfuzZqQPXOv?g";
17521752
#else
17531753
static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdfuzZqQPXOv?g";

Modules/_ctypes/_ctypes_test.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
#include <Python.h>
1515

16-
#include <ffi.h> // FFI_TARGET_HAS_COMPLEX_TYPE
17-
18-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
16+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
1917
# include "../_complex.h" // csqrt()
2018
# undef I // for _ctypes_test_generated.c.h
2119
#endif
@@ -449,7 +447,7 @@ EXPORT(double) my_sqrt(double a)
449447
return sqrt(a);
450448
}
451449

452-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
450+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
453451
EXPORT(double complex) my_csqrt(double complex a)
454452
{
455453
return csqrt(a);

Modules/_ctypes/callproc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module _ctypes
105105
#include "pycore_global_objects.h"// _Py_ID()
106106
#include "pycore_traceback.h" // _PyTraceback_Add()
107107

108-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
108+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
109109
#include "../_complex.h" // complex
110110
#endif
111111

@@ -655,7 +655,7 @@ union result {
655655
double d;
656656
float f;
657657
void *p;
658-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
658+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
659659
double complex C;
660660
float complex E;
661661
long double complex F;

Modules/_ctypes/cfield.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <ffi.h>
1515
#include "ctypes.h"
1616

17-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
17+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
1818
# include "../_complex.h" // complex
1919
#endif
2020

@@ -972,7 +972,7 @@ d_get(void *ptr, Py_ssize_t size)
972972
return PyFloat_FromDouble(val);
973973
}
974974

975-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
975+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
976976
static PyObject *
977977
C_set(void *ptr, PyObject *value, Py_ssize_t size)
978978
{
@@ -1545,7 +1545,7 @@ static struct fielddesc formattable[] = {
15451545
{ 'B', B_set, B_get, NULL},
15461546
{ 'c', c_set, c_get, NULL},
15471547
{ 'd', d_set, d_get, NULL, d_set_sw, d_get_sw},
1548-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
1548+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
15491549
{ 'C', C_set, C_get, NULL},
15501550
{ 'E', E_set, E_get, NULL},
15511551
{ 'F', F_set, F_get, NULL},
@@ -1600,7 +1600,7 @@ _ctypes_init_fielddesc(void)
16001600
case 'B': fd->pffi_type = &ffi_type_uchar; break;
16011601
case 'c': fd->pffi_type = &ffi_type_schar; break;
16021602
case 'd': fd->pffi_type = &ffi_type_double; break;
1603-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
1603+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
16041604
case 'C': fd->pffi_type = &ffi_type_complex_double; break;
16051605
case 'E': fd->pffi_type = &ffi_type_complex_float; break;
16061606
case 'F': fd->pffi_type = &ffi_type_complex_longdouble; break;

Modules/_ctypes/ctypes.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
# include <alloca.h>
33
#endif
44

5-
#include <ffi.h> // FFI_TARGET_HAS_COMPLEX_TYPE
6-
75
#include "pycore_moduleobject.h" // _PyModule_GetState()
86
#include "pycore_typeobject.h" // _PyType_GetModuleState()
97

10-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
8+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
119
# include "../_complex.h" // complex
1210
#endif
1311

@@ -388,7 +386,7 @@ struct tagPyCArgObject {
388386
double d;
389387
float f;
390388
void *p;
391-
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE)
389+
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
392390
double complex C;
393391
float complex E;
394392
long double complex F;

configure

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+40
Original file line numberDiff line numberDiff line change
@@ -4089,6 +4089,46 @@ AS_VAR_IF([have_libffi], [yes], [
40894089
])
40904090
])
40914091

4092+
# Check for libffi with real complex double support.
4093+
# This is a workaround, since FFI_TARGET_HAS_COMPLEX_TYPE was defined in libffi v3.2.1,
4094+
# but real support was provided only in libffi v3.3.0.
4095+
# See https://github.com/python/cpython/issues/125206 for more details.
4096+
#
4097+
AC_CACHE_CHECK([libffi has complex type support], [ac_cv_ffi_complex_double_supported],
4098+
[ac_save_cc="$CC"
4099+
CC="$CC -lffi"
4100+
AC_RUN_IFELSE([AC_LANG_SOURCE([[
4101+
#include <complex.h>
4102+
#include <ffi.h>
4103+
int z_is_expected(double complex z)
4104+
{
4105+
const double complex expected = CMPLX(1.25, -0.5);
4106+
return z == expected;
4107+
}
4108+
int main(void)
4109+
{
4110+
double complex z = 1.25 - 0.5 * I;
4111+
ffi_type *args[1] = {&ffi_type_complex_double};
4112+
void *values[1] = {&z};
4113+
ffi_cif cif;
4114+
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
4115+
&ffi_type_sint, args) != FFI_OK)
4116+
{
4117+
return 2;
4118+
}
4119+
ffi_arg rc;
4120+
ffi_call(&cif, FFI_FN(z_is_expected), &rc, values);
4121+
return !rc;
4122+
}
4123+
]])], [ac_cv_ffi_complex_double_supported=yes],
4124+
[ac_cv_ffi_complex_double_supported=no],
4125+
[ac_cv_ffi_complex_double_supported=no])
4126+
CC="$ac_save_cc"])
4127+
if test "$ac_cv_ffi_complex_double_supported" = "yes"; then
4128+
AC_DEFINE([Py_FFI_SUPPORT_C_COMPLEX], [1],
4129+
[Defined if _Complex C type can be used with libffi.])
4130+
fi
4131+
40924132
# Check for use of the system libmpdec library
40934133
AC_MSG_CHECKING([for --with-system-libmpdec])
40944134
AC_ARG_WITH(

pyconfig.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,9 @@
16851685
/* Defined if Python is built as a shared library. */
16861686
#undef Py_ENABLE_SHARED
16871687

1688+
/* Defined if _Complex C type can be used with libffi. */
1689+
#undef Py_FFI_SUPPORT_C_COMPLEX
1690+
16881691
/* Define if you want to disable the GIL */
16891692
#undef Py_GIL_DISABLED
16901693

0 commit comments

Comments
 (0)