Skip to content

Commit 9617a72

Browse files
committed
gh-156864: correctly detect overflows for array's "e" type code
1 parent 5056ac5 commit 9617a72

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

Lib/test/test_array.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,12 @@ class HalfFloatTest(FPTest, unittest.TestCase):
16071607
typecode = 'e'
16081608
minitemsize = 2
16091609

1610+
def test_overflows(self):
1611+
# Overflows half-float type:
1612+
self.assertRaises(OverflowError, array.array, self.typecode, [123456])
1613+
# Overflows also float type:
1614+
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])
1615+
16101616
class FloatTest(FPTest, unittest.TestCase):
16111617
typecode = 'f'
16121618
minitemsize = 4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`array.array` setter now correctly detects overflows for the ``'e'``
2+
type code. Patch by Sergey B Kirpichev.

Modules/arraymodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ e_getitem(arrayobject *ap, Py_ssize_t i)
584584
static int
585585
e_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
586586
{
587-
float x;
588-
if (!PyArg_Parse(v, "f;array item must be float", &x)) {
587+
double x;
588+
if (!PyArg_Parse(v, "d;array item must be float", &x)) {
589589
return -1;
590590
}
591591

0 commit comments

Comments
 (0)