NUMPY_API * Sets the 'base' attribute of the array. This steals a reference * to 'obj'. * * Returns 0 on success, -1 on failure. */
| 152 | * Returns 0 on success, -1 on failure. |
| 153 | */ |
| 154 | NPY_NO_EXPORT int |
| 155 | PyArray_SetBaseObject(PyArrayObject *arr, PyObject *obj) |
| 156 | { |
| 157 | if (obj == NULL) { |
| 158 | PyErr_SetString(PyExc_ValueError, |
| 159 | "Cannot set the NumPy array 'base' " |
| 160 | "dependency to NULL after initialization"); |
| 161 | return -1; |
| 162 | } |
| 163 | /* |
| 164 | * Allow the base to be set only once. Once the object which |
| 165 | * owns the data is set, it doesn't make sense to change it. |
| 166 | */ |
| 167 | if (PyArray_BASE(arr) != NULL) { |
| 168 | Py_DECREF(obj); |
| 169 | PyErr_SetString(PyExc_ValueError, |
| 170 | "Cannot set the NumPy array 'base' " |
| 171 | "dependency more than once"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Don't allow infinite chains of views, always set the base |
| 177 | * to the first owner of the data. |
| 178 | * That is, either the first object which isn't an array, |
| 179 | * or the first object which owns its own data. |
| 180 | */ |
| 181 | |
| 182 | while (PyArray_Check(obj) && (PyObject *)arr != obj) { |
| 183 | PyArrayObject *obj_arr = (PyArrayObject *)obj; |
| 184 | PyObject *tmp; |
| 185 | |
| 186 | /* Propagate WARN_ON_WRITE through views. */ |
| 187 | if (PyArray_FLAGS(obj_arr) & NPY_ARRAY_WARN_ON_WRITE) { |
| 188 | PyArray_ENABLEFLAGS(arr, NPY_ARRAY_WARN_ON_WRITE); |
| 189 | } |
| 190 | |
| 191 | /* If this array owns its own data, stop collapsing */ |
| 192 | if (PyArray_CHKFLAGS(obj_arr, NPY_ARRAY_OWNDATA)) { |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | tmp = PyArray_BASE(obj_arr); |
| 197 | /* If there's no base, stop collapsing */ |
| 198 | if (tmp == NULL) { |
| 199 | break; |
| 200 | } |
| 201 | /* Stop the collapse new base when the would not be of the same |
| 202 | * type (i.e. different subclass). |
| 203 | */ |
| 204 | if (Py_TYPE(tmp) != Py_TYPE(arr)) { |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | Py_INCREF(tmp); |
| 210 | Py_DECREF(obj); |
| 211 | obj = tmp; |
no test coverage detected