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

Function _IsWriteable

numpy/core/src/multiarray/common.c:131–185  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129
130
131NPY_NO_EXPORT npy_bool
132_IsWriteable(PyArrayObject *ap)
133{
134 PyObject *base = PyArray_BASE(ap);
135 Py_buffer view;
136
137 /*
138 * C-data wrapping arrays may not own their data while not having a base;
139 * WRITEBACKIFCOPY arrays have a base, but do own their data.
140 */
141 if (base == NULL || PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA)) {
142 /*
143 * This is somewhat unsafe for directly wrapped non-writable C-arrays,
144 * which do not know whether the memory area is writable or not and
145 * do not own their data (but have no base).
146 * It would be better if this returned PyArray_ISWRITEABLE(ap).
147 * Since it is hard to deprecate, this is deprecated only on the Python
148 * side, but not on in PyArray_UpdateFlags.
149 */
150 return NPY_TRUE;
151 }
152
153 /*
154 * Get to the final base object.
155 * If it is a writeable array, then return True if we can
156 * find an array object or a writeable buffer object as
157 * the final base object.
158 */
159 while (PyArray_Check(base)) {
160 ap = (PyArrayObject *)base;
161 base = PyArray_BASE(ap);
162
163 if (PyArray_ISWRITEABLE(ap)) {
164 /*
165 * If any base is writeable, it must be OK to switch, note that
166 * bases are typically collapsed to always point to the most
167 * general one.
168 */
169 return NPY_TRUE;
170 }
171
172 if (base == NULL || PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA)) {
173 /* there is no further base to test the writeable flag for */
174 return NPY_FALSE;
175 }
176 assert(!PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA));
177 }
178
179 if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) {
180 PyErr_Clear();
181 return NPY_FALSE;
182 }
183 PyBuffer_Release(&view);
184 return NPY_TRUE;
185}
186
187
188/**

Callers 2

array_setflagsFunction · 0.85
PyArray_UpdateFlagsFunction · 0.85

Calls 2

PyArray_BASEFunction · 0.85
PyArray_CHKFLAGSFunction · 0.85

Tested by

no test coverage detected