Skip to content

Commit c280e21

Browse files
authored
Merge pull request numpy#19429 from defoishugo/fix_multiarray_leaks
BUG: Fix some multiarray leaks
2 parents 79b381f + 09326eb commit c280e21

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

numpy/core/src/multiarray/array_coercion.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ npy_new_coercion_cache(
555555
cache = PyMem_Malloc(sizeof(coercion_cache_obj));
556556
}
557557
if (cache == NULL) {
558+
Py_DECREF(arr_or_sequence);
558559
PyErr_NoMemory();
559560
return -1;
560561
}

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,14 +4675,14 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
46754675
PyObject *m, *d, *s;
46764676
PyObject *c_api;
46774677

4678-
/* Initialize CPU features */
4679-
if (npy_cpu_init() < 0) {
4680-
goto err;
4681-
}
4682-
46834678
/* Create the module and add the functions */
46844679
m = PyModule_Create(&moduledef);
46854680
if (!m) {
4681+
return NULL;
4682+
}
4683+
4684+
/* Initialize CPU features */
4685+
if (npy_cpu_init() < 0) {
46864686
goto err;
46874687
}
46884688

@@ -4934,5 +4934,6 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
49344934
PyErr_SetString(PyExc_RuntimeError,
49354935
"cannot load multiarray module.");
49364936
}
4937+
Py_DECREF(m);
49374938
return NULL;
49384939
}

numpy/core/src/multiarray/scalarapi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,12 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr,
233233
PyArray_VectorUnaryFunc* castfunc;
234234

235235
descr = PyArray_DescrFromScalar(scalar);
236+
if (descr == NULL) {
237+
return -1;
238+
}
236239
castfunc = PyArray_GetCastFunc(descr, outcode->type_num);
237240
if (castfunc == NULL) {
241+
Py_DECREF(descr);
238242
return -1;
239243
}
240244
if (PyTypeNum_ISEXTENDED(descr->type_num) ||
@@ -254,6 +258,7 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr,
254258
NPY_ARRAY_CARRAY, NULL);
255259
if (aout == NULL) {
256260
Py_DECREF(ain);
261+
Py_DECREF(descr);
257262
return -1;
258263
}
259264
castfunc(PyArray_DATA(ain), PyArray_DATA(aout), 1, ain, aout);

0 commit comments

Comments
 (0)