summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-07-26 20:50:25 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-07-26 20:50:25 +0000
commit04dfae6d7468213950012e37adeb86ab82efcdca (patch)
tree3c35e56ad24672a3731ac5ba8f792c1e78cf33e2 /numpy
parentfd96933a5f4e3cf0aba5336a3de713069be83a2b (diff)
downloadnumpy-04dfae6d7468213950012e37adeb86ab82efcdca.tar.gz
Fix-up problems with iterator subscripting.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/arrayobject.c40
1 files changed, 33 insertions, 7 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index 854a848a2..5fbc33a9a 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -2693,8 +2693,8 @@ array_subscript(PyArrayObject *self, PyObject *op)
fancy = fancy_indexing_check(op);
if (fancy != SOBJ_NOTFANCY) {
- oned = ((self->nd == 1) && !(PyTuple_Check(op) && \
- PyTuple_GET_SIZE(op) > 1));
+ oned = ((self->nd == 1) &&
+ !(PyTuple_Check(op) && PyTuple_GET_SIZE(op) > 1));
/* wrap arguments into a mapiter object */
mit = (PyArrayMapIterObject *)\
@@ -2860,8 +2860,8 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op)
fancy = fancy_indexing_check(index);
if (fancy != SOBJ_NOTFANCY) {
- oned = ((self->nd == 1) && !(PyTuple_Check(index) && \
- PyTuple_GET_SIZE(index) > 1));
+ oned = ((self->nd == 1) &&
+ !(PyTuple_Check(index) && PyTuple_GET_SIZE(index) > 1));
mit = (PyArrayMapIterObject *) \
PyArray_MapIterNew(index, oned, fancy);
@@ -8600,7 +8600,13 @@ iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind)
"boolean index array should have 1 dimension");
return NULL;
}
- index = (ind->dimensions[0]);
+ index = ind->dimensions[0];
+ if (index > self->size) {
+ PyErr_SetString(PyExc_ValueError,
+ "too many boolean indices");
+ return NULL;
+ }
+
strides = ind->strides[0];
dptr = ind->data;
/* Get size of return array */
@@ -8652,8 +8658,18 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind)
itemsize = self->ao->descr->elsize;
if (ind->nd == 0) {
num = *((intp *)ind->data);
- PyArray_ITER_GOTO1D(self, num);
- r = PyArray_ToScalar(self->dataptr, self->ao);
+ if (num < 0) num += self->size;
+ if (num < 0 || num >= self->size) {
+ PyErr_Format(PyExc_IndexError,
+ "index %d out of bounds" \
+ " 0<=index<%d", (int) num,
+ (int) self->size);
+ r = NULL;
+ }
+ else {
+ PyArray_ITER_GOTO1D(self, num);
+ r = PyArray_ToScalar(self->dataptr, self->ao);
+ }
PyArray_ITER_RESET(self);
return r;
}
@@ -8718,6 +8734,10 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind)
int len;
len = PyTuple_GET_SIZE(ind);
if (len > 1) goto fail;
+ if (len == 0) {
+ Py_INCREF(self->ao);
+ return (PyObject *)self->ao;
+ }
ind = PyTuple_GET_ITEM(ind, 0);
}
@@ -9550,6 +9570,12 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr)
finish:
/* Here check the indexes (now that we have iteraxes) */
mit->size = PyArray_MultiplyList(mit->dimensions, mit->nd);
+ if (mit->ait->size == 0 && mit->size != 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid index into a 0-size array");
+ goto fail;
+ }
+
for (i=0; i<mit->numiter; i++) {
intp indval;
it = mit->iters[i];