Skip to content

gh-121249: unconditionally support complex types in struct #132864

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 13 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Doc/library/struct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,6 @@ platform-dependent.
+--------+--------------------------+--------------------+----------------+------------+
| ``P`` | :c:expr:`void \*` | integer | | \(5) |
+--------+--------------------------+--------------------+----------------+------------+

Additionally, if IEC 60559 compatible complex arithmetic (Annex G of the
C11 standard) is supported, the following format characters are available:

+--------+--------------------------+--------------------+----------------+------------+
| Format | C Type | Python type | Standard size | Notes |
+========+==========================+====================+================+============+
| ``F`` | :c:expr:`float complex` | complex | 8 | \(10) |
+--------+--------------------------+--------------------+----------------+------------+
| ``D`` | :c:expr:`double complex` | complex | 16 | \(10) |
Expand Down Expand Up @@ -367,6 +360,12 @@ Notes:
For the ``'E'`` and ``'C'`` format characters, the packed representation uses
the IEEE 754 binary32 and binary64 format for components of the complex
number, regardless of the floating-point format used by the platform.
Note, that support for complex types is available unconditionally,
regardless on support IEC 60559 compatible complex arithmetic (Annex G of
the C11 standard) by the compiler. Each complex type interpreted as an
array type containing exactly two elements of the corresponding real type
(real and imaginary parts, respectively).


A format character may be preceded by an integral repeat count. For example,
the format string ``'4h'`` means exactly the same as ``'hhhh'``.
Expand Down
3 changes: 1 addition & 2 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,7 @@ struct
------

