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
15 changes: 15 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ def test_705836(self):
big = (1 << 25) - 1
big = math.ldexp(big, 127 - 24)
self.assertRaises(OverflowError, struct.pack, ">f", big)
self.assertRaises(OverflowError, struct.pack, "<f", big)
# same for native format, see gh-145633
self.assertRaises(OverflowError, struct.pack, "f", big)

# And for half-floats
big = (1 << 11) - 1
big = math.ldexp(big, 15 - 10)
packed = struct.pack(">e", big)
unpacked = struct.unpack(">e", packed)[0]
self.assertEqual(big, unpacked)
big = (1 << 12) - 1
big = math.ldexp(big, 15 - 11)
self.assertRaises(OverflowError, struct.pack, ">e", big)
self.assertRaises(OverflowError, struct.pack, "<e", big)
self.assertRaises(OverflowError, struct.pack, "e", big)

def test_1530559(self):
for code, byteorder in iter_integer_formats():
Expand Down
5 changes: 2 additions & 3 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,13 @@ np_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
static int
np_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
float x = (float)PyFloat_AsDouble(v);
double x = PyFloat_AsDouble(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_SetString(state->StructError,
"required argument is not a float");
return -1;
}
memcpy(p, &x, sizeof x);
return 0;
return PyFloat_Pack4(x, p, PY_LITTLE_ENDIAN);
}

static int
Expand Down
Loading