summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorThouis (Ray) Jones <thouis@gmail.com>2012-06-04 22:41:18 +0200
committerThouis (Ray) Jones <thouis@gmail.com>2012-06-06 11:22:45 +0200
commit4c68a33a5b75cbfed1aa0b3b23e947563b9a513a (patch)
tree2d1f4cff72fe35443d14eb5ab722802d21bb684b /numpy
parentde8c536813472be29a6292721df83d73dbf7016e (diff)
downloadnumpy-4c68a33a5b75cbfed1aa0b3b23e947563b9a513a.tar.gz
ENH: report bad value and dimenion to IndexError exceptions
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src11
-rw-r--r--numpy/core/src/multiarray/common.c3
-rw-r--r--numpy/core/src/multiarray/descriptor.c10
-rw-r--r--numpy/core/src/multiarray/item_selection.c44
-rw-r--r--numpy/core/src/multiarray/iterators.c1
-rw-r--r--numpy/core/src/multiarray/mapping.c30
6 files changed, 60 insertions, 39 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 6dcc7d8e9..0296d4063 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -3527,14 +3527,15 @@ 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);
+ return 1;
+ }
if (tmp < 0) {
tmp = tmp + nindarray;
}
- if ((tmp < 0) || (tmp >= nindarray)) {
- PyErr_SetString(PyExc_IndexError,
- "index out of range for array");
- return 1;
- }
if (nelem == 1) {
*dest++ = *(src + tmp);
}
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c
index 613632c45..db255025d 100644
--- a/numpy/core/src/multiarray/common.c
+++ b/numpy/core/src/multiarray/common.c
@@ -510,6 +510,7 @@ 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");
@@ -525,7 +526,7 @@ index2ptr(PyArrayObject *mp, npy_intp i)
if (i > 0 && i < dim0) {
return PyArray_DATA(mp)+i*PyArray_STRIDES(mp)[0];
}
- PyErr_SetString(PyExc_IndexError,"index out of bounds");
+ PyErr_Format(PyExc_IndexError, "index %"NPY_INTP_FMT" out of bounds in dimension 0", orig_i);
return NULL;
}
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c
index d8def3988..4bfcd3488 100644
--- a/numpy/core/src/multiarray/descriptor.c
+++ b/numpy/core/src/multiarray/descriptor.c
@@ -3387,14 +3387,14 @@ descr_subscript(PyArray_Descr *self, PyObject *op)
if (PyErr_Occurred()) {
return NULL;
}
- if (value < 0) {
- value += size;
- }
- if (value < 0 || value >= size) {
+ if (value < -size || value >= size) {
PyErr_Format(PyExc_IndexError,
- "Field index out of range.");
+ "Field index %d out of range.", value);
return NULL;
}
+ if (value < 0) {
+ value += size;
+ }
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 77faa1eb2..89ad3eb17 100644
--- a/numpy/core/src/multiarray/item_selection.c
+++ b/numpy/core/src/multiarray/item_selection.c
@@ -174,15 +174,16 @@ 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 < 0) {
- tmp = tmp + max_item;
- }
- if ((tmp < 0) || (tmp >= max_item)) {
- PyErr_SetString(PyExc_IndexError,
- "index out of range for array");
+ 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);
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,
@@ -252,15 +253,16 @@ 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);
+ goto fail;
+ }
if (tmp < 0) {
tmp = tmp + max_item;
}
- if ((tmp < 0) || (tmp >= max_item)) {
- PyErr_SetString(PyExc_IndexError,
- "index out of range "\
- "for array");
- goto fail;
- }
memmove(dest, src + tmp*chunk, chunk);
dest += chunk;
}
@@ -393,6 +395,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0,
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");
@@ -449,6 +452,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0,
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");
@@ -2531,7 +2535,9 @@ PyArray_MultiIndexGetItem(PyArrayObject *self, npy_intp *multi_index)
}
if (ind < 0 || ind >= shapevalue) {
- PyErr_SetString(PyExc_ValueError, "index out of bounds");
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" out of bounds in dimension %d",
+ multi_index[idim], idim);
return NULL;
}
@@ -2560,7 +2566,9 @@ PyArray_MultiIndexGetItem(PyArrayObject *self, npy_intp *multi_index)
}
if (ind < 0 || ind >= shapevalue) {
- PyErr_SetString(PyExc_ValueError, "index out of bounds");
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" out of bounds in dimension %d",
+ multi_index[idim], idim);
return NULL;
}
@@ -2608,7 +2616,9 @@ PyArray_MultiIndexSetItem(PyArrayObject *self, npy_intp *multi_index,
}
if (ind < 0 || ind >= shapevalue) {
- PyErr_SetString(PyExc_ValueError, "index out of bounds");
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" out of bounds in dimension %d",
+ multi_index[idim], idim);
return -1;
}
@@ -2650,7 +2660,9 @@ PyArray_MultiIndexSetItem(PyArrayObject *self, npy_intp *multi_index,
}
if (ind < 0 || ind >= shapevalue) {
- PyErr_SetString(PyExc_ValueError, "index out of bounds");
+ PyErr_Format(PyExc_IndexError,
+ "index %"NPY_INTP_FMT" out of bounds in dimension %d",
+ multi_index[idim], idim);
return -1;
}
diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c
index 262be6443..e4bc1a336 100644
--- a/numpy/core/src/multiarray/iterators.c
+++ b/numpy/core/src/multiarray/iterators.c
@@ -73,6 +73,7 @@ parse_index_entry(PyObject *op, npy_intp *step_size,
i += max;
}
if (i >= max || i < 0) {
+ /* TODO: should report out-of-range indices with axis */
PyErr_SetString(PyExc_IndexError, "invalid index");
goto fail;
}
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c
index ec746481c..779769e23 100644
--- a/numpy/core/src/multiarray/mapping.c
+++ b/numpy/core/src/multiarray/mapping.c
@@ -57,13 +57,15 @@ 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);
+ return NULL;
+ }
if (i < 0) {
i += dim0;
}
- if (i < 0 || i >= dim0) {
- PyErr_SetString(PyExc_IndexError,"index out of bounds");
- return NULL;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
/* Create the view array */
@@ -121,13 +123,15 @@ 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);
+ return NULL;
+ }
if (i < 0) {
i += dim0;
}
- if (i < 0 || i >= dim0) {
- PyErr_SetString(PyExc_IndexError,"index out of bounds");
- return NULL;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
if (PyArray_HASMASKNA(self)) {
@@ -202,13 +206,15 @@ 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);
+ return -1;
+ }
if (i < 0) {
i += dim0;
}
- if (i < 0 || i >= dim0) {
- PyErr_SetString(PyExc_IndexError,"index out of bounds");
- return -1;
- }
item = PyArray_DATA(self) + i * PyArray_STRIDE(self, 0);
return PyArray_DESCR(self)->f->setitem(v, item, self);