Skip to content

Commit

Permalink
BUG: non-empty takes on empty axes failed for clip/wrap logic
Browse files Browse the repository at this point in the history
These did no checking for this special case. And thus, wrap would go
into infinite loops trying to adjust the index, and clip would (probably)
segfault. This raises IndexError explicitely beforehand.
  • Loading branch information
seberg committed Feb 25, 2013
1 parent 58548e6 commit 230ee3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions numpy/core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
dest = PyArray_DATA(obj);
needs_refcounting = PyDataType_REFCHK(PyArray_DESCR(self));

if ((max_item == 0) && (PyArray_SIZE(obj) != 0)) {
/* Index error, since that is the usual error for raise mode */
PyErr_SetString(PyExc_IndexError,
"cannot do a non-empty take from an empty axes.");
goto fail;
}

func = PyArray_DESCR(self)->f->fasttake;
if (func == NULL) {
switch(clipmode) {
Expand Down
4 changes: 4 additions & 0 deletions numpy/core/tests/test_indexerrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def test_arraytypes_fasttake(self):
x = np.empty((2, 3, 0, 4))
assert_raises(IndexError, x.take, [0], axis=2)
assert_raises(IndexError, x.take, [1], axis=2)
assert_raises(IndexError, x.take, [0], axis=2, mode='wrap')
assert_raises(IndexError, x.take, [0], axis=2, mode='clip')

def test_take_from_object(self):
# Check exception taking from object array
Expand All @@ -21,6 +23,8 @@ def test_take_from_object(self):
assert_raises(IndexError, d.take, [1], axis=1)
assert_raises(IndexError, d.take, [0], axis=1)
assert_raises(IndexError, d.take, [0])
assert_raises(IndexError, d.take, [0], mode='wrap')
assert_raises(IndexError, d.take, [0], mode='clip')

def test_multiindex_exceptions(self):
a = np.empty(5, dtype=object)
Expand Down

0 comments on commit 230ee3a

Please sign in to comment.