* Support the :c:expr:`float complex` and :c:expr:`double complex` C types in
the :mod:`struct` module (formatting characters ``'F'`` and ``'D'``,
respectively) if the compiler has C11 complex arithmetic.
the :mod:`struct` module (formatting characters ``'F'`` and ``'D'``.
(Contributed by Sergey B Kirpichev in :gh:`121249`.)


Expand Down
76 changes: 12 additions & 64 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#include "pycore_long.h" // _PyLong_AsByteArray()
#include "pycore_moduleobject.h" // _PyModule_GetState()

#ifdef Py_HAVE_C_COMPLEX
# include "_complex.h" // complex
#endif
#include <stddef.h> // offsetof()

/*[clinic input]
Expand Down Expand Up @@ -84,10 +81,8 @@ typedef struct { char c; int x; } st_int;
typedef struct { char c; long x; } st_long;
typedef struct { char c; float x; } st_float;
typedef struct { char c; double x; } st_double;
#ifdef Py_HAVE_C_COMPLEX
typedef struct { char c; float complex x; } st_float_complex;
typedef struct { char c; double complex x; } st_double_complex;
#endif
typedef struct { char c; float x[2]; } st_float_complex;
typedef struct { char c; double x[2]; } st_double_complex;
typedef struct { char c; void *x; } st_void_p;
typedef struct { char c; size_t x; } st_size_t;
typedef struct { char c; _Bool x; } st_bool;
Expand All @@ -97,10 +92,8 @@ typedef struct { char c; _Bool x; } st_bool;
#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
#ifdef Py_HAVE_C_COMPLEX
# define FLOAT_COMPLEX_ALIGN (sizeof(st_float_complex) - sizeof(float complex))
# define DOUBLE_COMPLEX_ALIGN (sizeof(st_double_complex) - sizeof(double complex))
#endif
#define FLOAT_COMPLEX_ALIGN (sizeof(st_float_complex) - 2*sizeof(float))
#define DOUBLE_COMPLEX_ALIGN (sizeof(st_double_complex) - 2*sizeof(double))
#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
#define SIZE_T_ALIGN (sizeof(st_size_t) - sizeof(size_t))
#define BOOL_ALIGN (sizeof(st_bool) - sizeof(_Bool))
Expand Down Expand Up @@ -529,25 +522,23 @@ nu_double(_structmodulestate *state, const char *p, const formatdef *f)
return PyFloat_FromDouble(x);
}

#ifdef Py_HAVE_C_COMPLEX
static PyObject *
nu_float_complex(_structmodulestate *state, const char *p, const formatdef *f)
{
float complex x;
float x[2];

memcpy(&x, p, sizeof(x));
return PyComplex_FromDoubles(creal(x), cimag(x));
return PyComplex_FromDoubles(x[0], x[1]);
}

static PyObject *
nu_double_complex(_structmodulestate *state, const char *p, const formatdef *f)
{
double complex x;
double x[2];

memcpy(&x, p, sizeof(x));
return PyComplex_FromDoubles(creal(x), cimag(x));
return PyComplex_FromDoubles(x[0], x[1]);
}
#endif

static PyObject *
nu_void_p(_structmodulestate *state, const char *p, const formatdef *f)
Expand Down Expand Up @@ -822,13 +813,12 @@ np_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
return 0;
}

#ifdef Py_HAVE_C_COMPLEX
static int
np_float_complex(_structmodulestate *state, char *p, PyObject *v,
const formatdef *f)
{
Py_complex c = PyComplex_AsCComplex(v);
float complex x = CMPLXF((float)c.real, (float)c.imag);
float x[2] = {(float)c.real, (float)c.imag};

if (c.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
Expand All @@ -844,7 +834,7 @@ np_double_complex(_structmodulestate *state, char *p, PyObject *v,
const formatdef *f)
{
Py_complex c = PyComplex_AsCComplex(v);
double complex x = CMPLX(c.real, c.imag);
double x[2] = {c.real, c.imag};

if (c.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
Expand All @@ -854,25 +844,6 @@ np_double_complex(_structmodulestate *state, char *p, PyObject *v,
memcpy(p, &x, sizeof(x));
return 0;
}
#else
static int
np_complex_stub(_structmodulestate *state, char *p, PyObject *v,
const formatdef *f)
{
PyErr_Format(state->StructError,
"'%c' format not supported on this system",
f->format);
return -1;
}
static PyObject *
nu_complex_stub(_structmodulestate *state, const char *p, const formatdef *f)
{
PyErr_Format(state->StructError,
"'%c' format not supported on this system",
f->format);
return NULL;
}
#endif

static int
np_void_p(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
Expand Down Expand Up @@ -912,13 +883,8 @@ static const formatdef native_table[] = {
{'e', sizeof(short), SHORT_ALIGN, nu_halffloat, np_halffloat},
{'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
{'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
#ifdef Py_HAVE_C_COMPLEX
{'F', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
{'D', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
#else
{'F', 1, 0, nu_complex_stub, np_complex_stub},
{'D', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{'F', 2*sizeof(float), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
{'D', 2*sizeof(double), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
{0}
};
Expand Down Expand Up @@ -1019,7 +985,6 @@ bu_double(_structmodulestate *state, const char *p, const formatdef *f)
return unpack_double(p, 0);
}

#ifdef Py_HAVE_C_COMPLEX
static PyObject *
bu_float_complex(_structmodulestate *state, const char *p, const formatdef *f)
{
Expand Down Expand Up @@ -1049,7 +1014,6 @@ bu_double_complex(_structmodulestate *state, const char *p, const formatdef *f)
}
return PyComplex_FromDoubles(x, y);
}
#endif

static PyObject *
bu_bool(_structmodulestate *state, const char *p, const formatdef *f)
Expand Down Expand Up @@ -1190,7 +1154,6 @@ bp_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
return PyFloat_Pack8(x, p, 0);
}

#ifdef Py_HAVE_C_COMPLEX
static int
bp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
Expand Down Expand Up @@ -1220,7 +1183,6 @@ bp_double_complex(_structmodulestate *state, char *p, PyObject *v, const formatd
}
return PyFloat_Pack8(x.imag, p + 8, 0);
}
#endif

static int
bp_bool(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
Expand Down Expand Up @@ -1252,13 +1214,8 @@ static formatdef bigendian_table[] = {
{'e', 2, 0, bu_halffloat, bp_halffloat},
{'f', 4, 0, bu_float, bp_float},
{'d', 8, 0, bu_double, bp_double},
#ifdef Py_HAVE_C_COMPLEX
{'F', 8, 0, bu_float_complex, bp_float_complex},
{'D', 16, 0, bu_double_complex, bp_double_complex},
#else
{'F', 1, 0, nu_complex_stub, np_complex_stub},
{'D', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{0}
};

Expand Down Expand Up @@ -1358,7 +1315,6 @@ lu_double(_structmodulestate *state, const char *p, const formatdef *f)
return unpack_double(p, 1);
}

#ifdef Py_HAVE_C_COMPLEX
static PyObject *
lu_float_complex(_structmodulestate *state, const char *p, const formatdef *f)
{
Expand Down Expand Up @@ -1388,7 +1344,6 @@ lu_double_complex(_structmodulestate *state, const char *p, const formatdef *f)
}
return PyComplex_FromDoubles(x, y);
}
#endif

static int
lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
Expand Down Expand Up @@ -1523,7 +1478,6 @@ lp_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
return PyFloat_Pack8(x, p, 1);
}

#ifdef Py_HAVE_C_COMPLEX
static int
lp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
Expand Down Expand Up @@ -1554,7 +1508,6 @@ lp_double_complex(_structmodulestate *state, char *p, PyObject *v, const formatd
}
return PyFloat_Pack8(x.imag, p + 8, 1);
}
#endif

static formatdef lilendian_table[] = {
{'x', 1, 0, NULL},
Expand All @@ -1576,13 +1529,8 @@ static formatdef lilendian_table[] = {
{'e', 2, 0, lu_halffloat, lp_halffloat},
{'f', 4, 0, lu_float, lp_float},
{'d', 8, 0, lu_double, lp_double},
#ifdef Py_HAVE_C_COMPLEX
{'F', 8, 0, lu_float_complex, lp_float_complex},
{'D', 16, 0, lu_double_complex, lp_double_complex},
#else
{'F', 1, 0, nu_complex_stub, np_complex_stub},
{'D', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{0}
};

Expand Down
Loading