summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2013-02-19 16:52:32 +0100
committerSebastian Berg <sebastian@sipsolutions.net>2013-02-25 01:01:51 +0100
commit230ee3aa201552a8a9fa13c4b319f68cbd504d85 (patch)
tree2879a7be8c58354f119f8bfc96ed6741489fbd49
parent58548e66d5d3bda3e884ae0c0ab0805ab0160484 (diff)
downloadnumpy-230ee3aa201552a8a9fa13c4b319f68cbd504d85.tar.gz
BUG: non-empty takes on empty axes failed for clip/wrap logic
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.
-rw-r--r--numpy/core/src/multiarray/item_selection.c7
-rw-r--r--numpy/core/tests/test_indexerrors.py4
2 files changed, 11 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c
index 7246e805c..4adecf193 100644
--- a/numpy/core/src/multiarray/item_selection.c
+++ b/numpy/core/src/multiarray/item_selection.c
@@ -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) {
diff --git a/numpy/core/tests/test_indexerrors.py b/numpy/core/tests/test_indexerrors.py
index af10df9b3..2f9c5d4d2 100644
--- a/numpy/core/tests/test_indexerrors.py
+++ b/numpy/core/tests/test_indexerrors.py
@@ -10,6 +10,8 @@ class TestIndexErrors(TestCase):
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
@@ -21,6 +23,8 @@ class TestIndexErrors(TestCase):
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)