summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src74
-rw-r--r--numpy/core/src/multiarray/compiled_base.c8
-rw-r--r--numpy/core/src/multiarray/descriptor.c53
-rw-r--r--numpy/core/src/multiarray/getset.c37
-rw-r--r--numpy/core/src/umath/_umath_tests.c.src6
-rw-r--r--numpy/linalg/umath_linalg.c.src28
6 files changed, 166 insertions, 40 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 3d6a5eda8..506fecd2f 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -4687,7 +4687,7 @@ set_typeinfo(PyObject *dict)
infodict = PyDict_New();
if (infodict == NULL) return -1;
-
+ int ret;
/**begin repeat
*
* #name = BOOL,
@@ -4730,10 +4730,15 @@ set_typeinfo(PyObject *dict)
&Py@Name@ArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "@name@", s);
+ ret = PyDict_SetItemString(infodict, "@name@", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
/**end repeat**/
@@ -4753,10 +4758,15 @@ set_typeinfo(PyObject *dict)
_ALIGN(@type@), &Py@Name@ArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "@name@", s);
+ ret = PyDict_SetItemString(infodict, "@name@", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
/**end repeat**/
@@ -4766,37 +4776,57 @@ set_typeinfo(PyObject *dict)
&PyObjectArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "OBJECT", s);
+ ret = PyDict_SetItemString(infodict, "OBJECT", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
s = PyArray_typeinfo(
NPY_STRINGLTR, NPY_STRING, 0, _ALIGN(char),
&PyStringArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "STRING", s);
+ ret = PyDict_SetItemString(infodict, "STRING", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
s = PyArray_typeinfo(
NPY_UNICODELTR, NPY_UNICODE, 0, _ALIGN(npy_ucs4),
&PyUnicodeArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "UNICODE", s);
+ ret = PyDict_SetItemString(infodict, "UNICODE", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
s = PyArray_typeinfo(
NPY_VOIDLTR, NPY_VOID, 0, _ALIGN(char),
&PyVoidArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "VOID", s);
+ ret = PyDict_SetItemString(infodict, "VOID", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
s = PyArray_typeinforanged(
NPY_DATETIMELTR, NPY_DATETIME, NPY_BITSOF_DATETIME,
_ALIGN(npy_datetime),
@@ -4805,10 +4835,15 @@ set_typeinfo(PyObject *dict)
&PyDatetimeArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "DATETIME", s);
+ ret = PyDict_SetItemString(infodict, "DATETIME", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
s = PyArray_typeinforanged(
NPY_TIMEDELTALTR, NPY_TIMEDELTA, NPY_BITSOF_TIMEDELTA,
_ALIGN(npy_timedelta),
@@ -4817,15 +4852,23 @@ set_typeinfo(PyObject *dict)
&PyTimedeltaArrType_Type
);
if (s == NULL) {
+ Py_DECREF(infodict);
return -1;
}
- PyDict_SetItemString(infodict, "TIMEDELTA", s);
+ ret = PyDict_SetItemString(infodict, "TIMEDELTA", s);
Py_DECREF(s);
+ if (ret < 0) {
+ Py_DECREF(infodict);
+ return -1;
+ }
-#define SETTYPE(name) \
- Py_INCREF(&Py##name##ArrType_Type); \
- PyDict_SetItemString(infodict, #name, \
- (PyObject *)&Py##name##ArrType_Type)
+#define SETTYPE(name) \
+ Py_INCREF(&Py##name##ArrType_Type); \
+ if (PyDict_SetItemString(infodict, #name, \
+ (PyObject *)&Py##name##ArrType_Type) < 0) { \
+ Py_DECREF(infodict); \
+ return -1; \
+ }
SETTYPE(Generic);
SETTYPE(Number);
@@ -4840,8 +4883,11 @@ set_typeinfo(PyObject *dict)
#undef SETTYPE
- PyDict_SetItemString(dict, "typeinfo", infodict);
+ ret = PyDict_SetItemString(dict, "typeinfo", infodict);
Py_DECREF(infodict);
+ if (ret < 0) {
+ return -1;
+ }
return 0;
}
diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c
index 59f5e0e25..308e72009 100644
--- a/numpy/core/src/multiarray/compiled_base.c
+++ b/numpy/core/src/multiarray/compiled_base.c
@@ -1255,8 +1255,12 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
" used instead of 'dims'") < 0) {
return NULL;
}
- PyDict_SetItemString(kwds, "shape", dims_item);
- PyDict_DelItemString(kwds, "dims");
+ if (PyDict_SetItemString(kwds, "shape", dims_item) < 0) {
+ return NULL;
+ }
+ if (PyDict_DelItemString(kwds, "dims") < 0) {
+ return NULL;
+ }
}
}
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c
index ea40064ab..0f35e867c 100644
--- a/numpy/core/src/multiarray/descriptor.c
+++ b/numpy/core/src/multiarray/descriptor.c
@@ -560,11 +560,15 @@ _convert_from_array_descr(PyObject *obj, int align)
Py_DECREF(tup);
goto fail;
}
- PyDict_SetItem(fields, title, tup);
+ if (PyDict_SetItem(fields, title, tup) < 0) {
+ goto fail;
+ }
}
}
else {
- PyDict_SetItem(fields, name, tup);
+ if (PyDict_SetItem(fields, name, tup) < 0) {
+ goto fail;
+ }
}
totalsize += conv->elsize;
@@ -1210,8 +1214,12 @@ _convert_from_dict(PyObject *obj, int align)
Py_DECREF(tup);
goto fail;
}
- PyDict_SetItem(fields, name, tup);
+ int ret = PyDict_SetItem(fields, name, tup);
Py_DECREF(name);
+ if (ret < 0) {
+ Py_DECREF(tup);
+ goto fail;
+ }
if (len == 3) {
if (PyBaseString_Check(title)) {
if (PyDict_GetItemWithError(fields, title) != NULL) {
@@ -1220,7 +1228,14 @@ _convert_from_dict(PyObject *obj, int align)
Py_DECREF(tup);
goto fail;
}
- PyDict_SetItem(fields, title, tup);
+ else if (PyErr_Occurred()) {
+ /* MemoryError during dict lookup */
+ goto fail;
+ }
+ if (PyDict_SetItem(fields, title, tup) < 0) {
+ Py_DECREF(tup);
+ goto fail;
+ }
}
}
Py_DECREF(tup);
@@ -2127,7 +2142,14 @@ arraydescr_names_set(PyArray_Descr *self, PyObject *val)
self->hash = -1;
/* Update dictionary keys in fields */
new_names = PySequence_Tuple(val);
+ if (new_names == NULL) {
+ return -1;
+ }
new_fields = PyDict_New();
+ if (new_fields == NULL) {
+ Py_DECREF(new_names);
+ return -1;
+ }
for (i = 0; i < N; i++) {
PyObject *key;
PyObject *item;
@@ -2148,16 +2170,22 @@ arraydescr_names_set(PyArray_Descr *self, PyObject *val)
new_key = PyTuple_GET_ITEM(new_names, i);
/* Check for duplicates */
ret = PyDict_Contains(new_fields, new_key);
- if (ret != 0) {
- if (ret < 0) {
- PyErr_Clear();
- }
+ if (ret < 0) {
+ Py_DECREF(new_names);
+ Py_DECREF(new_fields);
+ return -1;
+ }
+ else if (ret != 0) {
PyErr_SetString(PyExc_ValueError, "Duplicate field names given.");
Py_DECREF(new_names);
Py_DECREF(new_fields);
return -1;
}
- PyDict_SetItem(new_fields, new_key, item);
+ if (PyDict_SetItem(new_fields, new_key, item) < 0) {
+ Py_DECREF(new_names);
+ Py_DECREF(new_fields);
+ return -1;
+ }
}
/* Replace names */
@@ -2981,8 +3009,13 @@ PyArray_DescrNewByteorder(PyArray_Descr *self, char newendian)
Py_INCREF(old);
PyTuple_SET_ITEM(newvalue, i, old);
}
- PyDict_SetItem(newfields, key, newvalue);
+ int ret = PyDict_SetItem(newfields, key, newvalue);
Py_DECREF(newvalue);
+ if (ret < 0) {
+ Py_DECREF(newfields);
+ Py_DECREF(new);
+ return NULL;
+ }
}
Py_DECREF(new->fields);
new->fields = newfields;
diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c
index 9d39ee7a8..9a9c51fee 100644
--- a/numpy/core/src/multiarray/getset.c
+++ b/numpy/core/src/multiarray/getset.c
@@ -272,31 +272,56 @@ array_interface_get(PyArrayObject *self)
Py_DECREF(dict);
return NULL;
}
+ int ret;
/* dataptr */
obj = array_dataptr_get(self);
- PyDict_SetItemString(dict, "data", obj);
+ ret = PyDict_SetItemString(dict, "data", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
obj = array_protocol_strides_get(self);
- PyDict_SetItemString(dict, "strides", obj);
+ ret = PyDict_SetItemString(dict, "strides", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
obj = array_protocol_descr_get(self);
- PyDict_SetItemString(dict, "descr", obj);
+ ret = PyDict_SetItemString(dict, "descr", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
obj = arraydescr_protocol_typestr_get(PyArray_DESCR(self));
- PyDict_SetItemString(dict, "typestr", obj);
+ ret = PyDict_SetItemString(dict, "typestr", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
obj = array_shape_get(self);
- PyDict_SetItemString(dict, "shape", obj);
+ ret = PyDict_SetItemString(dict, "shape", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
obj = PyInt_FromLong(3);
- PyDict_SetItemString(dict, "version", obj);
+ ret = PyDict_SetItemString(dict, "version", obj);
Py_DECREF(obj);
+ if (ret < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
return dict;
}
diff --git a/numpy/core/src/umath/_umath_tests.c.src b/numpy/core/src/umath/_umath_tests.c.src
index bd3fe80b6..672fe31ae 100644
--- a/numpy/core/src/umath/_umath_tests.c.src
+++ b/numpy/core/src/umath/_umath_tests.c.src
@@ -610,7 +610,13 @@ PyMODINIT_FUNC PyInit__umath_tests(void) {
}
import_array();
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
import_ufunc();
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
d = PyModule_GetDict(m);
diff --git a/numpy/linalg/umath_linalg.c.src b/numpy/linalg/umath_linalg.c.src
index fa1396545..e66fc9916 100644
--- a/numpy/linalg/umath_linalg.c.src
+++ b/numpy/linalg/umath_linalg.c.src
@@ -3590,7 +3590,7 @@ GUFUNC_DESCRIPTOR_t gufunc_descriptors [] = {
}
};
-static void
+static int
addUfuncs(PyObject *dictionary) {
PyObject *f;
int i;
@@ -3609,12 +3609,19 @@ addUfuncs(PyObject *dictionary) {
d->doc,
0,
d->signature);
- PyDict_SetItemString(dictionary, d->name, f);
+ if (f == NULL) {
+ return -1;
+ }
#if 0
dump_ufunc_object((PyUFuncObject*) f);
#endif
+ int ret = PyDict_SetItemString(dictionary, d->name, f);
Py_DECREF(f);
+ if (ret < 0) {
+ return -1;
+ }
}
+ return 0;
}
@@ -3654,17 +3661,22 @@ PyObject *PyInit__umath_linalg(void)
import_ufunc();
d = PyModule_GetDict(m);
+ if (d == NULL) {
+ return NULL;
+ }
version = PyString_FromString(umath_linalg_version_string);
- PyDict_SetItemString(d, "__version__", version);
+ if (version == NULL) {
+ return NULL;
+ }
+ int ret = PyDict_SetItemString(d, "__version__", version);
Py_DECREF(version);
+ if (ret < 0) {
+ return NULL;
+ }
/* Load the ufunc operators into the module's namespace */
- addUfuncs(d);
-
- if (PyErr_Occurred()) {
- PyErr_SetString(PyExc_RuntimeError,
- "cannot load _umath_linalg module.");
+ if (addUfuncs(d) < 0) {
return NULL;
}