GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-61103: support double complex (_Complex) type in ctypes by skirpichev · Pull Request #120894 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Jun 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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).

  • Issue: Add support for C99 complex type (_Complex) as ctypes.c_complex #61103

📚 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
pythongh-61103: support double complex (_Complex) type in ctypes
c9b872f
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).
@bedevere-app bedevere-app Bot mentioned this pull request Jun 23, 2024
Add support for C99 complex type (_Complex) as ctypes.c_complex #61103
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Jun 23, 2024
skirpichev added 3 commits June 23, 2024 08:15
@skirpichev
+ ifdef
b402300
@skirpichev
+ fix rst formatting
ad39e6d
@skirpichev
+ CMPLX
fd9135b
nineteendo
nineteendo reviewed Jun 23, 2024
View reviewed changes
Comment thread Lib/test/test_ctypes/test_libc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_libc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev and others added 2 commits June 23, 2024 09:00
@skirpichev @nineteendo
Apply suggestions from code review
c296fb3
Co-authored-by: Nice Zombies <[email protected]>
@skirpichev
+ support _Complex in c-analyzer
7453ca5
@skirpichev
skirpichev requested a review from ericsnowcurrently as a code owner June 23, 2024 07:12
skirpichev added 4 commits June 23, 2024 10:47
@skirpichev
+1
e444c9c
@skirpichev
expand docs
2e90643
@skirpichev
drop ctypes.__STDC_IEC_559_COMPLEX__
32ffacf
@skirpichev
And more tests, finally)
9163950
@skirpichev
skirpichev requested a review from nineteendo June 23, 2024 09:36
nineteendo
nineteendo reviewed Jun 23, 2024
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev @nineteendo
Apply suggestions from code review
bf81682
Co-authored-by: Nice Zombies <[email protected]>
picnixz
picnixz reviewed Jun 23, 2024
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2024-06-23-07-23-08.gh-issue-61103.ca_U_l.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/_ctypes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/cfield.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tools/c-analyzer/c_parser/parser/_regexes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_ctypes/cfield.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Jun 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I can add the rest (float complex and long double complex) in this pr or in other.

I'd prefer having a separate PR because we might need aliases in case sizeof(long double) == sizeof(double) (the same technique that is used for long long and long IIRC)

c_double_complex.type chosen to be "C". Maybe we should allow instead multiple chars to specify types (e.g. "Cd" for double complex)?

I think it might be indeed a good alternative, because otherwise you need to find good 1-char names for float complex and long double complex in the future, but I think Cf, Cd and Cl (or Cq for quad-precision) are good enough. But for that we need to update the fielddesc structure with an additional field that would be a "modifier" of some sort or allow the "code" to be more than just a single character.

this pr doesn't include changes in the struct module. Perhaps, I should add support for complex type here as well?

I'd say in a separate PR since there is a translation of ctypes format to PEP 3118 names (where complex numbers are represented by 'Z').

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev and others added 2 commits June 23, 2024 14:47
@skirpichev @picnixz
Apply suggestions from code review
af2fdce
Co-authored-by: Bénédikt Tran <[email protected]>
@skirpichev
+ allow _Complex specifier before float/double/etc
6d9d5ab
@skirpichev
skirpichev force-pushed the ctypes-c_complex-61103 branch from 45f8544 to 013765e Compare June 23, 2024 13:07
@skirpichev
+ comment on clang workaround
f9c709c
@skirpichev
skirpichev force-pushed the ctypes-c_complex-61103 branch from 013765e to f9c709c Compare June 23, 2024 17:33
@nineteendo

nineteendo commented Jun 23, 2024

Copy link
Copy Markdown
Contributor

Use a force-push only to fix typo's or revert a merge.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 23, 2024

Copy link
Copy Markdown
Member Author

Use a force-push only to fix typo's or revert a merge.

Thank you for advice. Usually I don't do this. I hope this time force-push was OK, as it's restricted to unreviewed commits.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nineteendo

nineteendo commented Jun 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

It's less bad if no-one commented on those commits, but it's still more work to figure out what you changed.
It's also more distracting than a regular commit. (also, you don't need to quote the whole previous message)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
Address review:
ccd418d
* add configure test for _Complex type
* define HAVE_C_COMPLEX
* move workarounds for buggy implementations to Module/_complex.h
@skirpichev
skirpichev requested review from corona10 and erlend-aasland as code owners June 24, 2024 03:47
@bedevere-bot

bedevere-bot commented Jun 30, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit e9cab4a 🤖

The command will test the builders whose names match following regular expression: s390x SLES

The builders matched are:

  • s390x SLES PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Well, one failure seems to be related:
