From 3b2473599bf1aea6f1da3c6f7d9049e871f22bdd Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 21 Jun 2021 10:14:33 -0500 Subject: [PATCH 1/2] BUG: Fix reference count leak This adds a missing decref to the signature/dtype keyword argument logic in reductions. (The code will change quite a bit after 1.21, but this avoids a reference count leak on 1.21.) --- numpy/core/src/umath/ufunc_object.c | 1 + 1 file changed, 1 insertion(+) diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 0644a28c011b..b448505df986 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -4159,6 +4159,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, } Py_INCREF(dtype->singleton); otype = dtype->singleton; + Py_DECREF(dtype); } if (out_obj && !PyArray_OutputConverter(out_obj, &out)) { goto fail; From 70ac04ce1ae76a1a6d1a997b541932de04ea964e Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 21 Jun 2021 11:55:43 -0500 Subject: [PATCH 2/2] BUG: Remove unnecessary incref in type resolution `ensure_dtype_nbo()` already increments the reference count, so the INCREF is not necessary here. --- numpy/core/src/umath/ufunc_object.c | 2 +- numpy/core/src/umath/ufunc_type_resolution.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index b448505df986..9e73dfd94a9c 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -4157,8 +4157,8 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, if (dtype == NULL) { goto fail; } - Py_INCREF(dtype->singleton); otype = dtype->singleton; + Py_INCREF(otype); Py_DECREF(dtype); } if (out_obj && !PyArray_OutputConverter(out_obj, &out)) { diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index 2834235e409f..88aa9ed6c112 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -390,7 +390,6 @@ PyUFunc_SimpleBinaryComparisonTypeResolver(PyUFuncObject *ufunc, operands, type_tup, out_dtypes); } - Py_INCREF(descr); out_dtypes[0] = ensure_dtype_nbo(descr); if (out_dtypes[0] == NULL) { return -1;