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

Function PyArray_FromStructInterface

numpy/core/src/multiarray/ctors.c:1989–2082  ·  view source on GitHub ↗

NUMPY_API */

Source from the content-addressed store, hash-verified

1987
1988/*NUMPY_API */
1989NPY_NO_EXPORT PyObject *
1990PyArray_FromStructInterface(PyObject *input)
1991{
1992 PyArray_Descr *thetype = NULL;
1993 PyArrayInterface *inter;
1994 PyObject *attr;
1995 char endian = NPY_NATBYTE;
1996
1997 attr = PyArray_LookupSpecial_OnInstance(input, npy_ma_str_array_struct);
1998 if (attr == NULL) {
1999 if (PyErr_Occurred()) {
2000 return NULL;
2001 } else {
2002 return Py_NotImplemented;
2003 }
2004 }
2005 if (!PyCapsule_CheckExact(attr)) {
2006 if (PyType_Check(input) && PyObject_HasAttrString(attr, "__get__")) {
2007 /*
2008 * If the input is a class `attr` should be a property-like object.
2009 * This cannot be interpreted as an array, but is a valid.
2010 * (Needed due to the lookup being on the instance rather than type)
2011 */
2012 Py_DECREF(attr);
2013 return Py_NotImplemented;
2014 }
2015 goto fail;
2016 }
2017 inter = PyCapsule_GetPointer(attr, NULL);
2018 if (inter == NULL) {
2019 goto fail;
2020 }
2021 if (inter->two != 2) {
2022 goto fail;
2023 }
2024 if ((inter->flags & NPY_ARRAY_NOTSWAPPED) != NPY_ARRAY_NOTSWAPPED) {
2025 endian = NPY_OPPBYTE;
2026 inter->flags &= ~NPY_ARRAY_NOTSWAPPED;
2027 }
2028
2029 if (inter->flags & NPY_ARR_HAS_DESCR) {
2030 if (PyArray_DescrConverter(inter->descr, &thetype) == NPY_FAIL) {
2031 thetype = NULL;
2032 PyErr_Clear();
2033 }
2034 }
2035
2036 if (thetype == NULL) {
2037 PyObject *type_str = PyUnicode_FromFormat(
2038 "%c%c%d", endian, inter->typekind, inter->itemsize);
2039 if (type_str == NULL) {
2040 Py_DECREF(attr);
2041 return NULL;
2042 }
2043 int ok = PyArray_DescrConverter(type_str, &thetype);
2044 Py_DECREF(type_str);
2045 if (ok != NPY_SUCCEED) {
2046 Py_DECREF(attr);

Callers 1

_array_from_array_likeFunction · 0.85

Calls 3

PyArray_DescrConverterFunction · 0.85

Tested by

no test coverage detected