10000 MAINT: Use single failure path in Py_UFunc_GenericReduction. · numpy/numpy@2fadede · GitHub
[go: up one dir, main page]

Skip to content

Commit 2fadede

Browse files
committed
MAINT: Use single failure path in Py_UFunc_GenericReduction.
This should help avoid reference leaks in future additions.
1 parent 1cb7ffd commit 2fadede

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

numpy/core/src/umath/ufunc_object.c

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
36563656
int i, naxes=0, ndim;
36573657
int axes[NPY_MAXDIMS];
36583658
PyObject *axes_in = NULL;
3659-
PyArrayObject *mp, *ret = NULL;
3659+
PyArrayObject *mp = NULL, *ret = NULL;
36603660
PyObject *op, *res = NULL;
36613661
PyObject *obj_ind, *context;
36623662
PyArrayObject *indices = NULL;
@@ -3712,19 +3712,17 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37123712
PyArray_Descr *indtype;
37133713
indtype = PyArray_DescrFromType(NPY_INTP);
< 10000 /td>
37143714
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO&O&:reduceat", reduceat_kwlist,
3715-
&op,
3716-
&obj_ind,
3717-
&axes_in,
3718-
PyArray_DescrConverter2, &otype,
3719-
PyArray_OutputConverter, &out)) {
3720-
Py_XDECREF(otype);
3721-
return NULL;
3715+
&op,
3716+
&obj_ind,
3717+
&axes_in,
3718+
PyArray_DescrConverter2, &otype,
3719+
PyArray_OutputConverter, &out)) {
3720+
goto fail;
37223721
}
37233722
indices = (PyArrayObject *)PyArray_FromAny(obj_ind, indtype,
37243723
1, 1, NPY_ARRAY_CARRAY, NULL);
37253724
if (indices == NULL) {
3726-
Py_XDECREF(otype);
3727-
return NULL;
3725+
goto fail;
37283726
}
37293727
}
37303728
else if (operation == UFUNC_ACCUMULATE) {
@@ -3734,8 +3732,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37343732
&axes_in,
37353733
PyArray_DescrConverter2, &otype,
37363734
PyArray_OutputConverter, &out)) {
3737-
Py_XDECREF(otype);
3738-
return NULL;
3735+
goto fail;
37393736
}
37403737
}
37413738
else {
@@ -3746,8 +3743,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37463743
PyArray_DescrConverter2, &otype,
37473744
PyArray_OutputConverter, &out,
37483745
&keepdims)) {
3749-
Py_XDECREF(otype);
3750-
return NULL;
3746+
goto fail;
37513747
}
37523748
}
37533749
/* Ensure input is an array */
@@ -3760,7 +3756,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37603756
mp = (PyArrayObject *)PyArray_FromAny(op, NULL, 0, 0, 0, context);
37613757
Py_XDECREF(context);
37623758
if (mp == NULL) {
3763-
return NULL;
3759+
goto fail;
37643760
}
37653761

37663762
ndim = PyArray_NDIM(mp);
@@ -3771,9 +3767,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37713767
PyErr_Format(PyExc_TypeError,
37723768
"cannot perform %s with flexible type",
37733769
_reduce_type[operation]);
3774-
Py_XDECREF(otype);
3775-
Py_DECREF(mp);
3776-
return NULL;
3770+
goto fail;
37773771
}
37783772

37793773
/* Convert the 'axis' parameter into a list of axes */
@@ -3793,22 +3787,16 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
37933787
if (naxes < 0 || naxes > NPY_MAXDIMS) {
37943788
PyErr_SetString(PyExc_ValueError,
37953789
"too many values for 'axis'");
3796-
Py_XDECREF(otype);
3797-
Py_DECREF(mp);
3798-
return NULL;
3790+
goto fail;
37993791
}
38003792
for (i = 0; i < naxes; ++i) {
38013793
PyObject *tmp = PyTuple_GET_ITEM(axes_in, i);
38023794
int axis = PyArray_PyIntAsInt(tmp);
38033795
if (error_converting(axis)) {
3804-
Py_XDECREF(otype);
3805-
Py_DECREF(mp);
3806-
return NULL;
3796+
goto fail;
38073797
}
38083798
if (check_and_adjust_axis(&axis, ndim) < 0) {
3809-
Py_XDECREF(otype);
3810-
Py_DECREF(mp);
3811-
return NULL;
3799+
goto fail;
38123800
}
38133801
axes[i] = (int)axis;
38143802
}
@@ -3818,18 +3806,14 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
38183806
int axis = PyArray_PyIntAsInt(axes_in);
38193807
/* TODO: PyNumber_Index would be good to use here */
38203808
if (error_converting(axis)) {
3821-
Py_XDECREF(otype);
3822-
Py_DECREF(mp);
3823-
return NULL;
3809+
goto fail;
38243810
}
38253811
/* Special case letting axis={0 or -1} slip through for scalars */
38263812
if (ndim == 0 && (axis == 0 || axis == -1)) {
38273813
axis = 0;
38283814
}
38293815
else if (check_and_adjust_axis(&axis, ndim) < 0) {
3830-
Py_XDECREF(otype);
3831-
Py_DECREF(mp);
3832-
return NULL;
3816+
goto fail;
38333817
}
38343818
axes[0] = (int)axis;
38353819
naxes = 1;
@@ -3849,9 +3833,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
38493833
(naxes == 0 || (naxes == 1 && axes[0] == 0)))) {
38503834
PyErr_Format(PyExc_TypeError, "cannot %s on a scalar",
38513835
_reduce_type[operation]);
3852-
Py_XDECREF(otype);
3853-
Py_DECREF(mp);
3854-
return NULL;
3836+
goto fail;
38553837
}
38563838
}
38573839

@@ -3897,9 +3879,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
38973879
if (naxes != 1) {
38983880
PyErr_SetString(PyExc_ValueError,
38993881
"accumulate does not allow multiple axes");
3900-
Py_XDECREF(otype);
3901-
Py_DECREF(mp);
3902-
return NULL;
3882+
goto fail;
39033883
}
39043884
ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, mp, out, axes[0],
39053885
otype->type_num);
@@ -3908,9 +3888,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
39083888
if (naxes != 1) {
39093889
PyErr_SetString(PyExc_ValueError,
39103890
"reduceat does not allow multiple axes");
3911-
Py_XDECREF(otype);
3912-
Py_DECREF(mp);
3913-
return NULL;
3891+
goto fail;
39143892
}
39153893
ret = (PyArrayObject *)PyUFunc_Reduceat(ufunc, mp, indices, out,
39163894
axes[0], otype->type_num);
@@ -3943,6 +3921,11 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
39433921
}
39443922
}
39453923
return PyArray_Return(ret);
3924+
3925+
fail:
3926+
Py_XDECREF(otype);
3927+
Py_XDECREF(mp);
3928+
return NULL;
39463929
}
39473930

39483931
/*

0 commit comments

Comments
 (0)
0