diff options
author | Matti Picus <matti.picus@gmail.com> | 2019-04-15 00:09:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-15 00:09:27 +0300 |
commit | f5749f9df9aef4c0ec18a8968d8a02a0f18d6ea7 (patch) | |
tree | 1362cb21d8222a56866f3c24fdb520f1a0698a9a | |
parent | fb425b769bbe4ff4b5a28f58876871414cc8bb12 (diff) | |
parent | b24060e5787b5fff80f855fa105e91d989e1ca6c (diff) | |
download | numpy-f5749f9df9aef4c0ec18a8968d8a02a0f18d6ea7.tar.gz |
Merge pull request #13328 from eric-wieser/fix-weird-reduce-logic
MAINT: Tidy up error message for accumulate and reduceat
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 6a96fe296..dd5fb7e03 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -4535,11 +4535,17 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, /* Convert the 'axis' parameter into a list of axes */ if (axes_in == NULL) { - naxes = 1; - axes[0] = 0; + /* apply defaults */ + if (ndim == 0) { + naxes = 0; + } + else { + naxes = 1; + axes[0] = 0; + } } - /* Convert 'None' into all the axes */ else if (axes_in == Py_None) { + /* Convert 'None' into all the axes */ naxes = ndim; for (i = 0; i < naxes; ++i) { axes[i] = i; @@ -4564,40 +4570,28 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, axes[i] = (int)axis; } } - /* Try to interpret axis as an integer */ else { + /* Try to interpret axis as an integer */ int axis = PyArray_PyIntAsInt(axes_in); /* TODO: PyNumber_Index would be good to use here */ if (error_converting(axis)) { goto fail; } - /* Special case letting axis={0 or -1} slip through for scalars */ - if (ndim == 0 && (axis == 0 || axis == -1)) { - axis = 0; - } - else if (check_and_adjust_axis(&axis, ndim) < 0) { - goto fail; - } - axes[0] = (int)axis; - naxes = 1; - } - - /* Check to see if input is zero-dimensional. */ - if (ndim == 0) { /* - * A reduction with no axes is still valid but trivial. * As a special case for backwards compatibility in 'sum', - * 'prod', et al, also allow a reduction where axis=0, even + * 'prod', et al, also allow a reduction for scalars even * though this is technically incorrect. */ - naxes = 0; - - if (!(operation == UFUNC_REDUCE && - (naxes == 0 || (naxes == 1 && axes[0] == 0)))) { - PyErr_Format(PyExc_TypeError, "cannot %s on a scalar", - _reduce_type[operation]); + if (ndim == 0 && (axis == 0 || axis == -1)) { + naxes = 0; + } + else if (check_and_adjust_axis(&axis, ndim) < 0) { goto fail; } + else { + axes[0] = (int)axis; + naxes = 1; + } } /* @@ -4640,6 +4634,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, Py_XDECREF(wheremask); break; case UFUNC_ACCUMULATE: + if (ndim == 0) { + PyErr_SetString(PyExc_TypeError, "cannot accumulate on a scalar"); + goto fail; + } if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "accumulate does not allow multiple axes"); @@ -4649,6 +4647,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args, otype->type_num); break; case UFUNC_REDUCEAT: + if (ndim == 0) { + PyErr_SetString(PyExc_TypeError, "cannot reduceat on a scalar"); + goto fail; + } if (naxes != 1) { PyErr_SetString(PyExc_ValueError, "reduceat does not allow multiple axes"); |