diff options
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 14 | ||||
-rw-r--r-- | numpy/core/tests/test_ufunc.py | 7 |
2 files changed, 14 insertions, 7 deletions
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 8de0911e0..1709795f1 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2650,6 +2650,13 @@ initialize_reduce_result(int identity, PyArrayObject *result, npy_intp *strides, *shape, *shape_orig; PyArrayObject *arr_view; int idim, ndim = PyArray_NDIM(arr); + + if (PyArray_SIZE(arr) == 0) { + PyErr_SetString(PyExc_ValueError, + "zero-size array to ufunc.reduce without identity"); + return NULL; + } + /* * TODO: Should the ufunc tell us whether it's commutative * and/or associative, and this operation can be reordered? @@ -2801,13 +2808,6 @@ PyUFunc_Reduce(PyUFuncObject *self, PyArrayObject *arr, PyArrayObject *out, axis_flags[axis] = 1; } -/* - printf("reduce.%s\n", ufunc_name); - {int i; printf("axes flags:"); - for(i = 0;i < ndim; ++i) printf("%d ", (int)axis_flags[i]); - printf("\n");} -*/ - /* Get the appropriate ufunc inner loop */ otype_final = otype; if (get_binary_op_function(self, &otype_final, diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index c05d21cc1..ad483634d 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -497,6 +497,13 @@ class TestUfunc(TestCase): assert_equal(np.logical_or.reduce(a), 3) assert_equal(np.logical_and.reduce(a), None) + def test_zerosize_reduction(self): + assert_equal(np.sum([]), 0) + assert_equal(np.prod([]), 1) + assert_equal(np.any([]), False) + assert_equal(np.all([]), True) + assert_raises(ValueError, np.max, []) + assert_raises(ValueError, np.min, []) def test_casting_out_param(self): # Test that it's possible to do casts on output |