Skip to content

Commit a2e62e8

Browse files
committed
BUG: make data attribute of ndarray read-only
Fixes numpy#1873
1 parent 83f36c5 commit a2e62e8

1 file changed

Lines changed: 1 addition & 61 deletions

File tree

‎numpy/core/src/multiarray/getset.c‎

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -320,66 +320,6 @@ array_data_get(PyArrayObject *self)
320320
#endif
321321
}
322322

323-
/*
324-
* TODO: Given view semantics, I think this function is a really
325-
* bad idea, and should be removed!
326-
*/
327-
static int
328-
array_data_set(PyArrayObject *self, PyObject *op)
329-
{
330-
void *buf;
331-
Py_ssize_t buf_len;
332-
int writeable=1;
333-
334-
if (op == NULL) {
335-
PyErr_SetString(PyExc_AttributeError,
336-
"Cannot delete array data");
337-
return -1;
338-
}
339-
if (PyObject_AsWriteBuffer(op, &buf, &buf_len) < 0) {
340-
writeable = 0;
341-
if (PyObject_AsReadBuffer(op, (const void **)&buf, &buf_len) < 0) {
342-
PyErr_SetString(PyExc_AttributeError,
343-
"object does not have single-segment " \
344-
"buffer interface");
345-
return -1;
346-
}
347-
}
348-
if (!PyArray_ISONESEGMENT(self)) {
349-
PyErr_SetString(PyExc_AttributeError, "cannot set single-" \
350-
"segment buffer for discontiguous array");
351-
return -1;
352-
}
353-
if (PyArray_NBYTES(self) > buf_len) {
354-
PyErr_SetString(PyExc_AttributeError, "not enough data for array");
355-
return -1;
356-
}
357-
if (PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA) {
358-
PyArray_XDECREF(self);
359-
PyDataMem_FREE(PyArray_DATA(self));
360-
}
361-
if (PyArray_BASE(self)) {
362-
if (PyArray_FLAGS(self) & NPY_ARRAY_UPDATEIFCOPY) {
363-
PyArray_ENABLEFLAGS((PyArrayObject *)PyArray_BASE(self),
364-
NPY_ARRAY_WRITEABLE);
365-
PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY);
366-
}
367-
Py_DECREF(PyArray_BASE(self));
368-
((PyArrayObject_fields *)self)->base = NULL;
369-
}
370-
Py_INCREF(op);
371-
if (PyArray_SetBaseObject(self, op) < 0) {
372-
return -1;
373-
}
374-
((PyArrayObject_fields *)self)->data = buf;
375-
((PyArrayObject_fields *)self)->flags = NPY_ARRAY_CARRAY;
376-
if (!writeable) {
377-
PyArray_CLEARFLAGS(self, ~NPY_ARRAY_WRITEABLE);
378-
}
379-
return 0;
380-
}
381-
382-
383323
static PyObject *
384324
array_itemsize_get(PyArrayObject *self)
385325
{
@@ -922,7 +862,7 @@ NPY_NO_EXPORT PyGetSetDef array_getsetlist[] = {
922862
NULL, NULL},
923863
{"data",
924864
(getter)array_data_get,
925-
(setter)array_data_set,
865+
NULL,
926866
NULL, NULL},
927867
{"itemsize",
928868
(getter)array_itemsize_get,

0 commit comments

Comments
 (0)