NUMPY_API*/
| 2105 | |
| 2106 | /*NUMPY_API*/ |
| 2107 | NPY_NO_EXPORT PyObject * |
| 2108 | PyArray_FromInterface(PyObject *origin) |
| 2109 | { |
| 2110 | PyObject *iface = NULL; |
| 2111 | PyObject *attr = NULL; |
| 2112 | PyObject *base = NULL; |
| 2113 | PyArrayObject *ret; |
| 2114 | PyArray_Descr *dtype = NULL; |
| 2115 | char *data = NULL; |
| 2116 | Py_buffer view; |
| 2117 | int i, n; |
| 2118 | npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS]; |
| 2119 | int dataflags = NPY_ARRAY_BEHAVED; |
| 2120 | |
| 2121 | iface = PyArray_LookupSpecial_OnInstance(origin, npy_ma_str_array_interface); |
| 2122 | |
| 2123 | if (iface == NULL) { |
| 2124 | if (PyErr_Occurred()) { |
| 2125 | return NULL; |
| 2126 | } |
| 2127 | return Py_NotImplemented; |
| 2128 | } |
| 2129 | if (!PyDict_Check(iface)) { |
| 2130 | if (PyType_Check(origin) && PyObject_HasAttrString(iface, "__get__")) { |
| 2131 | /* |
| 2132 | * If the input is a class `iface` should be a property-like object. |
| 2133 | * This cannot be interpreted as an array, but is a valid. |
| 2134 | * (Needed due to the lookup being on the instance rather than type) |
| 2135 | */ |
| 2136 | Py_DECREF(iface); |
| 2137 | return Py_NotImplemented; |
| 2138 | } |
| 2139 | |
| 2140 | Py_DECREF(iface); |
| 2141 | PyErr_SetString(PyExc_ValueError, |
| 2142 | "Invalid __array_interface__ value, must be a dict"); |
| 2143 | return NULL; |
| 2144 | } |
| 2145 | |
| 2146 | /* Get type string from interface specification */ |
| 2147 | attr = _PyDict_GetItemStringWithError(iface, "typestr"); |
| 2148 | if (attr == NULL) { |
| 2149 | Py_DECREF(iface); |
| 2150 | if (!PyErr_Occurred()) { |
| 2151 | PyErr_SetString(PyExc_ValueError, |
| 2152 | "Missing __array_interface__ typestr"); |
| 2153 | } |
| 2154 | return NULL; |
| 2155 | } |
| 2156 | |
| 2157 | /* allow bytes for backwards compatibility */ |
| 2158 | if (!PyBytes_Check(attr) && !PyUnicode_Check(attr)) { |
| 2159 | PyErr_SetString(PyExc_TypeError, |
| 2160 | "__array_interface__ typestr must be a string"); |
| 2161 | goto fail; |
| 2162 | } |
| 2163 | |
| 2164 | /* Get dtype from type string */ |
no test coverage detected