Skip to content

Commit

Permalink
BUG: Clear error before constructing error message using calls to `Py…
Browse files Browse the repository at this point in the history
…Object_Repr`. Also, do a better job of handling any errors raised while constructing the error message.
  • Loading branch information
jakirkham committed Jan 12, 2016
1 parent 67592d3 commit bab118d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1926,14 +1926,34 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
/* Raise an error if the casting rule isn't followed */
if (!PyArray_CanCastArrayTo(arr, newtype, casting)) {
PyObject *errmsg;
PyArray_Descr *arr_descr = NULL;
PyObject *arr_descr_repr = NULL;
PyObject *newtype_repr = NULL;

PyErr_Clear();
errmsg = PyUString_FromString("Cannot cast array data from ");
PyUString_ConcatAndDel(&errmsg,
PyObject_Repr((PyObject *)PyArray_DESCR(arr)));
arr_descr = PyArray_DESCR(arr);
if (arr_descr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
arr_descr_repr = PyObject_Repr((PyObject *)arr_descr);
if (arr_descr_repr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
PyUString_ConcatAndDel(&errmsg, arr_descr_repr);
PyUString_ConcatAndDel(&errmsg,
PyUString_FromString(" to "));
PyUString_ConcatAndDel(&errmsg,
PyObject_Repr((PyObject *)newtype));
newtype_repr = PyObject_Repr((PyObject *)newtype);
if (newtype_repr == NULL) {
Py_DECREF(newtype);
Py_DECREF(errmsg);
return NULL;
}
PyUString_ConcatAndDel(&errmsg, newtype_repr);
PyUString_ConcatAndDel(&errmsg,
PyUString_FromFormat(" according to the rule %s",
npy_casting_to_string(casting)));
Expand Down

0 comments on commit bab118d

Please sign in to comment.