Skip to content

Commit

Permalink
BUG: Fix small issues found using valgrind
Browse files Browse the repository at this point in the history
(And one found by eye looking.)
  • Loading branch information
seberg authored and charris committed Dec 13, 2021
1 parent 0bbe787 commit 351dc06
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 3 additions & 1 deletion numpy/core/src/multiarray/ctors.c
Expand Up @@ -1710,10 +1710,12 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
if (flags & NPY_ARRAY_ENSURENOCOPY ) {
PyErr_SetString(PyExc_ValueError,
"Unable to avoid copy while creating an array.");
Py_DECREF(dtype);
npy_free_coercion_cache(cache);
return NULL;
}

if (cache == 0 && newtype != NULL &&
if (cache == NULL && newtype != NULL &&
PyDataType_ISSIGNED(newtype) && PyArray_IsScalar(op, Generic)) {
assert(ndim == 0);
/*
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/src/umath/dispatching.c
Expand Up @@ -641,7 +641,7 @@ add_and_return_legacy_wrapping_ufunc_loop(PyUFuncObject *ufunc,
Py_DECREF(info);
return NULL;
}

Py_DECREF(info); /* now borrowed from the ufunc's list of loops */
return info;
}

Expand Down
4 changes: 4 additions & 0 deletions numpy/core/src/umath/legacy_array_method.c
Expand Up @@ -224,6 +224,10 @@ get_wrapped_legacy_ufunc_loop(PyArrayMethod_Context *context,
*out_loop = &generic_wrapped_legacy_loop;
*out_transferdata = get_new_loop_data(
loop, user_data, (*flags & NPY_METH_REQUIRES_PYAPI) != 0);
if (*out_transferdata == NULL) {
PyErr_NoMemory();
return -1;
}
return 0;
}

Expand Down
34 changes: 20 additions & 14 deletions numpy/core/src/umath/reduction.c
Expand Up @@ -301,20 +301,6 @@ PyUFunc_ReduceWrapper(PyArrayMethod_Context *context,

PyArrayMethod_StridedLoop *strided_loop;
NPY_ARRAYMETHOD_FLAGS flags = 0;
npy_intp fixed_strides[3];
NpyIter_GetInnerFixedStrideArray(iter, fixed_strides);
if (wheremask != NULL) {
if (PyArrayMethod_GetMaskedStridedLoop(context,
1, fixed_strides, &strided_loop, &auxdata, &flags) < 0) {
goto fail;
}
}
else {
if (context->method->get_strided_loop(context,
1, 0, fixed_strides, &strided_loop, &auxdata, &flags) < 0) {
goto fail;
}
}

int needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0;
needs_api |= NpyIter_IterationNeedsAPI(iter);
Expand Down Expand Up @@ -349,6 +335,25 @@ PyUFunc_ReduceWrapper(PyArrayMethod_Context *context,
goto fail;
}

/*
* Note that we need to ensure that the iterator is reset before getting
* the fixed strides. (The buffer information is unitialized before.)
*/
npy_intp fixed_strides[3];
NpyIter_GetInnerFixedStrideArray(iter, fixed_strides);
if (wheremask != NULL) {
if (PyArrayMethod_GetMaskedStridedLoop(context,
1, fixed_strides, &strided_loop, &auxdata, &flags) < 0) {
goto fail;
}
}
else {
if (context->method->get_strided_loop(context,
1, 0, fixed_strides, &strided_loop, &auxdata, &flags) < 0) {
goto fail;
}
}

if (NpyIter_GetIterSize(iter) != 0) {
NpyIter_IterNextFunc *iternext;
char **dataptr;
Expand Down Expand Up @@ -382,6 +387,7 @@ PyUFunc_ReduceWrapper(PyArrayMethod_Context *context,
}
Py_INCREF(result);

NPY_AUXDATA_FREE(auxdata);
if (!NpyIter_Deallocate(iter)) {
Py_DECREF(result);
return NULL;
Expand Down

0 comments on commit 351dc06

Please sign in to comment.