Skip to content

Commit 1d41358

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

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-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'')
823+
with self.assertRaisesRegex(struct.error, msg2):
824+
struct.pack('C', 1j)
825+
with self.assertRaisesRegex(struct.error, msg2):
826+
struct.unpack('C', b'')
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,16 @@ 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+
complex_format_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+
}
857867
#endif
858868

859869
static int
@@ -897,6 +907,9 @@ static const formatdef native_table[] = {
897907
#ifdef Py_HAVE_C_COMPLEX
898908
{'E', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
899909
{'C', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
910+
#else
911+
{'E', 1, 0, complex_format_stub, complex_format_stub},
912+
{'C', 1, 0, complex_format_stub, complex_format_stub},
900913
#endif
901914
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
902915
{0}
@@ -1236,6 +1249,9 @@ static formatdef bigendian_table[] = {
12361249
#ifdef Py_HAVE_C_COMPLEX
12371250
{'E', 8, 0, bu_float_complex, bp_float_complex},
12381251
{'C', 16, 0, bu_double_complex, bp_double_complex},
1252+
#else
1253+
{'E', 1, 0, complex_format_stub, complex_format_stub},
1254+
{'C', 1, 0, complex_format_stub, complex_format_stub},
12391255
#endif
12401256
{0}
12411257
};
@@ -1559,6 +1575,9 @@ static formatdef lilendian_table[] = {
15591575
#ifdef Py_HAVE_C_COMPLEX
15601576
{'E', 8, 0, lu_float_complex, lp_float_complex},
15611577
{'C', 16, 0, lu_double_complex, lp_double_complex},
1578+
#else
1579+
{'E', 1, 0, complex_format_stub, complex_format_stub},
1580+
{'C', 1, 0, complex_format_stub, complex_format_stub},
15621581
#endif
15631582
{0}
15641583
};

0 commit comments

Comments
 (0)