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

Function array_data_set

numpy/core/src/multiarray/getset.c:338–420  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

336}
337
338static int
339array_data_set(PyArrayObject *self, PyObject *op, void *NPY_UNUSED(ignored))
340{
341 void *buf;
342 Py_ssize_t buf_len;
343 int writeable=1;
344 Py_buffer view;
345
346 /* 2016-19-02, 1.12 */
347 int ret = DEPRECATE("Assigning the 'data' attribute is an "
348 "inherently unsafe operation and will "
349 "be removed in the future.");
350 if (ret < 0) {
351 return -1;
352 }
353
354 if (op == NULL) {
355 PyErr_SetString(PyExc_AttributeError,
356 "Cannot delete array data");
357 return -1;
358 }
359 if (PyObject_GetBuffer(op, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) {
360 writeable = 0;
361 PyErr_Clear();
362 if (PyObject_GetBuffer(op, &view, PyBUF_SIMPLE) < 0) {
363 return -1;
364 }
365 }
366 buf = view.buf;
367 buf_len = view.len;
368 /*
369 * In Python 3 both of the deprecated functions PyObject_AsWriteBuffer and
370 * PyObject_AsReadBuffer that this code replaces release the buffer. It is
371 * up to the object that supplies the buffer to guarantee that the buffer
372 * sticks around after the release.
373 */
374 PyBuffer_Release(&view);
375
376 if (!PyArray_ISONESEGMENT(self)) {
377 PyErr_SetString(PyExc_AttributeError,
378 "cannot set single-segment buffer for discontiguous array");
379 return -1;
380 }
381 if (PyArray_NBYTES(self) > buf_len) {
382 PyErr_SetString(PyExc_AttributeError, "not enough data for array");
383 return -1;
384 }
385 if (PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA) {
386 PyArray_XDECREF(self);
387 size_t nbytes = PyArray_NBYTES(self);
388 if (nbytes == 0) {
389 nbytes = 1;
390 }
391 PyObject *handler = PyArray_HANDLER(self);
392 if (handler == NULL) {
393 /* This can happen if someone arbitrarily sets NPY_ARRAY_OWNDATA */
394 PyErr_SetString(PyExc_RuntimeError,
395 "no memory handler found but OWNDATA flag set");

Callers

nothing calls this directly

Calls 9

PyArray_FLAGSFunction · 0.85
PyArray_XDECREFFunction · 0.85
PyArray_HANDLERFunction · 0.85
PyDataMem_UserFREEFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_BASEFunction · 0.85
PyArray_ENABLEFLAGSFunction · 0.85
PyArray_CLEARFLAGSFunction · 0.85
PyArray_SetBaseObjectFunction · 0.85

Tested by

no test coverage detected