Skip to content

Commit d2c7337

Browse files
sebergcharris
authored andcommitted
BUG,DEP: Fix writeable flag setting for arrays without base
This also deprecates setting a non-writeable array to writeable if that array does not own its data (and has no base object to check if the memory may be writeable). (Deprecation on Python side only) Closes numpygh-481
1 parent c8ea622 commit d2c7337

5 files changed

Lines changed: 118 additions & 12 deletions

File tree

‎doc/release/1.16.4-notes.rst‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ New functions
2222

2323
New deprecations
2424
================
25-
25+
Writeable flag of C-API wrapped arrays
26+
--------------------------------------
27+
When an array is created from the C-API to wrap a pointer to data, the only
28+
indication we have of the read-write nature of the data is the ``writeable``
29+
flag set during creation. It is dangerous to force the flag to writeable. In
30+
the future it will not be possible to switch the writeable flag to ``True``
31+
from python. This deprecation should not affect many users since arrays
32+
created in such a manner are very rare in practice and only available through
33+
the NumPy C-API.
2634

2735
Expired deprecations
2836
====================

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,29 @@ get_buffer_info(PyObject *NPY_UNUSED(self), PyObject *args)
847847

848848
#undef GET_PYBUF_FLAG
849849

850+
/*
851+
* Return a new array object wrapping existing C-allocated (dummy) data.
852+
* Such an array does not own its data (must not free it), but because it
853+
* wraps C data, it also has no base object. Used to test arr.flags.writeable
854+
* setting behaviour.
855+
*/
856+
static PyObject*
857+
get_c_wrapping_array(PyObject* NPY_UNUSED(self), PyObject* arg)
858+
{
859+
int writeable, flags;
860+
npy_intp zero = 0;
861+
862+
writeable = PyObject_IsTrue(arg);
863+
if (error_converting(writeable)) {
864+
return NULL;
865+
}
866+
867+
flags = writeable ? NPY_ARRAY_WRITEABLE : 0;
868+
/* Create an empty array (which points to a random place) */
869+
return PyArray_NewFromDescr(&PyArray_Type, PyArray_DescrFromType(NPY_INTP),
870+
1, &zero, NULL, &zero, flags, NULL);
871+
}
872+
850873

