Skip to content

Commit fec6be6

Browse files
committed
raise a custom error on systems without native complex numbers
1 parent e5e04c8 commit fec6be6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Lib/test/test_struct.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,20 @@ def test_repr(self):
812812
s = struct.Struct('=i2H')
813813
self.assertEqual(repr(s), f'Struct({s.format!r})')
814814

815-
@unittest.skipUnless(have_c_complex, "requires C11 complex type")
816815
def test_c_complex_round_trip(self):
816+
if not have_c_complex:
817+
msg1 = "'E' format not supported on this system"
818+
msg2 = "'C' format not supported on this system"
819+
with self.assertRaisesRegex(struct.error, msg1):
820+
struct.pack('E', 1j)
821+
with self.assertRaisesRegex(struct.error, msg1):
822+
struct.unpack('E', b'1')
823+
with self.assertRaisesRegex(struct.error, msg2):
824+
struct.pack('C', 1j)
825+
with self.assertRaisesRegex(struct.error, msg2):
826+
struct.unpack('C', b'1')
827+
return
828+
817829
values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2,
818830
-3, INF, -INF, NAN], 2)]
819831
for z in values:

Modules/_struct.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,24 @@ np_double_complex(_structmodulestate *state, char *p, PyObject *v,
854854
memcpy(p, (char *)&x, sizeof(x));
855855
return 0;
856856
}
857+
#else
858+
static int
859+
np_complex_stub(_structmodulestate *state, char *p, PyObject *v,
860+
const formatdef *f)
861+
{
862+
PyErr_Format(state->StructError,
863+
"'%c' format not supported on this system",
864+
f->format);
865+
return -1;
866+
}
867+
static PyObject *
868+
nu_complex_stub(_structmodulestate *state, const char *p, const formatdef *f)
869+
{
870+
PyErr_Format(state->StructError,
871+
"'%c' format not supported on this system",
872+
f->format);
873+
return NULL;
874+
}
857875
#endif
858876

859877
static int
@@ -897,6 +915,9 @@ static const formatdef native_table[] = {
897915
#ifdef Py_HAVE_C_COMPLEX
898916
{'E', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
899917
{'C', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
918+
#else
919+
{'E', 1, 0, nu_complex_stub, np_complex_stub},
920+
{'C', 1, 0, nu_complex_stub, np_complex_stub},
900921
#endif
901922
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
902923
{0}
@@ -1236,6 +1257,9 @@ static formatdef bigendian_table[] = {
12361257
#ifdef Py_HAVE_C_COMPLEX
12371258
{'E', 8, 0, bu_float_complex, bp_float_complex},
12381259
{'C', 16, 0, bu_double_complex, bp_double_complex},
1260+
#else
1261+
{'E', 1, 0, nu_complex_stub, np_complex_stub},
1262+
{'C', 1, 0, nu_complex_stub, np_complex_stub},
12391263
#endif
12401264
{0}
12411265
};
@@ -1559,6 +1583,9 @@ static formatdef lilendian_table[] = {
15591583
#ifdef Py_HAVE_C_COMPLEX
15601584
{'E', 8, 0, lu_float_complex, lp_float_complex},
15611585
{'C', 16, 0, lu_double_complex, lp_double_complex},
1586+
#else
1587+
{'E', 1, 0, nu_complex_stub, np_complex_stub},
1588+
{'C', 1, 0, nu_complex_stub, np_complex_stub},
15621589
#endif
15631590
{0}
15641591
};

0 commit comments

Comments
 (0)