Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
Python binary has been built in the standard way:
./configure --with-pydebug && make -jBut test_ctypes fails:
-> % ./python -m unittest -v test.test_ctypes.test_libc.LibTest.test_csqrt test_csqrt (test.test_ctypes.test_libc.LibTest.test_csqrt) ... FAIL======================================================================FAIL: test_csqrt (test.test_ctypes.test_libc.LibTest.test_csqrt)----------------------------------------------------------------------Traceback (most recent call last): File "/home/mikhail.efimov/projects/cpython/Lib/test/test_ctypes/test_libc.py", line 30, in test_csqrt self.assertEqual(lib.my_csqrt(4), 2+0j) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^AssertionError: (5e-324+6.95324111713477e-310j) != (2+0j)----------------------------------------------------------------------Ran 1 test in 0.001sFAILED (failures=1)System:
-> % cat /etc/issueDebian GNU/Linux 10 \n \lGCC:
-> % gcc -vUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapperOFFLOAD_TARGET_NAMES=nvptx-noneOFFLOAD_TARGET_DEFAULT=1Target: x86_64-linux-gnuConfigured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnuThread model: posixgcc version 8.3.0 (Debian 8.3.0-6)I've tried to fix it by myself but the result has not been achieved in a reasonable amount of time.
There is a simple test I've provided:
-> % cat test_csqrt.c#include <complex.h>#include <stdio.h>int complex my_csqrt(double complex a){ double complex z1 = a; double complex z2 = csqrt(a); printf("my_csqrt (%.10f%+.10fi) = %.10f%+.10fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2)); return 0;}int main() { my_csqrt(4.0); my_csqrt(4.0+4.0j); my_csqrt(-1+0.01j); my_csqrt(-1-0.01j); return 0;}-> % gcc -lm test_csqrt.c -o test_csqrt && ./test_csqrtmy_csqrt (4.0000000000+0.0000000000i) = 2.0000000000+0.0000000000imy_csqrt (4.0000000000+4.0000000000i) = 2.1973682269+0.9101797211imy_csqrt (-1.0000000000+0.0100000000i) = 0.0049999375+1.0000124996imy_csqrt (-1.0000000000-0.0100000000i) = 0.0049999375-1.0000124996iSo, it's not a problem in my libc version.
Moreover, this problem can be reproduced with standard libm.so (/lib/x86_64-linux-gnu/libm-2.28.so):
-> % cat ctypes_fix/test.py import ctypeslibm = ctypes.CDLL('libm.so.6')libm.clog.restype = ctypes.c_double_complexlibm.clog.argtypes = ctypes.c_double_complex,clog_5 = libm.clog(5.0)clog_1000_2j = libm.clog(1000.0+2j)print(f"{clog_5=}")print(f"{clog_1000_2j=}")-> % ./python ctypes_fix/test.pyclog_5=(5e-324+6.9529453382261e-310j)clog_1000_2j=(5e-324+6.9529453382261e-310j)IMHO, some problem lies in using ctypes.c_double_complex as an argument and return value types.
FYI, with double argtype and restype clog works like classical double log:
-> % cat ctypes_fix/test2.py import ctypeslibm = ctypes.CDLL('libm.so.6')libm.clog.restype = ctypes.c_doublelibm.clog.argtypes = ctypes.c_double,clog_5 = libm.clog(5.0)clog_1000 = libm.clog(1000.0)print(f"{clog_5=}")print(f"{clog_1000=}")-> % ./python ctypes_fix/test2.pyclog_5=1.6094379124341003clog_1000=6.907755278982137CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-125206: Bug in ctypes with old libffi is fixed #125322
- gh-125206: Correct detection of complex numbers support in libffi #126104
- gh-125206: don't use CMPLX in configure test #132865
- gh-125206: Make _Py_FFI_SUPPORT_C_COMPLEX private #135932
- [3.14] gh-125206: Make _Py_FFI_SUPPORT_C_COMPLEX private (GH-135932) #135973