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

Function _convert_from_array_descr

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

* obj is a list. Each item is a tuple with * * (field-name, data-type (either a list or a string), and an optional * shape parameter). * * field-name can be a string or a 2-tuple * data-type can now be a list, string, or 2-tuple * (string, metadata dictionary) */

Source from the content-addressed store, hash-verified

415 * (string, metadata dictionary)
416 */
417static PyArray_Descr *
418_convert_from_array_descr(PyObject *obj, int align)
419{
420 int n = PyList_GET_SIZE(obj);
421 PyObject *nameslist = PyTuple_New(n);
422 if (!nameslist) {
423 return NULL;
424 }
425
426 /* Types with fields need the Python C API for field access */
427 char dtypeflags = NPY_NEEDS_PYAPI;
428 int maxalign = 0;
429 int totalsize = 0;
430 PyObject *fields = PyDict_New();
431 if (!fields) {
432 return NULL;
433 }
434 for (int i = 0; i < n; i++) {
435 PyObject *item = PyList_GET_ITEM(obj, i);
436 if (!PyTuple_Check(item) || (PyTuple_GET_SIZE(item) < 2)) {
437 PyErr_Format(PyExc_TypeError,
438 "Field elements must be 2- or 3-tuples, got '%R'",
439 item);
440 goto fail;
441 }
442 PyObject *name = PyTuple_GET_ITEM(item, 0);
443 PyObject *title;
444 if (PyUnicode_Check(name)) {
445 title = NULL;
446 }
447 else if (PyTuple_Check(name)) {
448 if (PyTuple_GET_SIZE(name) != 2) {
449 PyErr_Format(PyExc_TypeError,
450 "If a tuple, the first element of a field tuple must have "
451 "two elements, not %zd",
452 PyTuple_GET_SIZE(name));
453 goto fail;
454 }
455 title = PyTuple_GET_ITEM(name, 0);
456 name = PyTuple_GET_ITEM(name, 1);
457 if (!PyUnicode_Check(name)) {
458 PyErr_SetString(PyExc_TypeError, "Field name must be a str");
459 goto fail;
460 }
461 }
462 else {
463 PyErr_SetString(PyExc_TypeError,
464 "First element of field tuple is "
465 "neither a tuple nor str");
466 goto fail;
467 }
468
469 /* Insert name into nameslist */
470 Py_INCREF(name);
471
472 if (PyUnicode_GetLength(name) == 0) {
473 Py_DECREF(name);
474 if (title == NULL) {

Callers 1

_convert_from_anyFunction · 0.85

Calls 2

_convert_from_anyFunction · 0.85
PyArray_DescrNewFromTypeFunction · 0.85

Tested by

no test coverage detected