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

Function PyArray_SetWritebackIfCopyBase

numpy/core/src/multiarray/arrayobject.c:108–146  ·  view source on GitHub ↗

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. */

Source from the content-addressed store, hash-verified

106 * Returns 0 on success, -1 on failure.
107 */
108NPY_NO_EXPORT int
109PyArray_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

Callers 4

PyArray_FromArrayFunction · 0.85
new_array_for_sumFunction · 0.85
npyiter_allocate_arraysFunction · 0.85

Calls 5

PyArray_BASEFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_ENABLEFLAGSFunction · 0.85
PyArray_CLEARFLAGSFunction · 0.85

Tested by

no test coverage detected