NUMPY_API * * Precondition: 'arr' is a copy of 'base' (though possibly with different * strides, ordering, etc.). This function sets the WRITEBACKIFCOPY flag and the * ->base pointer on 'arr', call PyArray_ResolveWritebackIfCopy to copy any * changes back to 'base' before deallocating the array. * * Steals a reference to 'base'. * * Returns 0 on success, -1 on failure. */
| 106 | * Returns 0 on success, -1 on failure. |
| 107 | */ |
| 108 | NPY_NO_EXPORT int |
| 109 | PyArray_SetWritebackIfCopyBase(PyArrayObject *arr, PyArrayObject *base) |
| 110 | { |
| 111 | if (base == NULL) { |
| 112 | PyErr_SetString(PyExc_ValueError, |
| 113 | "Cannot WRITEBACKIFCOPY to NULL array"); |
| 114 | return -1; |
| 115 | } |
| 116 | if (PyArray_BASE(arr) != NULL) { |
| 117 | PyErr_SetString(PyExc_ValueError, |
| 118 | "Cannot set array with existing base to WRITEBACKIFCOPY"); |
| 119 | goto fail; |
| 120 | } |
| 121 | if (PyArray_FailUnlessWriteable(base, "WRITEBACKIFCOPY base") < 0) { |
| 122 | goto fail; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Any writes to 'arr' will magically turn into writes to 'base', so we |
| 127 | * should warn if necessary. |
| 128 | */ |
| 129 | if (PyArray_FLAGS(base) & NPY_ARRAY_WARN_ON_WRITE) { |
| 130 | PyArray_ENABLEFLAGS(arr, NPY_ARRAY_WARN_ON_WRITE); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Unlike PyArray_SetBaseObject, we do not compress the chain of base |
| 135 | * references. |
| 136 | */ |
| 137 | ((PyArrayObject_fields *)arr)->base = (PyObject *)base; |
| 138 | PyArray_ENABLEFLAGS(arr, NPY_ARRAY_WRITEBACKIFCOPY); |
| 139 | PyArray_CLEARFLAGS(base, NPY_ARRAY_WRITEABLE); |
| 140 | |
| 141 | return 0; |
| 142 | |
| 143 | fail: |
| 144 | Py_DECREF(base); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /*NUMPY_API |
| 149 | * Sets the 'base' attribute of the array. This steals a reference |
no test coverage detected