8000 BUG: Ufunc reduce reference leak (backport) by mhvk · Pull Request #10200 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Ufunc reduce reference leak (backport) #10200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 27 additions & 42 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ make_arr_prep_args(npy_intp nin, PyObject *args, PyObject *kwds)
/*
* Validate the core dimensions of all the operands, and collect all of
* the labelled core dimensions into 'core_dim_sizes'.
*
*
* Returns 0 on success, and -1 on failure
*
* The behavior has been changed in NumPy 1.10.0, and the following
Expand Down Expand Up @@ -3656,7 +3656,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
int i, naxes=0, ndim;
int axes[NPY_MAXDIMS];
PyObject *axes_in = NULL;
PyArrayObject *mp, *ret = NULL;
PyArrayObject *mp = NULL, *ret = NULL;
PyObject *op, *res = NULL;
PyObject *obj_ind, *context;
PyArrayObject *indices = NULL;
Expand Down Expand Up @@ -3707,24 +3707,22 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
PyDict_SetItem(kwds, npy_um_str_out, out_obj);
}
}

if (operation == UFUNC_REDUCEAT) {
PyArray_Descr *indtype;
indtype = PyArray_DescrFromType(NPY_INTP);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO&O&:reduceat", reduceat_kwlist,
&op,
&obj_ind,
&axes_in,
PyArray_DescrConverter2, &otype,
PyArray_OutputConverter, &out)) {
Py_XDECREF(otype);
return NULL;
&op,
&obj_ind,
&axes_in,
PyArray_DescrConverter2, &otype,
PyArray_OutputConverter, &out)) {
goto fail;
}
indices = (PyArrayObject *)PyArray_FromAny(obj_ind, indtype,
1, 1, NPY_ARRAY_CARRAY, NULL);
if (indices == NULL) {
Py_XDECREF(otype);
return NULL;
goto fail;
}
}
else if (operation == UFUNC_ACCUMULATE) {
Expand All @@ -3734,8 +3732,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
&axes_in,
PyArray_DescrConverter2, &otype,
PyArray_OutputConverter, &out)) {
Py_XDECREF(otype);
return NULL;
goto fail;
}
}
else {
Expand All @@ -3746,8 +3743,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
PyArray_DescrConverter2, &otype,
PyArray_OutputConverter, &out,
&keepdims)) {
Py_XDECREF(otype);
return NULL;
goto fail;
}
}
/* Ensure input is an array */
Expand All @@ -3760,7 +3756,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
mp = (PyArrayObject *)PyArray_FromAny(op, NULL, 0, 0, 0, context);
Py_XDECREF(context);
if (mp == NULL) {
return NULL;
goto fail;
}

ndim = PyArray_NDIM(mp);
Expand All @@ -3771,9 +3767,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
PyErr_Format(PyExc_TypeError,
"cannot perform %s with flexible type",
_reduce_type[operation]);
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}

/* Convert the 'axis' parameter into a list of axes */
Expand All @@ -3793,22 +3787,16 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
if (naxes < 0 || naxes > NPY_MAXDIMS) {
PyErr_SetString(PyExc_ValueError,
"too many values for 'axis'");
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
for (i = 0; i < naxes; ++i) {
PyObject *tmp = PyTuple_GET_ITEM(axes_in, i);
int axis = PyArray_PyIntAsInt(tmp);
if (error_converting(axis)) {
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
if (check_and_adjust_axis(&axis, ndim) < 0) {
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
axes[i] = (int)axis;
}
Expand All @@ -3818,16 +3806,14 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
int axis = PyArray_PyIntAsInt(axes_in);
/* TODO: PyNumber_Index would be good to use here */
if (error_converting(axis)) {
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
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) {
return NULL;
goto fail;
}
axes[0] = (int)axis;
naxes = 1;
Expand All @@ -3847,9 +3833,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
(naxes == 0 || (naxes == 1 && axes[0] == 0)))) {
PyErr_Format(PyExc_TypeError, "cannot %s on a scalar",
_reduce_type[operation]);
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
}

Expand Down Expand Up @@ -3895,9 +3879,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
if (naxes != 1) {
PyErr_SetString(PyExc_ValueError,
"accumulate does not allow multiple axes");
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, mp, out, axes[0],
otype->type_num);
Expand All @@ -3906,9 +3888,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
if (naxes != 1) {
PyErr_SetString(PyExc_ValueError,
"reduceat does not allow multiple axes");
Py_XDECREF(otype);
Py_DECREF(mp);
return NULL;
goto fail;
}
ret = (PyArrayObject *)PyUFunc_Reduceat(ufunc, mp, indices, out,
axes[0], otype->type_num);
Expand Down Expand Up @@ -3941,6 +3921,11 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyObject *args,
}
}
return PyArray_Return(ret);

fail:
Py_XDECREF(otype);
Py_XDECREF(mp);
return NULL;
}

/*
Expand Down
0