https://buildbot.python.org/all/#/builders/1262/builds/13/steps/3/logs/stdio
Edit (similar one):
https://buildbot.python.org/all/#/builders/855/builds/119

Looking on the libffi sources, it seems that FFI_TYPE_COMPLEX is available unconditionally, even if platform doesn't support interfaces to _Complex (for sparc it works since v3.3). We should check FFI_TARGET_HAS_COMPLEX_TYPE instead.

I'll update PR when build finish.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
sed: FFI_TYPE_COMPLEX -> FFI_TARGET_HAS_COMPLEX_TYPE
6d5bf66
@vstinner

vstinner commented Jun 30, 2024

Copy link
Copy Markdown
Member

!buildbot AMD64 Fedora Rawhide PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jun 30, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 6d5bf66 🤖

The command will test the builders whose names match following regular expression: AMD64 Fedora Rawhide PR

The builders matched are:

  • AMD64 Fedora Rawhide PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 30, 2024

Copy link
Copy Markdown
Member

!buildbot AMD64 Fedora Rawhide PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jun 30, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 6d5bf66 🤖

The command will test the builders whose names match following regular expression: AMD64 Fedora Rawhide PR

The builders matched are:

  • AMD64 Fedora Rawhide PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 30, 2024

Copy link
Copy Markdown
Member

Oh, Rawhide failure is unrelated: "No space left on device".

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 30, 2024

Copy link
Copy Markdown
Member

!buildbot PPC64LE CentOS9 PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jun 30, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 6d5bf66 🤖

The command will test the builders whose names match following regular expression: PPC64LE CentOS9 PR

The builders matched are:

  • PPC64LE CentOS9 PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 30, 2024

Copy link
Copy Markdown
Member

!buildbot PPC64LE Fedora Stable Clang PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jun 30, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 6d5bf66 🤖

The command will test the builders whose names match following regular expression: PPC64LE Fedora Stable Clang PR

The builders matched are:

  • PPC64LE Fedora Stable Clang PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 1, 2024
@bedevere-bot

bedevere-bot commented Jul 1, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 6d5bf66 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 1, 2024
@vstinner
vstinner merged commit 6988ff0 into python:main Jul 1, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Jul 1, 2024
@vstinner

vstinner commented Jul 1, 2024

Copy link
Copy Markdown
Member

Thanks @skirpichev, I merged your PR.

I checked remaining buildbot failures: they are unrelated to the change.

@skirpichev: @erlend-aasland has concerns about relative #include.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev deleted the ctypes-c_complex-61103 branch July 1, 2024 09:56
@skirpichev

skirpichev commented Jul 1, 2024

Copy link
Copy Markdown
Member Author

Thanks for reviews and patience.

@erlend-aasland has concerns about relative #include.

Yep, here; I'll open an issue (I think that build failures in this pr already don't need this;)). There are other includes, besides _math.h.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Jul 1, 2024
Should we allow relative includes? (i.e. #include "../_ssl.h") #121223
Closed
Akasurde pushed a commit to Akasurde/cpython that referenced this pull request Jul 3, 2024
@skirpichev @nineteendo
@picnixz @vstinner @Akasurde
pythongh-61103: Support double complex (_Complex) type in ctypes (pyt…
704c35e
…hon#120894)

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)
```

Co-authored-by: Nice Zombies <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@skirpichev skirpichev mentioned this pull request Jul 10, 2024
gh-121039: add Floats/ComplexesAreIdenticalMixin to test.support.testcase #121071
Merged
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
@skirpichev @nineteendo
@picnixz @vstinner @estyxx
pythongh-61103: Support double complex (_Complex) type in ctypes (pyt…
b3f91a6
…hon#120894)

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)
```

Co-authored-by: Nice Zombies <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
This was referenced Oct 7, 2024
gh-121249: Support _Complex types in the struct module #121613
Merged
CTypes test failed, complex double problem #125206
Closed
gh-125206: Bug in ctypes with old libffi is fixed #125322
Merged
@skirpichev skirpichev mentioned this pull request Jan 22, 2025
implement PEP 3118 struct changes #47382
Open
@skirpichev skirpichev mentioned this pull request Apr 13, 2026
gh-146151: support for complex arrays in the array module #146237
Merged
@freakboy3742 freakboy3742 mentioned this pull request Jun 15, 2026
gh-151422: Don't link libffi into _ctypes_test.so #151423
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@picnixz picnixz picnixz approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
+1 more reviewer
@nineteendo nineteendo nineteendo left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@skirpichev @picnixz @nineteendo @vstinner @bedevere-bot @erlend-aasland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.