Skip to content

Commit

Permalink
Merge pull request numpy#15333 from eric-wieser/use-PyDict_GetItemWit…
Browse files Browse the repository at this point in the history
…hError

BUG: Add some missing C error handling
  • Loading branch information
mattip authored Jan 15, 2020
2 parents 9656e4c + 1b129a5 commit a5e5e51
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 40 deletions.
74 changes: 60 additions & 14 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,7 @@ set_typeinfo(PyObject *dict)
infodict = PyDict_New();
if (infodict == NULL) return -1;


int ret;
/**begin repeat
*
* #name = BOOL,
Expand Down Expand Up @@ -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**/
Expand All @@ -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**/

Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
8 changes: 6 additions & 2 deletions numpy/core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
53 changes: 43 additions & 10 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 31 additions & 6 deletions numpy/core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/src/umath/_umath_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit a5e5e51

Please sign in to comment.