summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaime <jaime.frio@gmail.com>2015-05-04 21:03:22 -0700
committerJaime <jaime.frio@gmail.com>2015-05-04 21:03:22 -0700
commitbdfea0af2036e74396b78e076e90170eddbbf670 (patch)
treeb0d7f554da4cb5b6a4cc5afdd318f447f88952fe
parent93bcedfd4e31e4e1eb92a5abf0de46464a154953 (diff)
parentba29c7b224618b069644e747d6e9236f88accad4 (diff)
downloadnumpy-bdfea0af2036e74396b78e076e90170eddbbf670.tar.gz
Merge pull request #5821 from behzadnouri/place-segfault
BUG: fixes segfault in np.place when vals is empty
-rw-r--r--numpy/core/src/multiarray/compiled_base.c17
-rw-r--r--numpy/lib/tests/test_function_base.py8
2 files changed, 25 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c
index 76c5e9c25..825ca8c7a 100644
--- a/numpy/core/src/multiarray/compiled_base.c
+++ b/numpy/core/src/multiarray/compiled_base.c
@@ -344,6 +344,7 @@ arr_insert_loop(char *mptr, char *vptr, char *input_data, char *zero,
/* If we move past value data. Reset */
if (copied >= numvals) {
vptr = avals_data;
+ copied = 0;
}
}
mptr += melsize;
@@ -427,6 +428,21 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
}
objarray = (PyArray_DESCR(ainput)->type_num == NPY_OBJECT);
+ if (!numvals) {
+ /* nothing to insert! fail unless none of mask is true */
+ const char *iter = mptr;
+ const char *const last = iter + PyArray_NBYTES(amask);
+ while (iter != last && !memcmp(iter, zero, melsize)) {
+ iter += melsize;
+ }
+ if (iter != last) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot insert from an empty array!");
+ goto fail;
+ }
+ goto finish;
+ }
+
/* Handle zero-dimensional case separately */
if (nd == 0) {
if (memcmp(mptr,zero,melsize) != 0) {
@@ -461,6 +477,7 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
NPY_END_ALLOW_THREADS;
}
+finish:
Py_DECREF(amask);
Py_DECREF(avals);
PyDataMem_FREE(zero);
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index cf9fcf5e2..12f9d414b 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -626,6 +626,14 @@ class TestExtins(TestCase):
place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6])
assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7])
+ place(a, np.zeros(7), [])
+ assert_array_equal(a, np.arange(1, 8))
+
+ place(a, [1, 0, 1, 0, 1, 0, 1], [8, 9])
+ assert_array_equal(a, [8, 2, 9, 4, 8, 6, 9])
+ assert_raises_regex(ValueError, "Cannot insert from an empty array",
+ lambda: place(a, [0, 0, 0, 0, 0, 1, 0], []))
+
def test_both(self):
a = rand(10)
mask = a > 0.5