Skip to content

Commit f0fe2dc

Browse files
committed
Call setitem once in ins1() and remove the new slot if it fails
1 parent cf6c1f3 commit f0fe2dc

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

‎Modules/arraymodule.c‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -585,18 +585,15 @@ static int
585585
e_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
586586
{
587587
double x;
588-
char buf[sizeof(short)];
589588
if (!PyArg_Parse(v, "d;array item must be float", &x)) {
590589
return -1;
591590
}
592-
if (PyFloat_Pack2(x, buf, PY_LITTLE_ENDIAN) < 0) {
593-
return -1;
594-
}
595591

596592
CHECK_ARRAY_BOUNDS(ap, i);
597593

598594
if (i >= 0) {
599-
memcpy(ap->ob_item + sizeof(short)*i, buf, sizeof(buf));
595+
return PyFloat_Pack2(x, ap->ob_item + sizeof(short)*i,
596+
PY_LITTLE_ENDIAN);
600597
}
601598
return 0;
602599
}
@@ -611,17 +608,14 @@ static int
611608
f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
612609
{
613610
double x;
614-
char buf[sizeof(float)];
615611
if (!PyArg_Parse(v, "d;array item must be float", &x))
616612
return -1;
617-
if (PyFloat_Pack4(x, buf, PY_LITTLE_ENDIAN) < 0) {
618-
return -1;
619-
}
620613

621614
CHECK_ARRAY_BOUNDS(ap, i);
622615

623616
if (i >= 0) {
624-
memcpy(ap->ob_item + sizeof(float)*i, buf, sizeof(buf));
617+
return PyFloat_Pack4(x, ap->ob_item + sizeof(float)*i,
618+
PY_LITTLE_ENDIAN);
625619
}
626620
return 0;
627621
}
@@ -659,21 +653,25 @@ static int
659653
cf_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
660654
{
661655
Py_complex x;
662-
char f[8];
663656

664657
if (!PyArg_Parse(v, "D;array item must be complex", &x)) {
665658
return -1;
666659
}
667-
if (PyFloat_Pack4(x.real, f, PY_LITTLE_ENDIAN) < 0
668-
|| PyFloat_Pack4(x.imag, f + sizeof(float), PY_LITTLE_ENDIAN) < 0)
669-
{
670-
return -1;
671-
}
672660

673661
CHECK_ARRAY_BOUNDS(ap, i);
674662

675663
if (i >= 0) {
676-
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;
677675
}
678676
return 0;
679677
}
@@ -825,6 +823,8 @@ getarrayitem(PyObject *op, Py_ssize_t i)
825823
return (*ap->ob_descr->getitem)(ap, i);
826824
}
827825

826+
static int array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh);
827+
828828
static int
829829
ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
830830
{
@@ -834,8 +834,6 @@ ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
834834
PyErr_BadInternalCall();
835835
return -1;
836836
}
837-
if ((*self->ob_descr->setitem)(self, -1, v) < 0)
838-
return -1;
839837

840838
if (array_resize(self, n+1) == -1)
841839
return -1;
@@ -852,7 +850,11 @@ ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
852850
memmove(items + (where+1)*self->ob_descr->itemsize,
853851
items + where*self->ob_descr->itemsize,
854852
(n-where)*self->ob_descr->itemsize);
855-
return (*self->ob_descr->setitem)(self, where, v);
853+
if ((*self->ob_descr->setitem)(self, where, v) < 0) {
854+
array_del_slice(self, where, where+1);
855+
return -1;
856+
}
857+
return 0;
856858
}
857859

858860
/* Methods */

0 commit comments

Comments
 (0)