851874
/*
852875
* Test C-api level item getting.
@@ -1932,6 +1955,9 @@ static PyMethodDef Multiarray_TestsMethods[] = {
19321955
{"get_buffer_info",
19331956
get_buffer_info,
19341957
METH_VARARGS, NULL},
1958+
{"get_c_wrapping_array",
1959+
get_c_wrapping_array,
1960+
METH_O, NULL},
19351961
{"array_indexing",
19361962
array_indexing,
19371963
METH_VARARGS, NULL},

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -598,31 +598,46 @@ _zerofill(PyArrayObject *ret)
598598
NPY_NO_EXPORT npy_bool
599599
_IsWriteable(PyArrayObject *ap)
600600
{
601-
PyObject *base=PyArray_BASE(ap);
601+
PyObject *base = PyArray_BASE(ap);
602602
#if defined(NPY_PY3K)
603603
Py_buffer view;
604604
#else
605605
void *dummy;
606606
Py_ssize_t n;
607607
#endif
608608

609-
/* If we own our own data, then no-problem */
610-
if ((base == NULL) || (PyArray_FLAGS(ap) & NPY_ARRAY_OWNDATA)) {
609+
/*
610+
* C-data wrapping arrays may not own their data while not having a base;
611+
* WRITEBACKIFCOPY arrays have a base, but do own their data.
612+
*/
613+
if (base == NULL || PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA)) {
614+
/*
615+
* This is somewhat unsafe for directly wrapped non-writable C-arrays,
616+
* which do not know whether the memory area is writable or not and
617+
* do not own their data (but have no base).
618+
* It would be better if this returned PyArray_ISWRITEABLE(ap).
619+
* Since it is hard to deprecate, this is deprecated only on the Python
620+
* side, but not on in PyArray_UpdateFlags.
621+
*/
611622
return NPY_TRUE;
612623
}
624+
613625
/*
614-
* Get to the final base object
615-
* If it is a writeable array, then return TRUE
616-
* If we can find an array object
617-
* or a writeable buffer object as the final base object
626+
* Get to the final base object.
627+
* If it is a writeable array, then return True if we can
628+
* find an array object or a writeable buffer object as
629+
* the final base object.
618630
*/
631+
while (PyArray_Check(base)) {
632+
ap = (PyArrayObject *)base;
633+
base = PyArray_BASE(ap);
619634

620-
while(PyArray_Check(base)) {
621-
if (PyArray_CHKFLAGS((PyArrayObject *)base, NPY_ARRAY_OWNDATA)) {
622-
return (npy_bool) (PyArray_ISWRITEABLE((PyArrayObject *)base));
635+
if (base == NULL || PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA)) {
636+
return (npy_bool) PyArray_ISWRITEABLE(ap);
623637
}
624-
base = PyArray_BASE((PyArrayObject *)base);
638+
assert(!PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA));
625639
}
640+
626641
#if defined(NPY_PY3K)
627642
if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) {
628643
PyErr_Clear();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,22 @@ array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds)
25462546
if (write_flag != Py_None) {
25472547
if (PyObject_IsTrue(write_flag)) {
25482548
if (_IsWriteable(self)) {
2549+
/*
2550+
* _IsWritable (and PyArray_UpdateFlags) allows flipping this,
2551+
* although the C-Api user who created the array may have
2552+
* chosen to make it non-writable for a good reason, so
2553+
* deprecate.
2554+
*/
2555+
if ((PyArray_BASE(self) == NULL) &&
2556+
!PyArray_CHKFLAGS(self, NPY_ARRAY_OWNDATA) &&
2557+
!PyArray_CHKFLAGS(self, NPY_ARRAY_WRITEABLE)) {
2558+
/* 2017-05-03, NumPy 1.17.0 */
2559+
if (DEPRECATE("making a non-writeable array writeable "
2560+
"is deprecated for arrays without a base "
2561+
"which do not own their data.") < 0) {
2562+
return NULL;
2563+
}
2564+
}
25492565
PyArray_ENABLEFLAGS(self, NPY_ARRAY_WRITEABLE);
25502566
}
25512567
else {

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,47 @@ def test_writeable_pickle(self):
142142
assert_(vals.flags.writeable)
143143
assert_(isinstance(vals.base, bytes))
144144

145+
def test_writeable_from_c_data(self):
146+
# Test that the writeable flag can be changed for an array wrapping
147+
# low level C-data, but not owning its data.
148+
# Also see that this is deprecated to change from python.
149+
from numpy.core._multiarray_tests import get_c_wrapping_array
150+
151+
arr_writeable = get_c_wrapping_array(True)
152+
assert not arr_writeable.flags.owndata
153+
assert arr_writeable.flags.writeable
154+
view = arr_writeable[...]
155+
156+
# Toggling the writeable flag works on the view:
157+
view.flags.writeable = False
158+
assert not view.flags.writeable
159+
view.flags.writeable = True
160+
assert view.flags.writeable
161+
# Flag can be unset on the arr_writeable:
162+
arr_writeable.flags.writeable = False
163+
164+
arr_readonly = get_c_wrapping_array(False)
165+
assert not arr_readonly.flags.owndata
166+
assert not arr_readonly.flags.writeable
167+
168+
for arr in [arr_writeable, arr_readonly]:
169+
view = arr[...]
170+
view.flags.writeable = False # make sure it is readonly
171+
arr.flags.writeable = False
172+
assert not arr.flags.writeable
173+
174+
with assert_raises(ValueError):
175+
view.flags.writeable = True
176+
177+
with warnings.catch_warnings():
178+
warnings.simplefilter("error", DeprecationWarning)
179+
with assert_raises(DeprecationWarning):
180+
arr.flags.writeable = True
181+
182+
with assert_warns(DeprecationWarning):
183+
arr.flags.writeable = True
184+
185+
145186
def test_otherflags(self):
146187
assert_equal(self.a.flags.carray, True)
147188
assert_equal(self.a.flags['C'], True)

0 commit comments

Comments
 (0)