8000 gh-61103: support double complex (_Complex) type in ctypes by skirpichev · Pull Request #120894 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-61103: support double complex (_Complex) type in ctypes #120894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Jul 1, 2024

Conversation

skirpichev
Copy link
Member
@skirpichev skirpichev commented Jun 23, 2024

Example:

>>> import ctypes
>>> libm = ctypes.CDLL('libm.so.6')
>>> libm.clog.argtypes = [ctypes.c_double_complex]
>>> libm.clog.restype = ctypes.c_double_complex
>>> libm.clog(1+1j)
(0.34657359027997264+0.7853981633974483j)

ctypes.c_double_complex is available only if compiler does support complex arithmetic (Annex G).


📚 Documentation preview 📚: https://cpython-previews--120894.org.readthedocs.build/



Notes for reviewers:

  • It seems, most compilers implement this optional feature of C11+. (Though neither does this correctly.) Maybe we should require one?
  • only one complex type was added; if this looks good - I can add the rest (float complex and long double complex) in this pr or in other.
  • c_double_complex._type_ chosen to be "C". Maybe we should allow instead multiple chars to specify types (e.g. "Cd" for double complex)?
  • this pr doesn't include changes in the struct module. Perhaps, I should add support for complex type here as well?
  • if test_complex_round_trip() does make sense for reviewers, here is a simple refactoring to avoid adding yet another assertFloatsAreIdentical() helper: gh-121039: add Floats/ComplexesAreIdenticalMixin to test.support.testcase #121071

Example:
```pycon
>>> import ctypes
>>> ctypes.__STDC_IEC_559_COMPLEX__
1
>>> libm = ctypes.CDLL('libm.so.6')
>>> libm.clog.argtypes = [ctypes.c_double_complex]
>>> libm.clog.restype = ctypes.c_double_complex
>>> libm.clog(1+1j)
(0.34657359027997264+0.7853981633974483j)
```

``ctypes.__STDC_IEC_559_COMPLEX__`` is ``0`` if compiler doesn't support
complex arithmetic (Annex G).
skirpichev and others added 2 commits June 23, 2024 09:00
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
@skirpichev skirpichev requested a review from nineteendo June 23, 2024 09:36
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>