Repository navigation
Add support for C99 complex type (_Complex) to the struct module #121249
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Jul 2, 2024 Implemented by change 7487db4.
IMO, we should provide a support for long double complex in struct module.
For now there is a little inconvience:-> % cat struct_calcsize_complex.py import struct print(struct.calcsize("d")) print(struct.calcsize("C")) print(struct.calcsize("E")) print(struct.calcsize("F"))-> % ./python struct_calcsize_complex.py 8 16 8 Traceback (most recent call last): File "/home/mikhail.efimov/projects/cpython/struct_calcsize_complex.py", line 5, in <module> print(struct.calcsize("F")) ~~~~~~~~~~~~~~~^^^^^ struct.error: bad char in struct formatwe should provide a support for long double complex in struct module.
But we don't have support for long double here. I don't know for sure why it wasn't here historically, will dig into it.
Probably together with support for long double - a support for long double complex will also make sense.
Even long double is not supported?
Probably, there is not by intention, just because a lack of time.It seems like support for long double complex now is partial.
In come cases they are allowed (call csqrtl, for example), but in other they aren't (struct module).
I've just wanted to emphasize this.Probably, there is not by intention
Maybe is. The long double is a more complex beast: https://en.wikipedia.org/wiki/Long_double
I would prefer to not support long double, it looks complicated for little usage.
@vstinner, I don't think it's too complicated. On technical side we, probably, have to add only PyFloat_Pack/Unpack16() and PyFloat_Pack/Unpack10() functions for the IEEE 754 quadruples and the x86 extended precision format. Say, "m" and "o" format codes for the struct module.
- "m" - like "f" and "d" will use IEEE binary128 for the packed representation;
- "o" - like "e" will use x86 extended precision format for native conversion as well.
These are popular standards and it's nice to have conversion helpers, available in the Python.
The "F" format for the long double complex type will be implemented like "E" and "C", i.e. using IEEE binary128.
If that sounds, I'll open an issue. Hardly discussion on d.p.o does make sense.
Reacted by Mikhail Efimov- added a commit that references this issue
on Mar 29, 2025 26 remaining items
I think this is done. The ctypes module has own issue.
@skirpichev: All PRs attached to this issues are merged. Can we close this issue?
I think so :-)
Reacted by Victor Stinner- added a commit that references this issue
on Jul 1, 2026 - moved this to Todo in Struct, memoryview and array issues 🏗️
on Jul 13, 2026 - moved this from Todo to Done in Struct, memoryview and array issues 🏗️
on Jul 13, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Feature or enhancement
Proposal:
The struct module has support for
floatanddoubletypes, so at least there should be alsofloat _Complexanddouble _Complex. I'll work on a patch.Initial version
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
complextypes instruct#132864