Skip to content

Commit 9b4b7cf

Browse files
committed
fix also 'f' and 'Zf'
1 parent 3fd7d3b commit 9b4b7cf

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

Lib/test/test_array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,9 @@ class FloatTest(FPTest, unittest.TestCase):
16171617
typecode = 'f'
16181618
minitemsize = 4
16191619

1620+
def test_overflows(self):
1621+
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])
1622+
16201623
class DoubleTest(FPTest, unittest.TestCase):
16211624
typecode = 'd'
16221625
minitemsize = 8
@@ -1643,6 +1646,10 @@ class ComplexFloatTest(CFPTest, unittest.TestCase):
16431646
typecode = 'Zf'
16441647
minitemsize = 8
16451648

1649+
def test_overflows(self):
1650+
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])
1651+
self.assertRaises(OverflowError, array.array, self.typecode, [1e300j])
1652+
16461653
class ComplexDoubleTest(CFPTest, unittest.TestCase):
16471654
typecode = 'Zd'
16481655
minitemsize = 16
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
:func:`array.array` setter now correctly detects overflows for the ``'e'``
2-
type code. Patch by Sergey B Kirpichev.
1+
:func:`array.array` setter now correctly detects overflows for the ``'e'``,
2+
``'f'`` and ``'Zf'`` type codes. Patch by Sergey B Kirpichev.

Modules/arraymodule.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,16 @@ f_getitem(arrayobject *ap, Py_ssize_t i)
607607
static int
608608
f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
609609
{
610-
float x;
611-
if (!PyArg_Parse(v, "f;array item must be float", &x))
610+
double x;
611+
if (!PyArg_Parse(v, "d;array item must be float", &x))
612612
return -1;
613613

614614
CHECK_ARRAY_BOUNDS(ap, i);
615615

616-
if (i >= 0)
617-
((float *)ap->ob_item)[i] = x;
616+
if (i >= 0) {
617+
return PyFloat_Pack4(x, ap->ob_item + sizeof(float)*i,
618+
PY_LITTLE_ENDIAN);
619+
}
618620
return 0;
619621
}
620622

@@ -651,18 +653,25 @@ static int
651653
cf_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
652654
{
653655
Py_complex x;
654-
float f[2];
655656

656657
if (!PyArg_Parse(v, "D;array item must be complex", &x)) {
657658
return -1;
658659
}
659660

660661
CHECK_ARRAY_BOUNDS(ap, i);
661662

662-
f[0] = (float)x.real;
663-
f[1] = (float)x.imag;
664663
if (i >= 0) {
665-
memcpy(ap->ob_item + i*sizeof(f), &f, sizeof(f));
664+
char f[8];
665+
int ret = PyFloat_Pack4(x.real, f, PY_LITTLE_ENDIAN);
666+
667+
if (ret) {
668+
return ret;
669+
}
670+
ret = PyFloat_Pack4(x.imag, f + sizeof(float), PY_LITTLE_ENDIAN);
671+
if (!ret) {
672+
memcpy(ap->ob_item + i*sizeof(f), &f, sizeof(f));
673+
}
674+
return ret;
666675
}
667676
return 0;
668677
}

0 commit comments

Comments
 (0)