summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThouis (Ray) Jones <thouis@gmail.com>2012-06-06 10:32:35 +0200
committerThouis (Ray) Jones <thouis@gmail.com>2012-06-06 11:22:45 +0200
commit56f865980447b2fc5475b1b71ef7347383660782 (patch)
treee3a41654332f891192d8c003026073616b247452
parent4c68a33a5b75cbfed1aa0b3b23e947563b9a513a (diff)
downloadnumpy-56f865980447b2fc5475b1b71ef7347383660782.tar.gz
Add check_and_adjust_index(), and replace most index checks with it.
This commit adds a check_and_adjust_index(npy_intp *index, npy_intp max, int axis) function which checks index against max, setting an IndexError and returning -1 if it's not valid, and otherwise adjusting index in-place to handle Python's negative indexing, and returning 0. It also changes most places in the code where indexes were being checked and adjusted with a call to this function.
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src9
-rw-r--r--numpy/core/src/multiarray/common.c55
-rw-r--r--numpy/core/src/multiarray/common.h9
-rw-r--r--numpy/core/src/multiarray/ctors.c3
-rw-r--r--numpy/core/src/multiarray/descriptor.c11
-rw-r--r--numpy/core/src/multiarray/item_selection.c74
-rw-r--r--numpy/core/src/multiarray/iterators.c48
-rw-r--r--numpy/core/src/multiarray/mapping.c91
-rw-r--r--numpy/core/src/multiarray/methods.c18
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c3
10 files changed, 105 insertions, 216 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 0296d4063..c512025b1 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -3527,15 +3527,8 @@ static int
for (i = 0; i < n_outer; i++) {
for (j = 0; j < m_middle; j++) {
tmp = indarray[j];
- if ((tmp < -nindarray) || (tmp >= nindarray)) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension %"NPY_INTP_FMT,
- tmp, j);
+ if (check_and_adjust_index(&tmp, nindarray, (int) j) < 0)
return 1;
- }
- if (tmp < 0) {
- tmp = tmp + nindarray;
- }
if (nelem == 1) {
*dest++ = *(src + tmp);
}
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c
index db255025d..a704fd1ad 100644
--- a/numpy/core/src/multiarray/common.c
+++ b/numpy/core/src/multiarray/common.c
@@ -506,28 +506,63 @@ _array_typedescr_fromstr(char *c_str)
return descr;
}
+NPY_NO_EXPORT int
+check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis)
+{
+ /* Check that index is valid, taking into account negative indices */
+ if ((*index < -max_item) || (*index >= max_item)) {
+ /* Try to be as clear as possible about what went wrong. */
+ if (axis >= 0) {
+ if (max_item > 0) {
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" is out of bounds for axis %d: "
+ "[%"NPY_INTP_FMT",%"NPY_INTP_FMT")",
+ *index, axis,
+ -max_item, max_item);
+ } else {
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" is out of bounds for 0-d axis %d",
+ *index, axis);
+ }
+ } else {
+ if (max_item > 0) {
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" is out of bounds: "
+ "[%"NPY_INTP_FMT",%"NPY_INTP_FMT")",
+ *index,
+ -max_item, max_item);
+ } else {
+ /* I don't believe there are currently any cases where this occurs. */
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" is out of bounds for 0-d axis",
+ *index);
+ }
+ }
+ return -1;
+ }
+ /* adjust negative indices */
+ if (*index < 0) {
+ *index += max_item;
+ }
+ return 0;
+}
+
NPY_NO_EXPORT char *
index2ptr(PyArrayObject *mp, npy_intp i)
{
npy_intp dim0;
- npy_intp orig_i = i; /* in case of error. */
if (PyArray_NDIM(mp) == 0) {
PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed");
return NULL;
}
dim0 = PyArray_DIMS(mp)[0];
- if (i < 0) {
- i += dim0;
- }
- if (i == 0 && dim0 > 0) {
+ if (check_and_adjust_index(&i, dim0, 0) < 0)
+ return NULL;
+ if (i == 0) {
return PyArray_DATA(mp);
}
- if (i > 0 && i < dim0) {
- return PyArray_DATA(mp)+i*PyArray_STRIDES(mp)[0];
- }
- PyErr_Format(PyExc_IndexError, "index %"NPY_INTP_FMT" out of bounds in dimension 0", orig_i);
- return NULL;
+ return PyArray_DATA(mp)+i*PyArray_STRIDES(mp)[0];
}
NPY_NO_EXPORT int
diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h
index 60b4008d8..085a91d01 100644
--- a/numpy/core/src/multiarray/common.h
+++ b/numpy/core/src/multiarray/common.h
@@ -41,6 +41,15 @@ _array_find_python_scalar_type(PyObject *op);
NPY_NO_EXPORT PyArray_Descr *
_array_typedescr_fromstr(char *str);
+/*
+ * Returns -1 and sets an exception if *index is an invalid index for
+ * an array of size max_item, otherwise adjusts it in place to be
+ * 0 <= *index < max_item, and returns 0.
+ * If axis >= 0, it will be reported as part of the exception.
+ */
+NPY_NO_EXPORT int
+check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis);
+
NPY_NO_EXPORT char *
index2ptr(PyArrayObject *mp, npy_intp i);
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index c8548f894..12c2ccad5 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -2925,6 +2925,7 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
{
PyObject *temp1, *temp2;
int n = PyArray_NDIM(arr);
+ int axis_orig = *axis;
if (*axis == NPY_MAXDIMS || n == 0) {
if (n != 1) {
@@ -2967,7 +2968,7 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
}
if ((*axis < 0) || (*axis >= n)) {
PyErr_Format(PyExc_ValueError,
- "axis(=%d) out of bounds", *axis);
+ "axis(=%d) out of bounds", axis_orig);
Py_DECREF(temp2);
return NULL;
}
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c
index 4bfcd3488..babbbc9b7 100644
--- a/numpy/core/src/multiarray/descriptor.c
+++ b/numpy/core/src/multiarray/descriptor.c
@@ -3383,18 +3383,19 @@ descr_subscript(PyArray_Descr *self, PyObject *op)
PyObject *name;
int size = PyTuple_GET_SIZE(self->names);
int value = PyArray_PyIntAsInt(op);
+ int orig_value = value;
if (PyErr_Occurred()) {
return NULL;
}
- if (value < -size || value >= size) {
- PyErr_Format(PyExc_IndexError,
- "Field index %d out of range.", value);
- return NULL;
- }
if (value < 0) {
value += size;
}
+ if (value < 0 || value >= size) {
+ PyErr_Format(PyExc_IndexError,
+ "Field index %d out of range.", orig_value);
+ return NULL;
+ }
name = PyTuple_GET_ITEM(self->names, value);
retval = descr_subscript(self, name);
}
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c
index 89ad3eb17..b7440e7c1 100644
--- a/numpy/core/src/multiarray/item_selection.c
+++ b/numpy/core/src/multiarray/item_selection.c
@@ -174,16 +174,10 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
tmp = ((npy_intp *)(PyArray_DATA(indices)))[j];
- if ((tmp < -max_item) || (tmp >= max_item)) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of range for array in dimension %"NPY_INTP_FMT,
- tmp, axis);
+ if (check_and_adjust_index(&tmp, max_item, axis) < 0) {
NPY_AUXDATA_FREE(transferdata);
goto fail;
}
- if (tmp < 0) {
- tmp = tmp + max_item;
- }
maskedstransfer(dest, itemsize,
src + tmp*chunk, itemsize,
(npy_mask *)(src_maskna + tmp*nelem), 1,
@@ -253,16 +247,9 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
tmp = ((npy_intp *)(PyArray_DATA(indices)))[j];
- if ((tmp < -max_item) || (tmp >= max_item)) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of range for "
- "array in dimension %"NPY_INTP_FMT,
- tmp, axis);
+ if (check_and_adjust_index(&tmp, max_item, axis) < 0) {
goto fail;
}
- if (tmp < 0) {
- tmp = tmp + max_item;
- }
memmove(dest, src + tmp*chunk, chunk);
dest += chunk;
}
@@ -391,14 +378,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0,
for (i = 0; i < ni; i++) {
src = PyArray_DATA(values) + chunk*(i % nv);
tmp = ((npy_intp *)(PyArray_DATA(indices)))[i];
- if (tmp < 0) {
- tmp = tmp + max_item;
- }
- if ((tmp < 0) || (tmp >= max_item)) {
- /* TODO: should report out-of-range indices with axis */
- PyErr_SetString(PyExc_IndexError,
- "index out of " \
- "range for array");
+ if (check_and_adjust_index(&tmp, max_item, 0) < 0) {
goto fail;
}
PyArray_Item_INCREF(src, PyArray_DESCR(self));
@@ -448,14 +428,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0,
for (i = 0; i < ni; i++) {
src = PyArray_DATA(values) + chunk * (i % nv);
tmp = ((npy_intp *)(PyArray_DATA(indices)))[i];
- if (tmp < 0) {
- tmp = tmp + max_item;
- }
- if ((tmp < 0) || (tmp >= max_item)) {
- /* TODO: should report out-of-range indices with axis */
- PyErr_SetString(PyExc_IndexError,
- "index out of " \
- "range for array");
+ if (check_and_adjust_index(&tmp, max_item, 0) < 0) {
goto fail;
}
memmove(dest + tmp * chunk, src, chunk);
@@ -1106,6 +1079,7 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which)
PyArrayObject *ap = NULL, *store_arr = NULL;
char *ip;
int i, n, m, elsize, orign;
+ int axis_orig=axis;
n = PyArray_NDIM(op);
if ((n == 0) || (PyArray_SIZE(op) == 1)) {
@@ -1115,7 +1089,7 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which)
axis += n;
}
if ((axis < 0) || (axis >= n)) {
- PyErr_Format(PyExc_ValueError, "axis(=%d) out of bounds", axis);
+ PyErr_Format(PyExc_ValueError, "axis(=%d) out of bounds", axis_orig);
return -1;
}
if (!PyArray_ISWRITEABLE(op)) {
@@ -2530,14 +2504,7 @@ PyArray_MultiIndexGetItem(PyArrayObject *self, npy_intp *multi_index)
npy_intp shapevalue = shape[idim];
npy_intp ind = multi_index[idim];
- if (ind < 0) {
- ind += shapevalue;
- }
-
- if (ind < 0 || ind >= shapevalue) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension %d",
- multi_index[idim], idim);
+ if (check_and_adjust_index(&ind, shapevalue, idim) < 0) {
return NULL;
}
@@ -2561,14 +2528,7 @@ PyArray_MultiIndexGetItem(PyArrayObject *self, npy_intp *multi_index)
npy_intp shapevalue = shape[idim];
npy_intp ind = multi_index[idim];
- if (ind < 0) {
- ind += shapevalue;
- }
-
- if (ind < 0 || ind >= shapevalue) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension %d",
- multi_index[idim], idim);
+ if (check_and_adjust_index(&ind, shapevalue, idim) < 0) {
return NULL;
}
@@ -2611,14 +2571,7 @@ PyArray_MultiIndexSetItem(PyArrayObject *self, npy_intp *multi_index,
npy_intp shapevalue = shape[idim];
npy_intp ind = multi_index[idim];
- if (ind < 0) {
- ind += shapevalue;
- }
-
- if (ind < 0 || ind >= shapevalue) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension %d",
- multi_index[idim], idim);
+ if (check_and_adjust_index(&ind, shapevalue, idim) < 0) {
return -1;
}
@@ -2655,14 +2608,7 @@ PyArray_MultiIndexSetItem(PyArrayObject *self, npy_intp *multi_index,
npy_intp shapevalue = shape[idim];
npy_intp ind = multi_index[idim];
- if (ind < 0) {
- ind += shapevalue;
- }
-
- if (ind < 0 || ind >= shapevalue) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension %d",
- multi_index[idim], idim);
+ if (check_and_adjust_index(&ind, shapevalue, idim) < 0) {
return -1;
}
diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c
index e4bc1a336..1cc932ad9 100644
--- a/numpy/core/src/multiarray/iterators.c
+++ b/numpy/core/src/multiarray/iterators.c
@@ -31,7 +31,8 @@ slice_coerce_index(PyObject *o, npy_intp *v);
*/
NPY_NO_EXPORT npy_intp
parse_index_entry(PyObject *op, npy_intp *step_size,
- npy_intp *n_steps, npy_intp max)
+ npy_intp *n_steps, npy_intp max,
+ int axis, int check_index)
{
npy_intp i;
@@ -69,13 +70,10 @@ parse_index_entry(PyObject *op, npy_intp *step_size,
}
*n_steps = SINGLE_INDEX;
*step_size = 0;
- if (i < 0) {
- i += max;
- }
- if (i >= max || i < 0) {
- /* TODO: should report out-of-range indices with axis */
- PyErr_SetString(PyExc_IndexError, "invalid index");
+ if (check_index) {
+ if (check_and_adjust_index(&i, max, axis) < 0) {
goto fail;
+ }
}
}
return i;
@@ -135,8 +133,9 @@ parse_index(PyArrayObject *self, PyObject *op,
}
}
start = parse_index_entry(op1, &step_size, &n_steps,
- nd_old < PyArray_NDIM(self) ?
- PyArray_DIMS(self)[nd_old] : 0);
+ nd_old < PyArray_NDIM(self) ?
+ PyArray_DIMS(self)[nd_old] : 0,
+ nd_old, nd_old < PyArray_NDIM(self));
Py_DECREF(op1);
if (start == -1) {
break;
@@ -718,14 +717,7 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind)
itemsize = PyArray_DESCR(self->ao)->elsize;
if (PyArray_NDIM(ind) == 0) {
num = *((npy_intp *)PyArray_DATA(ind));
- if (num < 0) {
- num += self->size;
- }
- if (num < 0 || num >= self->size) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds" \
- " 0<=index<%"NPY_INTP_FMT,
- num, self->size);
+ if (check_and_adjust_index(&num, self->size, -1) < 0) {
PyArray_ITER_RESET(self);
return NULL;
}
@@ -759,14 +751,7 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind)
swap = (PyArray_ISNOTSWAPPED(ret) != PyArray_ISNOTSWAPPED(self->ao));
while (counter--) {
num = *((npy_intp *)(ind_it->dataptr));
- if (num < 0) {
- num += self->size;
- }
- if (num < 0 || num >= self->size) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds" \
- " 0<=index<%"NPY_INTP_FMT,
- num, self->size);
+ if (check_and_adjust_index(&num, self->size, -1) < 0) {
Py_DECREF(ind_it);
Py_DECREF(ret);
PyArray_ITER_RESET(self);
@@ -842,7 +827,7 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind)
/* Check for Integer or Slice */
if (PyLong_Check(ind) || PyInt_Check(ind) || PySlice_Check(ind)) {
start = parse_index_entry(ind, &step_size, &n_steps,
- self->size);
+ self->size, 0, 1);
if (start == -1) {
goto fail;
}
@@ -1001,14 +986,7 @@ iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind,
counter = ind_it->size;
while (counter--) {
num = *((npy_intp *)(ind_it->dataptr));
- if (num < 0) {
- num += self->size;
- }
- if ((num < 0) || (num >= self->size)) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds" \
- " 0<=index<%"NPY_INTP_FMT, num,
- self->size);
+ if (check_and_adjust_index(&num, self->size, -1) < 0) {
Py_DECREF(ind_it);
return -1;
}
@@ -1119,7 +1097,7 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val)
/* Check Slice */
if (PySlice_Check(ind)) {
- start = parse_index_entry(ind, &step_size, &n_steps, self->size);
+ start = parse_index_entry(ind, &step_size, &n_steps, self->size, 0, 0);
if (start == -1) {
goto finish;
}
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c
index 779769e23..821697f1a 100644
--- a/numpy/core/src/multiarray/mapping.c
+++ b/numpy/core/src/multiarray/mapping.c
@@ -57,15 +57,9 @@ array_big_item(PyArrayObject *self, npy_intp i)
/* Bounds check and get the data pointer */
dim0 = PyArray_DIM(self, 0);
- if (i < -dim0 || i >= dim0) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension 0",
- i);
+ if (check_and_adjust_index(&i, dim0, 0) < 0) {
return NULL;
}
- if (i < 0) {
- i += dim0;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
/* Create the view array */
@@ -123,15 +117,9 @@ array_item_nice(PyArrayObject *self, Py_ssize_t i)
/* Bounds check and get the data pointer */
dim0 = PyArray_DIM(self, 0);
- if (i < -dim0 || i >= dim0) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension 0",
- i);
+ if (check_and_adjust_index(&i, dim0, 0) < 0) {
return NULL;
}
- if (i < 0) {
- i += dim0;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
if (PyArray_HASMASKNA(self)) {
@@ -206,15 +194,9 @@ array_ass_big_item(PyArrayObject *self, npy_intp i, PyObject *v)
/* Bounds check and get the data pointer */
dim0 = PyArray_DIM(self, 0);
- if (i < -dim0 || i >= dim0) {
- PyErr_Format(PyExc_IndexError,
- "index %"NPY_INTP_FMT" out of bounds in dimension 0",
- i);
+ if (check_and_adjust_index(&i, dim0, 0) < 0) {
return -1;
}
- if (i < 0) {
- i += dim0;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
return PyArray_DESCR(self)->f->setitem(v, item, self);
@@ -1610,19 +1592,10 @@ array_ass_sub(PyArrayObject *self, PyObject *ind, PyObject *op)
if (!PyArray_HASMASKNA(self)) {
for (idim = 0; idim < ndim; idim++) {
npy_intp v = vals[idim];
- if (v < 0) {
- v += shape[idim];
- }
- if (v < 0 || v >= shape[idim]) {
- PyErr_Format(PyExc_IndexError,
- "index (%"NPY_INTP_FMT") out of range "\
- "(0<=index<%"NPY_INTP_FMT") in dimension %d",
- vals[idim], PyArray_DIMS(self)[idim], idim);
+ if (check_and_adjust_index(&v, shape[idim], idim) < 0) {
return -1;
}
- else {
- item += v * strides[idim];
- }
+ item += v * strides[idim];
}
return PyArray_DESCR(self)->f->setitem(op, item, self);
}
@@ -1633,20 +1606,11 @@ array_ass_sub(PyArrayObject *self, PyObject *ind, PyObject *op)
for (idim = 0; idim < ndim; idim++) {
npy_intp v = vals[idim];
- if (v < 0) {
- v += shape[idim];
- }
- if (v < 0 || v >= shape[idim]) {
- PyErr_Format(PyExc_IndexError,
- "index (%"NPY_INTP_FMT") out of range "\
- "(0<=index<%"NPY_INTP_FMT") in dimension %d",
- vals[idim], PyArray_DIMS(self)[idim], idim);
+ if (check_and_adjust_index(&v, shape[idim], idim) < 0) {
return -1;
}
- else {
- item += v * strides[idim];
- maskna_item += v * maskna_strides[idim];
- }
+ item += v * strides[idim];
+ maskna_item += v * maskna_strides[idim];
}
na = NpyNA_FromObject(op, 1);
if (na == NULL) {
@@ -1768,19 +1732,10 @@ array_subscript_nice(PyArrayObject *self, PyObject *op)
if (!PyArray_HASMASKNA(self)) {
for (idim = 0; idim < ndim; idim++) {
npy_intp v = vals[idim];
- if (v < 0) {
- v += shape[idim];
- }
- if (v < 0 || v >= shape[idim]) {
- PyErr_Format(PyExc_IndexError,
- "index (%"NPY_INTP_FMT") out of range "\
- "(0<=index<%"NPY_INTP_FMT") in dimension %d",
- vals[idim], PyArray_DIMS(self)[idim], idim);
+ if (check_and_adjust_index(&v, shape[idim], idim) < 0) {
return NULL;
}
- else {
- item += v * strides[idim];
- }
+ item += v * strides[idim];
}
return PyArray_Scalar(item, PyArray_DESCR(self), (PyObject *)self);
}
@@ -1790,20 +1745,11 @@ array_subscript_nice(PyArrayObject *self, PyObject *op)
for (idim = 0; idim < ndim; idim++) {
npy_intp v = vals[idim];
- if (v < 0) {
- v += shape[idim];
- }
- if (v < 0 || v >= shape[idim]) {
- PyErr_Format(PyExc_IndexError,
- "index (%"NPY_INTP_FMT") out of range "\
- "(0<=index<%"NPY_INTP_FMT") in dimension %d",
- vals[idim], PyArray_DIMS(self)[idim], idim);
+ if (check_and_adjust_index(&v, shape[idim], idim) < 0) {
return NULL;
}
- else {
- item += v * strides[idim];
- maskna_item += v * maskna_strides[idim];
- }
+ item += v * strides[idim];
+ maskna_item += v * maskna_strides[idim];
}
if (NpyMaskValue_IsExposed((npy_mask)*maskna_item)) {
return PyArray_Scalar(item, PyArray_DESCR(self),
@@ -2141,7 +2087,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr)
subnd = PyArray_NDIM(arr) - mit->numiter;
if (subnd < 0) {
- PyErr_SetString(PyExc_ValueError,
+ PyErr_SetString(PyExc_IndexError,
"too many indices for array");
return;
}
@@ -2270,14 +2216,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr)
while (it->index < it->size) {
indptr = ((npy_intp *)it->dataptr);
indval = *indptr;
- if (indval < 0) {
- indval += dimsize;
- }
- if (indval < 0 || indval >= dimsize) {
- PyErr_Format(PyExc_IndexError,
- "index (%"NPY_INTP_FMT") out of range "\
- "(0<=index<%"NPY_INTP_FMT") in dimension %d",
- indval, (dimsize-1), mit->iteraxes[i]);
+ if (check_and_adjust_index(&indval, dimsize, mit->iteraxes[i]) < 0) {
goto fail;
}
PyArray_ITER_NEXT(it);
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index 8129a7021..29035ad1e 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -697,14 +697,7 @@ array_toscalar(PyArrayObject *self, PyObject *args)
return NULL;
}
- /* Negative indexing */
- if (value < 0) {
- value += size;
- }
-
- if (value < 0 || value >= size) {
- PyErr_SetString(PyExc_ValueError,
- "index out of bounds");
+ if (check_and_adjust_index(&value, size, -1) < 0) {
return NULL;
}
@@ -783,14 +776,7 @@ array_setscalar(PyArrayObject *self, PyObject *args)
return NULL;
}
- /* Negative indexing */
- if (value < 0) {
- value += size;
- }
-
- if (value < 0 || value >= size) {
- PyErr_SetString(PyExc_ValueError,
- "index out of bounds");
+ if (check_and_adjust_index(&value, size, -1) < 0) {
return NULL;
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 7a657d8b9..75929198d 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -320,6 +320,7 @@ PyArray_ConcatenateArrays(int narrays, PyArrayObject **arrays, int axis)
PyArrayObject *ret = NULL;
PyArrayObject_fields *sliding_view = NULL;
int has_maskna;
+ int orig_axis = axis;
if (narrays <= 0) {
PyErr_SetString(PyExc_ValueError,
@@ -342,7 +343,7 @@ PyArray_ConcatenateArrays(int narrays, PyArrayObject **arrays, int axis)
}
if (axis < 0 || axis >= ndim) {
PyErr_Format(PyExc_IndexError,
- "axis %d out of bounds [0, %d)", axis, ndim);
+ "axis %d out of bounds [0, %d)", orig_axis, ndim);
return NULL;
}