10000 Merge pull request #13328 from eric-wieser/fix-weird-reduce-logic · pentschev/numpy@f5749f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5749f9

Browse files
authored
Merge pull request numpy#13328 from eric-wieser/fix-weird-reduce-logic
MAINT: Tidy up error message for accumulate and reduceat
2 parents fb425b7 + b24060e commit f5749f9

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

numpy/core/src/umath/ufunc_object.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4535,11 +4535,17 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
45354535

45364536
/* Convert the 'axis' parameter into a list of axes */
45374537
if (axes_in == NULL) {
4538-
naxes = 1;
4539-
axes[0] = 0;
4538+
/* apply defaults */
4539+
if (ndim == 0) {
4540+
naxes = 0;
4541+
}
4542+
else {
4543+
naxes = 1;
4544+
axes[0] = 0;
4545+
}
45404546
}
4541-
/* Convert 'None' into all the axes */
45424547
else if (axes_in == Py_None) {
4548+
/* Convert 'None' into all the axes */
45434549
naxes = ndim;
45444550
for (i = 0; i < naxes; ++i) {
45454551
axes[i] = i;
@@ -4564,40 +4570,28 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
45644570
axes[i] = (int)axis;
45654571
}
45664572
}
4567-
/* Try to interpret axis as an integer */
45684573
else {
4574+
/* Try to interpret axis as an integer */
45694575
int axis = PyArray_PyIntAsInt(axes_in);
45704576
/* TODO: PyNumber_Index would be good to use here */
45714577
if (error_converting(axis)) {
45724578
goto fail;
45734579
}
4574-
/* Special case letting axis={0 or -1} slip through for scalars */
4575-
if (ndim == 0 && (axis == 0 || axis == -1)) {
4576-
axis = 0;
4577-
}
4578-
else if (check_and_adjust_axis(&axis, ndim) < 0) {
4579-
goto fail;
4580-
}
4581-
axes[0] = (int)axis;
4582-
naxes = 1;
4583-
}
4584-
4585-
/* Check to see if input is zero-dimensional. */
4586-
if (ndim == 0) {
45874580
/*
4588-
* A reduction with no axes is still valid but trivial.
45894581
* As a special case for backwards compatibility in 'sum',
4590-
* 'prod', et al, also allow a reduction where axis=0, even
4582+
* 'prod', et al, also allow a reduction for scalars even
45914583
* though this is technically incorrect.
45924584
*/
4593-
naxes = 0;
4594-
4595-
if (!(operation == UFUNC_REDUCE &&
4596-
(naxes == 0 || (naxes == 1 && axes[0] == 0)))) {
4597-
PyErr_Format(PyExc_TypeError, "cannot %s on a scalar",
4598-
_reduce_type[operation]);
4585+
if (ndim == 0 && (axis == 0 || axis == -1)) {
4586+
naxes = 0;
4587+
}
4588+
else if (check_and_adjust_axis(&axis, ndim) < 0) {
45994589
goto fail;
46004590
}
4591+
else {
4592+
axes[0] = (int)axis;
4593+
naxes = 1;
4594+
}
46014595
}
46024596

46034597
/*
@@ -4640,6 +4634,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
46404634
Py_XDECREF(wheremask);
46414635
break;
46424636
case UFUNC_ACCUMULATE:
4637+
if (ndim == 0) {
4638+
PyErr_SetString(PyExc_TypeError, "cannot accumulate on a scalar");
4639+
goto fail;
4640+
}
46434641
if (naxes != 1) {
46444642
PyErr_SetString(PyExc_ValueError,
46454643
"accumulate does not allow multiple axes");
@@ -4649,6 +4647,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
46494647
otype->type_num);
46504648
break;
46514649
case UFUNC_REDUCEAT:
4650+
if (ndim == 0) {
4651+
PyErr_SetString(PyExc_TypeError, "cannot reduceat on a scalar");
4652+
goto fail;
4653+
}
46524654
if (naxes != 1) {
46534655
PyErr_SetString(PyExc_ValueError,
46544656
"reduceat does not allow multiple axes");

0 commit comments

Comments
 (0)
0