MCPcopy Create free account
hub / github.com/numpy/numpy / _convert_from_list

Function _convert_from_list

numpy/core/src/multiarray/descriptor.c:611–710  ·  view source on GitHub ↗

* a list specifying a data-type can just be * a list of formats. The names for the fields * will default to f0, f1, f2, and so forth. */

Source from the content-addressed store, hash-verified

609 * will default to f0, f1, f2, and so forth.
610 */
611static PyArray_Descr *
612_convert_from_list(PyObject *obj, int align)
613{
614 int n = PyList_GET_SIZE(obj);
615 /*
616 * Ignore any empty string at end which _internal._commastring
617 * can produce
618 */
619 PyObject *last_item = PyList_GET_ITEM(obj, n-1);
620 if (PyUnicode_Check(last_item)) {
621 Py_ssize_t s = PySequence_Size(last_item);
622 if (s < 0) {
623 return NULL;
624 }
625 if (s == 0) {
626 n = n - 1;
627 }
628 }
629 if (n == 0) {
630 PyErr_SetString(PyExc_ValueError, "Expected at least one field name");
631 return NULL;
632 }
633 PyObject *nameslist = PyTuple_New(n);
634 if (!nameslist) {
635 return NULL;
636 }
637 PyObject *fields = PyDict_New();
638 if (!fields) {
639 Py_DECREF(nameslist);
640 return NULL;
641 }
642
643 /* Types with fields need the Python C API for field access */
644 char dtypeflags = NPY_NEEDS_PYAPI;
645 int maxalign = 0;
646 int totalsize = 0;
647 for (int i = 0; i < n; i++) {
648 PyArray_Descr *conv = _convert_from_any(
649 PyList_GET_ITEM(obj, i), align);
650 if (conv == NULL) {
651 goto fail;
652 }
653 dtypeflags |= (conv->flags & NPY_FROM_FIELDS);
654 if (align) {
655 int _align = conv->alignment;
656 if (_align > 1) {
657 totalsize = NPY_NEXT_ALIGNED_OFFSET(totalsize, _align);
658 }
659 maxalign = PyArray_MAX(maxalign, _align);
660 }
661 PyObject *size_obj = PyLong_FromLong((long) totalsize);
662 if (!size_obj) {
663 Py_DECREF(conv);
664 goto fail;
665 }
666 PyObject *tup = PyTuple_New(2);
667 if (!tup) {
668 Py_DECREF(size_obj);

Callers 1

Calls 2

_convert_from_anyFunction · 0.85
PyArray_DescrNewFromTypeFunction · 0.85

Tested by

no test coverage detected