8000 gh-121249: Support _Complex types in the struct module by skirpichev · Pull Request #121613 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121249: Support _Complex types in the struct module #121613

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 12 commits into from
Oct 7, 2024
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
Prev Previous commit
Next Next commit
raise a custom error on systems without native complex numbers
  • Loading branch information
skirpichev committed Jul 13, 2024
commit fec6be60230249a12476060843cb644aba347a7c
14 changes: 13 additions & 1 deletion Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,20 @@ def test_repr(self):
s = struct.Struct('=i2H')
self.assertEqual(repr(s), f'Struct({s.format!r})')

@unittest.skipUnless(have_c_complex, "requires C11 complex type")
def test_c_complex_round_trip(self):
if not have_c_complex:
msg1 = "'E' format not supported on this system"
msg2 = "'C' format not supported on this system"
with self.assertRaisesRegex(struct.error, msg1):
struct.pack('E', 1j)
with self.assertRaisesRegex(struct.error, msg1):
struct.unpack('E', b'1')
with self.assertRaisesRegex(struct.error, msg2):
struct.pack('C', 1j)
with self.assertRaisesRegex(struct.error, msg2):
struct.unpack('C', b'1')
return

values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2,
-3, INF, -INF, NAN], 2)]
for z in values:
Expand Down
27 changes: 27 additions & 0 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,24 @@ np_double_complex(_structmodulestate *state, char *p, PyObject *v,
memcpy(p, (char *)&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
Expand Down Expand Up @@ -897,6 +915,9 @@ static const formatdef native_table[] = {
#ifdef Py_HAVE_C_COMPLEX
{'E', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
{'C', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
#else
{'E', 1, 0, nu_complex_stub, np_complex_stub},
{'C', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
{0}
Expand Down Expand Up @@ -1236,6 +1257,9 @@ static formatdef bigendian_table[] = {
#ifdef Py_HAVE_C_COMPLEX
{'E', 8, 0, bu_float_complex, bp_float_complex},
{'C', 16, 0, bu_double_complex, bp_double_complex},
#else
{'E', 1, 0, nu_complex_stub, np_complex_stub},
{'C', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{0}
};
Expand Down Expand Up @@ -1559,6 +1583,9 @@ static formatdef lilendian_table[] = {
#ifdef Py_HAVE_C_COMPLEX
{'E', 8, 0, lu_float_complex, lp_float_complex},
{'C', 16, 0, lu_double_complex, lp_double_complex},
#else
{'E', 1, 0, nu_complex_stub, np_complex_stub},
{'C', 1, 0, nu_complex_stub, np_complex_stub},
#endif
{0}
};
Expand Down
Loading
0