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

Function PyArray_Scalar

numpy/core/src/multiarray/scalarapi.c:621–768  ·  view source on GitHub ↗

Does nothing with descr (cannot be NULL) */ NUMPY_API Get scalar-equivalent to a region of memory described by a descriptor. */

Source from the content-addressed store, hash-verified

619 Get scalar-equivalent to a region of memory described by a descriptor.
620*/
621NPY_NO_EXPORT PyObject *
622PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
623{
624 PyTypeObject *type;
625 PyObject *obj;
626 void *destptr;
627 PyArray_CopySwapFunc *copyswap;
628 int type_num;
629 int itemsize;
630 int swap;
631
632 type_num = descr->type_num;
633 if (type_num == NPY_BOOL) {
634 PyArrayScalar_RETURN_BOOL_FROM_LONG(*(npy_bool*)data);
635 }
636 else if (PyDataType_FLAGCHK(descr, NPY_USE_GETITEM)) {
637 return descr->f->getitem(data, base);
638 }
639 itemsize = descr->elsize;
640 copyswap = descr->f->copyswap;
641 type = descr->typeobj;
642 swap = !PyArray_ISNBO(descr->byteorder);
643 if (PyTypeNum_ISSTRING(type_num)) {
644 /* Eliminate NULL bytes */
645 char *dptr = data;
646
647 dptr += itemsize - 1;
648 while(itemsize && *dptr-- == 0) {
649 itemsize--;
650 }
651 if (type_num == NPY_UNICODE && itemsize) {
652 /*
653 * make sure itemsize is a multiple of 4
654 * so round up to nearest multiple
655 */
656 itemsize = (((itemsize - 1) >> 2) + 1) << 2;
657 }
658 }
659 if (type_num == NPY_UNICODE) {
660 /* we need the full string length here, else copyswap will write too
661 many bytes */
662 void *buff = PyArray_malloc(descr->elsize);
663 if (buff == NULL) {
664 return PyErr_NoMemory();
665 }
666 /* copyswap needs an array object, but only actually cares about the
667 * dtype
668 */
669 PyArrayObject_fields dummy_arr;
670 if (base == NULL) {
671 dummy_arr.descr = descr;
672 base = (PyObject *)&dummy_arr;
673 }
674 copyswap(buff, data, swap, base);
675
676 /* truncation occurs here */
677 PyObject *u = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buff, itemsize / 4);
678 PyArray_free(buff);

Callers 3

array_scalarFunction · 0.85
array_itemFunction · 0.85
array_subscriptFunction · 0.85

Calls 3

PyArray_FLAGSFunction · 0.85
PyDataMem_NEWFunction · 0.85
scalar_valueFunction · 0.85

Tested by

no test coverage detected