summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-12-15 10:17:32 -0600
committerGitHub <noreply@github.com>2021-12-15 18:17:32 +0200
commitd3881b360367d2e5b1e44efcb6653c82c36ca68c (patch)
treeb12f1b40de98cb5e360ad3189dcb1fcaa44980ed /numpy
parent7d2bb368020c116433f8f8dfdbc63925efc23ddd (diff)
downloadnumpy-d3881b360367d2e5b1e44efcb6653c82c36ca68c.tar.gz
BUG: Fix issues (mainly) found using pytest-leaks (#20583)
* BUG: Fix issues (mainly) found using pytest-leaks This fixes mainly simple reference counting issues found with pytest-leaks. However, two of them are more complicated (will comment on them in the PR). This may not be the full picture yet, leak-test still running and to be sure I may need to restart it once more by now (it takes more than 1 day to complete). * Undo setitem changes to see if they trip windows tests The do seem a bit fishy, may be better to remove any special casing (even if that may require copying the "nbytes logic" from ctors.c
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/conversion_utils.c3
-rw-r--r--numpy/core/src/multiarray/ctors.c1
-rw-r--r--numpy/core/src/umath/dispatching.c7
-rw-r--r--numpy/core/src/umath/ufunc_object.c16
4 files changed, 17 insertions, 10 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c
index ef101a78b..a1de580d9 100644
--- a/numpy/core/src/multiarray/conversion_utils.c
+++ b/numpy/core/src/multiarray/conversion_utils.c
@@ -172,7 +172,7 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) {
}
int int_copymode;
- PyObject* numpy_CopyMode = NULL;
+ static PyObject* numpy_CopyMode = NULL;
npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode);
if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) {
@@ -182,6 +182,7 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) {
}
int_copymode = (int)PyLong_AsLong(mode_value);
+ Py_DECREF(mode_value);
if (error_converting(int_copymode)) {
return NPY_FAIL;
}
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 487f0d4a9..5ece33e8c 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1964,6 +1964,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
if (flags & NPY_ARRAY_ENSURENOCOPY ) {
PyErr_SetString(PyExc_ValueError,
"Unable to avoid copy while creating an array from given array.");
+ Py_DECREF(newtype);
return NULL;
}
diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c
index 7bfc0a442..cfeb0b17a 100644
--- a/numpy/core/src/umath/dispatching.c
+++ b/numpy/core/src/umath/dispatching.c
@@ -742,14 +742,15 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
}
info = promote_and_get_info_and_ufuncimpl(ufunc,
ops, signature, new_op_dtypes, NPY_FALSE);
+ for (int i = 0; i < ufunc->nargs; i++) {
+ Py_XDECREF(new_op_dtypes[i]);
+ }
+
/* Add this to the cache using the original types: */
if (cacheable && PyArrayIdentityHash_SetItem(ufunc->_dispatch_cache,
(PyObject **)op_dtypes, info, 0) < 0) {
return NULL;
}
- for (int i = 0; i < ufunc->nargs; i++) {
- Py_XDECREF(new_op_dtypes);
- }
return info;
}
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c
index 1b310b471..78f6f4b5a 100644
--- a/numpy/core/src/umath/ufunc_object.c
+++ b/numpy/core/src/umath/ufunc_object.c
@@ -2738,12 +2738,10 @@ reducelike_promote_and_resolve(PyUFuncObject *ufunc,
PyArrayMethodObject *ufuncimpl = promote_and_get_ufuncimpl(ufunc,
ops, signature, operation_DTypes, NPY_FALSE, NPY_TRUE, NPY_TRUE);
- /* Output can currently get cleared, others XDECREF in case of error */
+ /* DTypes may currently get filled in fallbacks and XDECREF for error: */
+ Py_XDECREF(operation_DTypes[0]);
Py_XDECREF(operation_DTypes[1]);
- if (out != NULL) {
- Py_XDECREF(operation_DTypes[0]);
- Py_XDECREF(operation_DTypes[2]);
- }
+ Py_XDECREF(operation_DTypes[2]);
if (ufuncimpl == NULL) {
return NULL;
}
@@ -4892,6 +4890,7 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
*/
Py_XDECREF(wheremask);
for (int i = 0; i < nop; i++) {
+ Py_DECREF(signature[i]);
Py_XDECREF(operand_DTypes[i]);
Py_DECREF(operation_descrs[i]);
if (i < nin) {
@@ -4915,6 +4914,7 @@ fail:
Py_XDECREF(wheremask);
for (int i = 0; i < ufunc->nargs; i++) {
Py_XDECREF(operands[i]);
+ Py_XDECREF(signature[i]);
Py_XDECREF(operand_DTypes[i]);
Py_XDECREF(operation_descrs[i]);
if (i < nout) {
@@ -6154,7 +6154,9 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args)
Py_XDECREF(op2_array);
Py_XDECREF(iter);
Py_XDECREF(iter2);
- for (int i = 0; i < 3; i++) {
+ for (int i = 0; i < nop; i++) {
+ Py_DECREF(signature[i]);
+ Py_XDECREF(operand_DTypes[i]);
Py_XDECREF(operation_descrs[i]);
Py_XDECREF(array_operands[i]);
}
@@ -6180,6 +6182,8 @@ fail:
Py_XDECREF(iter);
Py_XDECREF(iter2);
for (int i = 0; i < 3; i++) {
+ Py_XDECREF(signature[i]);
+ Py_XDECREF(operand_DTypes[i]);
Py_XDECREF(operation_descrs[i]);
Py_XDECREF(array_operands[i]);
}