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
13 changes: 13 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,10 +1607,19 @@ class HalfFloatTest(FPTest, unittest.TestCase):
typecode = 'e'
minitemsize = 2

def test_overflows(self):
# Overflows half-float type:
self.assertRaises(OverflowError, array.array, self.typecode, [123456])
# Overflows also float type:
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])

class FloatTest(FPTest, unittest.TestCase):
typecode = 'f'
minitemsize = 4

def test_overflows(self):
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])

class DoubleTest(FPTest, unittest.TestCase):
typecode = 'd'
minitemsize = 8
Expand All @@ -1637,6 +1646,10 @@ class ComplexFloatTest(CFPTest, unittest.TestCase):
typecode = 'Zf'
minitemsize = 8

def test_overflows(self):
self.assertRaises(OverflowError, array.array, self.typecode, [1e300])
self.assertRaises(OverflowError, array.array, self.typecode, [1e300j])

class ComplexDoubleTest(CFPTest, unittest.TestCase):
typecode = 'Zd'
minitemsize = 16
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`array.array` setter now correctly detects overflows for the ``'e'``,
``'f'`` and ``'Zf'`` type codes. Patch by Sergey B Kirpichev.
29 changes: 19 additions & 10 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ e_getitem(arrayobject *ap, Py_ssize_t i)
static int
e_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
float x;
if (!PyArg_Parse(v, "f;array item must be float", &x)) {
double x;
if (!PyArg_Parse(v, "d;array item must be float", &x)) {
return -1;
}

Expand All @@ -607,14 +607,16 @@ f_getitem(arrayobject *ap, Py_ssize_t i)
static int
f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
float x;
if (!PyArg_Parse(v, "f;array item must be float", &x))
double x;
if (!PyArg_Parse(v, "d;array item must be float", &x))
return -1;

CHECK_ARRAY_BOUNDS(ap, i);

if (i >= 0)
((float *)ap->ob_item)[i] = x;
if (i >= 0) {
return PyFloat_Pack4(x, ap->ob_item + sizeof(float)*i,
PY_LITTLE_ENDIAN);
}
return 0;
}

Expand Down Expand Up @@ -651,18 +653,25 @@ static int
cf_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
Py_complex x;
float f[2];

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

CHECK_ARRAY_BOUNDS(ap, i);

f[0] = (float)x.real;
f[1] = (float)x.imag;
if (i >= 0) {
memcpy(ap->ob_item + i*sizeof(f), &f, sizeof(f));
char f[8];
int ret = PyFloat_Pack4(x.real, f, PY_LITTLE_ENDIAN);

if (ret) {
return ret;
}
ret = PyFloat_Pack4(x.imag, f + sizeof(float), PY_LITTLE_ENDIAN);
if (!ret) {
memcpy(ap->ob_item + i*sizeof(f), &f, sizeof(f));
}
return ret;
}
return 0;
}
Expand Down
Loading