diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2020-01-07 00:36:25 +0000 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2020-01-07 19:40:52 +0000 |
commit | ecc2a607a53a735c1d55a90fdf9695bb01583240 (patch) | |
tree | ae34e41a713b4027aa8242046c6125ad564e60c4 /numpy | |
parent | 16d70e8d22dbed01c3952c0dbbc9be4ae784fbea (diff) | |
download | numpy-ecc2a607a53a735c1d55a90fdf9695bb01583240.tar.gz |
MAINT: Remove unnecessary gotos
This makes the control flow easier to reason about - notably, the only case that now falls through the `if` / `else` are `bytes` objects.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 9db565585..e03a8cbed 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1436,6 +1436,15 @@ _convert_from_type(PyObject *obj) { } } +/* + * Generate a vague error message when a function returned NULL but forgot + * to set an exception. We should aim to remove this eventually. + */ +static void +_report_generic_error(void) { + PyErr_SetString(PyExc_TypeError, "data type not understood"); +} + /*NUMPY_API * Get typenum from an object -- None goes to NPY_DEFAULT_TYPE * This function takes a Python object representing a type and converts it @@ -1610,10 +1619,10 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) /* or a tuple */ *at = _convert_from_tuple(obj, 0); if (*at == NULL){ - if (PyErr_Occurred()) { - return NPY_FAIL; + if (!PyErr_Occurred()) { + _report_generic_error(); } - goto fail; + return NPY_FAIL; } return NPY_SUCCEED; } @@ -1621,10 +1630,10 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) /* or a list */ *at = _convert_from_array_descr(obj,0); if (*at == NULL) { - if (PyErr_Occurred()) { - return NPY_FAIL; + if (!PyErr_Occurred()) { + _report_generic_error(); } - goto fail; + return NPY_FAIL; } return NPY_SUCCEED; } @@ -1632,15 +1641,16 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) /* or a dictionary */ *at = _convert_from_dict(obj,0); if (*at == NULL) { - if (PyErr_Occurred()) { - return NPY_FAIL; + if (!PyErr_Occurred()) { + _report_generic_error(); } - goto fail; + return NPY_FAIL; } return NPY_SUCCEED; } else if (PyArray_Check(obj)) { - goto fail; + _report_generic_error(); + return NPY_FAIL; } else { if (_arraydescr_from_dtype_attr(obj, at)) { @@ -1649,7 +1659,7 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) * RecursionError occurred. */ if (*at == NULL) { - goto error; + return NPY_FAIL; } return NPY_SUCCEED; } @@ -1662,8 +1672,12 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) *at = _arraydescr_from_ctypes_type(Py_TYPE(obj)); return *at ? NPY_SUCCEED : NPY_FAIL; } - goto fail; + _report_generic_error(); + return NPY_FAIL; } + + assert(PyBytes_Check(obj)); + if (PyErr_Occurred()) { goto fail; } @@ -1725,7 +1739,7 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) if (PyDataType_ISUNSIZED(*at) && (*at)->elsize != elsize) { PyArray_DESCR_REPLACE(*at); if (*at == NULL) { - goto error; + return NPY_FAIL; } (*at)->elsize = elsize; } @@ -1736,21 +1750,15 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) && (*at)->byteorder != endian) { PyArray_DESCR_REPLACE(*at); if (*at == NULL) { - goto error; + return NPY_FAIL; } (*at)->byteorder = endian; } return NPY_SUCCEED; fail: - if (PyBytes_Check(obj)) { - PyErr_Format(PyExc_TypeError, - "data type \"%s\" not understood", PyBytes_AS_STRING(obj)); - } - else { - PyErr_SetString(PyExc_TypeError, - "data type not understood"); - } + PyErr_Format(PyExc_TypeError, + "data type \"%s\" not understood", PyBytes_AS_STRING(obj)); error: *at = NULL; |