diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2020-01-08 01:48:01 +0000 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2020-01-08 02:00:02 +0000 |
commit | a975ad565afb52d3a8c3f41d1f1f209b6f980310 (patch) | |
tree | d2cee5494508c136579b7e90a03fbc85f64e245b /numpy | |
parent | 6f54c7eb337627a55eff828c40ca51853618f3bd (diff) | |
download | numpy-a975ad565afb52d3a8c3f41d1f1f209b6f980310.tar.gz |
MAINT: Push down declarations in _convert_from_list
This makes it easier to reason about which variables contain mutable state.
C99 makes some of this possible, while other parts were already possible.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index bc532e93f..6e2de55c3 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -606,40 +606,33 @@ _convert_from_array_descr(PyObject *obj, int align) static PyArray_Descr * _convert_from_list(PyObject *obj, int align) { - int n, i; - int totalsize; - PyObject *fields; - PyArray_Descr *conv = NULL; - PyArray_Descr *new; - PyObject *key, *tup; - PyObject *nameslist = NULL; - int maxalign = 0; - /* Types with fields need the Python C API for field access */ - char dtypeflags = NPY_NEEDS_PYAPI; - - n = PyList_GET_SIZE(obj); + int n = PyList_GET_SIZE(obj); /* * Ignore any empty string at end which _internal._commastring * can produce */ - key = PyList_GET_ITEM(obj, n-1); - if (PyBytes_Check(key) && PyBytes_GET_SIZE(key) == 0) { + PyObject *last_item = PyList_GET_ITEM(obj, n-1); + if (PyBytes_Check(last_item) && PyBytes_GET_SIZE(last_item) == 0) { n = n - 1; } /* End ignore code.*/ - totalsize = 0; if (n == 0) { return NULL; } - nameslist = PyTuple_New(n); + PyObject *nameslist = PyTuple_New(n); if (!nameslist) { return NULL; } - fields = PyDict_New(); - for (i = 0; i < n; i++) { - tup = PyTuple_New(2); - key = PyUString_FromFormat("f%d", i); - conv = _arraydescr_run_converter(PyList_GET_ITEM(obj, i), align); + PyObject *fields = PyDict_New(); + + /* Types with fields need the Python C API for field access */ + char dtypeflags = NPY_NEEDS_PYAPI; + int maxalign = 0; + int totalsize = 0; + for (int i = 0; i < n; i++) { + PyObject *tup = PyTuple_New(2); + PyObject *key = PyUString_FromFormat("f%d", i); + PyArray_Descr *conv = _arraydescr_run_converter(PyList_GET_ITEM(obj, i), align); if (conv == NULL) { Py_DECREF(tup); Py_DECREF(key); @@ -662,7 +655,7 @@ _convert_from_list(PyObject *obj, int align) PyTuple_SET_ITEM(nameslist, i, key); totalsize += conv->elsize; } - new = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *new = PyArray_DescrNewFromType(NPY_VOID); new->fields = fields; new->names = nameslist; new->flags = dtypeflags; |