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

Function array_struct_get

numpy/core/src/multiarray/getset.c:597–661  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

595}
596
597static PyObject *
598array_struct_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
599{
600 PyArrayInterface *inter;
601
602 inter = (PyArrayInterface *)PyArray_malloc(sizeof(PyArrayInterface));
603 if (inter==NULL) {
604 return PyErr_NoMemory();
605 }
606 inter->two = 2;
607 inter->nd = PyArray_NDIM(self);
608 inter->typekind = PyArray_DESCR(self)->kind;
609 inter->itemsize = PyArray_DESCR(self)->elsize;
610 inter->flags = PyArray_FLAGS(self);
611 if (inter->flags & NPY_ARRAY_WARN_ON_WRITE) {
612 /* Export a warn-on-write array as read-only */
613 inter->flags = inter->flags & ~NPY_ARRAY_WARN_ON_WRITE;
614 inter->flags = inter->flags & ~NPY_ARRAY_WRITEABLE;
615 }
616 /* reset unused flags */
617 inter->flags &= ~(NPY_ARRAY_WRITEBACKIFCOPY | NPY_ARRAY_OWNDATA);
618 if (PyArray_ISNOTSWAPPED(self)) inter->flags |= NPY_ARRAY_NOTSWAPPED;
619 /*
620 * Copy shape and strides over since these can be reset
621 *when the array is "reshaped".
622 */
623 if (PyArray_NDIM(self) > 0) {
624 inter->shape = (npy_intp *)PyArray_malloc(2*sizeof(npy_intp)*PyArray_NDIM(self));
625 if (inter->shape == NULL) {
626 PyArray_free(inter);
627 return PyErr_NoMemory();
628 }
629 inter->strides = inter->shape + PyArray_NDIM(self);
630 if (PyArray_NDIM(self)) {
631 memcpy(inter->shape, PyArray_DIMS(self), sizeof(npy_intp)*PyArray_NDIM(self));
632 memcpy(inter->strides, PyArray_STRIDES(self), sizeof(npy_intp)*PyArray_NDIM(self));
633 }
634 }
635 else {
636 inter->shape = NULL;
637 inter->strides = NULL;
638 }
639 inter->data = PyArray_DATA(self);
640 if (PyDataType_HASFIELDS(PyArray_DESCR(self))) {
641 inter->descr = arraydescr_protocol_descr_get(PyArray_DESCR(self), NULL);
642 if (inter->descr == NULL) {
643 PyErr_Clear();
644 }
645 else {
646 inter->flags &= NPY_ARR_HAS_DESCR;
647 }
648 }
649 else {
650 inter->descr = NULL;
651 }
652 PyObject *ret = PyCapsule_New(inter, NULL, gentype_struct_free);
653 if (ret == NULL) {
654 return NULL;

Callers

nothing calls this directly

Calls 7

PyArray_NDIMFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_DATAFunction · 0.85

Tested by

no test coverage detected