@@ -4535,11 +4535,17 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
4535
4535
4536
4536
/* Convert the 'axis' parameter into a list of axes */
4537
4537
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
+ }
4540
4546
}
4541
- /* Convert 'None' into all the axes */
4542
4547
else if (axes_in == Py_None ) {
4548
+ /* Convert 'None' into all the axes */
4543
4549
naxes = ndim ;
4544
4550
for (i = 0 ; i < naxes ; ++ i ) {
4545
4551
axes [i ] = i ;
@@ -4564,40 +4570,28 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
4564
4570
axes [i ] = (int )axis ;
4565
4571
}
4566
4572
}
4567
- /* Try to interpret axis as an integer */
4568
4573
else {
4574
+ /* Try to interpret axis as an integer */
4569
4575
int axis = PyArray_PyIntAsInt (axes_in );
4570
4576
/* TODO: PyNumber_Index would be good to use here */
4571
4577
if (error_converting (axis )) {
4572
4578
goto fail ;
4573
4579
}
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 ) {
4587
4580
/*
4588
- * A reduction with no axes is still valid but trivial.
4589
4581
* 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
4591
4583
* though this is technically incorrect.
4592
4584
*/
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 ) {
4599
4589
goto fail ;
4600
4590
}
4591
+ else {
4592
+ axes [0 ] = (int )axis ;
4593
+ naxes = 1 ;
4594
+ }
4601
4595
}
4602
4596
4603
4597
/*
@@ -4640,6 +4634,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
4640
4634
Py_XDECREF (wheremask );
4641
4635
break ;
4642
4636
case UFUNC_ACCUMULATE :
4637
+ if (ndim == 0 ) {
4638
+ PyErr_SetString (PyExc_TypeError , "cannot accumulate on a scalar" );
4639
+ goto fail ;
4640
+ }
4643
4641
if (naxes != 1 ) {
4644
4642
PyErr_SetString (PyExc_ValueError ,
4645
4643
"accumulate does not allow multiple axes" );
@@ -4649,6 +4647,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
4649
4647
otype -> type_num );
4650
4648
break ;
4651
4649
case UFUNC_REDUCEAT :
4650
+ if (ndim == 0 ) {
4651
+ PyErr_SetString (PyExc_TypeError , "cannot reduceat on a scalar" );
4652
+ goto fail ;
4653
+ }
4652
4654
if (naxes != 1 ) {
4653
4655
PyErr_SetString (PyExc_ValueError ,
4654
4656
"reduceat does not allow multiple axes" );
0 commit comments