Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ def test_705836(self):
self.assertRaises(OverflowError, struct.pack, "<e", big)
self.assertRaises(OverflowError, struct.pack, "e", big)

def test_156867(self):
big_real = 1e300
self.assertRaises(OverflowError, struct.pack, ">Zf", big_real)
self.assertRaises(OverflowError, struct.pack, "<Zf", big_real)
self.assertRaises(OverflowError, struct.pack, "Zf", big_real)
big_imag = 1e300j
self.assertRaises(OverflowError, struct.pack, ">Zf", big_imag)
self.assertRaises(OverflowError, struct.pack, "<Zf", big_imag)
self.assertRaises(OverflowError, struct.pack, "Zf", big_imag)

def test_1530559(self):
for code, byteorder in iter_integer_formats():
format = byteorder + code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise :exc:`OverflowError`'s for native ``'Zf'`` format in :func:`struct.pack`,
like for ``'f'`` format. Previously overflows in the :c:expr:`float complex`
type were silent. Patch by Sergey B Kirpichev.
10 changes: 7 additions & 3 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,19 @@ np_float_complex(_structmodulestate *state, char *p, PyObject *v,
const formatdef *f)
{
Py_complex c = PyComplex_AsCComplex(v);
float x[2] = {(float)c.real, (float)c.imag};

if (c.real == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
"required argument is not a complex");
return -1;
}
memcpy(p, &x, sizeof(x));
return 0;

int ret = PyFloat_Pack4(c.real, p, PY_LITTLE_ENDIAN);

if (ret) {
return ret;
}
return PyFloat_Pack4(c.imag, p + sizeof(float), PY_LITTLE_ENDIAN);
}

static int
Expand Down
Loading