| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -397,6 +397,21 @@ def test_705836(self): | |||
| 397 | 397 | big = (1 << 25) - 1 | |
| 398 | 398 | big = math.ldexp(big, 127 - 24) | |
| 399 | 399 | self.assertRaises(OverflowError, struct.pack, ">f", big) | |
| 400 | + self.assertRaises(OverflowError, struct.pack, "<f", big) | ||
| 401 | + # same for native format, see gh-145633 | ||
| 402 | + self.assertRaises(OverflowError, struct.pack, "f", big) | ||
| 403 | + | ||
| 404 | + # And for half-floats | ||
| 405 | + big = (1 << 11) - 1 | ||
| 406 | + big = math.ldexp(big, 15 - 10) | ||
| 407 | + packed = struct.pack(">e", big) | ||
| 408 | + unpacked = struct.unpack(">e", packed)[0] | ||
| 409 | + self.assertEqual(big, unpacked) | ||
| 410 | + big = (1 << 12) - 1 | ||
| 411 | + big = math.ldexp(big, 15 - 11) | ||
| 412 | + self.assertRaises(OverflowError, struct.pack, ">e", big) | ||
| 413 | + self.assertRaises(OverflowError, struct.pack, "<e", big) | ||
| 414 | + self.assertRaises(OverflowError, struct.pack, "e", big) | ||
| 400 | 415 | ||
| 401 | 416 | def test_1530559(self): | |
| 402 | 417 | for code, byteorder in iter_integer_formats(): | |
@@ -1016,6 +1031,24 @@ def test_operations_on_half_initialized_Struct(self): | |||
| 1016 | 1031 | self.assertRaises(RuntimeError, repr, S) | |
| 1017 | 1032 | self.assertEqual(S.size, -1) | |
| 1018 | 1033 | ||
| 1034 | + def test_float_round_trip(self): | ||
| 1035 | + for format in ( | ||
| 1036 | + "f", "<f", ">f", | ||
| 1037 | + "d", "<d", ">d", | ||
| 1038 | + "e", "<e", ">e", | ||
| 1039 | + ): | ||
| 1040 | + with self.subTest(format=format): | ||
| 1041 | + f = struct.unpack(format, struct.pack(format, 1.5))[0] | ||
| 1042 | + self.assertEqual(f, 1.5) | ||
| 1043 | + f = struct.unpack(format, struct.pack(format, NAN))[0] | ||
| 1044 | + self.assertTrue(math.isnan(f), f) | ||
| 1045 | + f = struct.unpack(format, struct.pack(format, INF))[0] | ||
| 1046 | + self.assertTrue(math.isinf(f), f) | ||
| 1047 | + self.assertEqual(math.copysign(1.0, f), 1.0) | ||
| 1048 | + f = struct.unpack(format, struct.pack(format, -INF))[0] | ||
| 1049 | + self.assertTrue(math.isinf(f), f) | ||
| 1050 | + self.assertEqual(math.copysign(1.0, f), -1.0) | ||
| 1051 | + | ||
| 1019 | 1052 | ||
| 1020 | 1053 | class UnpackIteratorTest(unittest.TestCase): | |
| 1021 | 1054 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Fix ``struct.pack('f', float)``: use :c:func:`PyFloat_Pack4` to raise | ||
| 2 | + :exc:`OverflowError`. Patch by Sergey B Kirpichev and Victor Stinner. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -763,14 +763,13 @@ np_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f | |||
| 763 | 763 | static int | |
| 764 | 764 | np_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) | |
| 765 | 765 | { | |
| 766 | - float x = (float)PyFloat_AsDouble(v); | ||
| 766 | + double x = PyFloat_AsDouble(v); | ||
| 767 | 767 | if (x == -1 && PyErr_Occurred()) { | |
| 768 | 768 | PyErr_SetString(state->StructError, | |
| 769 | 769 | "required argument is not a float"); | |
| 770 | 770 | return -1; | |
| 771 | 771 | } | |
| 772 | - memcpy(p, &x, sizeof x); | ||
| 773 | - return 0; | ||
| 772 | + return PyFloat_Pack4(x, p, PY_LITTLE_ENDIAN); | ||
| 774 | 773 | } | |
| 775 | 774 | ||
| 776 | 775 | static int | |
| Back | FazBrowse Home | New Git URL |
0 commit comments