Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions numpy/_core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,9 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
PyErr_Clear();

PyArrayObject *array_other = (PyArrayObject *)PyArray_FROM_O(other);
if (array_other == NULL) {
return NULL;
}
if (PyArray_TYPE(array_other) == NPY_VOID) {
/*
* Void arrays are currently not handled by ufuncs, so if the other
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject* out,
/* set copy-back */
Py_INCREF(out);
if (PyArray_SetWritebackIfCopyBase(out_buf, out) < 0) {
Py_DECREF(out);
Py_DECREF(out_buf);
// PyArray_SetWritebackIfCopyBase steals reference to second argument
return NULL;
}
}
Expand Down
12 changes: 10 additions & 2 deletions numpy/_core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ get_legacy_print_mode(void) {
PyObject *legacy_print_mode = NULL;
if (PyDict_GetItemRef(format_options, npy_interned_str.legacy,
&legacy_print_mode) == -1) {
Py_DECREF(format_options);
return -1;
}
Py_DECREF(format_options);
Expand Down Expand Up @@ -303,6 +304,7 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd,
n = PyArray_DIMS(ap)[0];
ptr2 = (char **)PyArray_malloc(n * sizeof(char *));
if (!ptr2) {
Py_DECREF(ap);
PyErr_NoMemory();
return -1;
}
Expand All @@ -316,6 +318,7 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd,
m = PyArray_DIMS(ap)[1];
ptr3 = (char ***)PyArray_malloc(n*(m+1) * sizeof(char *));
if (!ptr3) {
Py_DECREF(ap);
PyErr_NoMemory();
return -1;
}
Expand Down Expand Up @@ -2305,7 +2308,9 @@ array_count_nonzero(PyObject *NPY_UNUSED(self), PyObject *const *args, Py_ssize_
if (descr == NULL) {
return NULL;
}
return PyArray_Scalar(&count, descr, NULL);
PyObject *result = PyArray_Scalar(&count, descr, NULL);
Py_DECREF(descr);
return result;
}

static PyObject *
Expand Down Expand Up @@ -3228,6 +3233,7 @@ PyArray_Where(PyObject *condition, PyObject *x, PyObject *y)
PyArrayObject *arr = NULL, *ax = NULL, *ay = NULL;
PyObject *ret = NULL;
PyArray_Descr *common_dt = NULL;
NpyIter *iter = NULL;

arr = (PyArrayObject *)PyArray_FROM_O(condition);
if (arr == NULL) {
Expand Down Expand Up @@ -3297,7 +3303,6 @@ PyArray_Where(PyObject *condition, PyObject *x, PyObject *y)
/* `PyArray_DescrFromType` cannot fail for simple builtin types: */
PyArray_Descr * op_dt[4] = {common_dt, PyArray_DescrFromType(NPY_BOOL), x_dt, y_dt};

NpyIter * iter;
NPY_BEGIN_THREADS_DEF;

iter = NpyIter_MultiNew(
Expand Down Expand Up @@ -3431,6 +3436,9 @@ PyArray_Where(PyObject *condition, PyObject *x, PyObject *y)
Py_XDECREF(common_dt);
NPY_cast_info_xfree(&x_cast_info);
NPY_cast_info_xfree(&y_cast_info);
if (iter != NULL) {
NpyIter_Deallocate(iter);
}
return NULL;
}

Expand Down
13 changes: 11 additions & 2 deletions numpy/_core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ _get_keywords(int rtype, PyArrayObject *out)
PyObject *kwds = NULL;
if (rtype != NPY_NOTYPE || out != NULL) {
kwds = PyDict_New();
if (kwds == NULL) {
return NULL;
}
if (rtype != NPY_NOTYPE) {
PyArray_Descr *descr;
descr = PyArray_DescrFromType(rtype);
Expand All @@ -169,13 +172,16 @@ PyArray_GenericReduceFunction(PyArrayObject *m1, PyObject *op, int axis,
PyObject *kwds;

args = Py_BuildValue("(Oi)", m1, axis);
if (args == NULL) {
return NULL;
}
kwds = _get_keywords(rtype, out);
meth = PyObject_GetAttrString(op, "reduce");
if (meth && PyCallable_Check(meth)) {
ret = PyObject_Call(meth, args, kwds);
}
Py_DECREF(args);
Py_DECREF(meth);
Py_XDECREF(meth);
Py_XDECREF(kwds);
return ret;
}
Expand All @@ -189,13 +195,16 @@ PyArray_GenericAccumulateFunction(PyArrayObject *m1, PyObject *op, int axis,
PyObject *kwds;

args = Py_BuildValue("(Oi)", m1, axis);
if (args == NULL) {
return NULL;
}
kwds = _get_keywords(rtype, out);
meth = PyObject_GetAttrString(op, "accumulate");
if (meth && PyCallable_Check(meth)) {
ret = PyObject_Call(meth, args, kwds);
}
Py_DECREF(args);
Py_DECREF(meth);
Py_XDECREF(meth);
Py_XDECREF(kwds);
return ret;
}
Expand Down
Loading