From f2394fe93af07b1926a9d8e49bca31a19d0b3d50 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 14 Dec 2022 11:50:51 +0100 Subject: [PATCH] BUG: Fix refcounting errors found using pytest-leaks These are the clear errors I found based on pytest-leaks. One day it would be nice to fix up pytest-leaks to better support newer versions of pytest and cleaning up fixtures... --- numpy/core/src/multiarray/convert.c | 3 +++ numpy/core/src/umath/ufunc_object.c | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c index 092a17c893cc..0ca291c7ebc8 100644 --- a/numpy/core/src/multiarray/convert.c +++ b/numpy/core/src/multiarray/convert.c @@ -393,6 +393,9 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) PyArray_BYTES(arr), PyArray_STRIDES(arr), descr, value); + if (PyDataType_REFCHK(descr)) { + PyArray_Item_XDECREF(value, descr); + } PyMem_FREE(value_buffer_heap); return retcode; } diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index b2955a6a59b3..3c977cb1cf1d 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -6456,7 +6456,6 @@ py_resolve_dtypes_generic(PyUFuncObject *ufunc, npy_bool return_context, /* Explicitly allow int, float, and complex for the "weak" types. */ else if (descr_obj == (PyObject *)&PyLong_Type) { descr = PyArray_DescrFromType(NPY_LONG); - Py_INCREF(descr); dummy_arrays[i] = (PyArrayObject *)PyArray_Empty(0, NULL, descr, 0); if (dummy_arrays[i] == NULL) { goto finish; @@ -6468,7 +6467,6 @@ py_resolve_dtypes_generic(PyUFuncObject *ufunc, npy_bool return_context, } else if (descr_obj == (PyObject *)&PyFloat_Type) { descr = PyArray_DescrFromType(NPY_DOUBLE); - Py_INCREF(descr); dummy_arrays[i] = (PyArrayObject *)PyArray_Empty(0, NULL, descr, 0); if (dummy_arrays[i] == NULL) { goto finish; @@ -6480,7 +6478,6 @@ py_resolve_dtypes_generic(PyUFuncObject *ufunc, npy_bool return_context, } else if (descr_obj == (PyObject *)&PyComplex_Type) { descr = PyArray_DescrFromType(NPY_CDOUBLE); - Py_INCREF(descr); dummy_arrays[i] = (PyArrayObject *)PyArray_Empty(0, NULL, descr, 0); if (dummy_arrays[i] == NULL) { goto finish;