Skip to content

Commit 6d47d31

Browse files
committed
DEP: Speed up WarnOnWrite deprecation in buffer interface
When a buffer interface does not request a writeable buffer, simply pass a read-only one when the warn on write flag is set. This is to give an easier way forward with avoiding the deprecation warnings: Simply do not ask for a writeable buffer. It will break code that expects writeable buffers but does not ask for them specifically a bit harder than would be nice. But since such code probably should ask for it specifically, this is likely fine (an RC release has to find out). The main reason for this is, that this way it plays very will with cython, which requests writeable buffers explicitly and if declared `const` is happy about read-only (so that using `const` is the best way to avoid the warning and makes code cleaner). Closes numpygh-13929, numpygh-13974
1 parent af5a108 commit 6d47d31

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

‎doc/release/1.17.0-notes.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ produce the deprecation warning. To help alleviate confusion, an additional
9191
`FutureWarning` will be emitted when accessing the ``writeable`` flag state to
9292
clarify the contradiction.
9393

94+
Note that for the C-side buffer protocol such an array will return a
95+
readonly buffer immediately unless a writable buffer is requested. If
96+
a writeable buffer is requested a warning will be given. When using
97+
cython, the `const` attribute should be used with such arrays to avoid
98+
the warning.
99+
94100

95101
Future Changes
96102
==============

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -771,17 +771,6 @@ array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
771771
goto fail;
772772
}
773773
}
774-
/*
775-
* If a read-only buffer is requested on a read-write array, we return a
776-
* read-write buffer, which is dubious behavior. But that's why this call
777-
* is guarded by PyArray_ISWRITEABLE rather than (flags &
778-
* PyBUF_WRITEABLE).
779-
*/
780-
if (PyArray_ISWRITEABLE(self)) {
781-
if (array_might_be_written(self) < 0) {
782-
goto fail;
783-
}
784-
}
785774

786775
if (view == NULL) {
787776
PyErr_SetString(PyExc_ValueError, "NULL view in getbuffer");
@@ -797,7 +786,14 @@ array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
797786
view->buf = PyArray_DATA(self);
798787
view->suboffsets = NULL;
799788
view->itemsize = PyArray_ITEMSIZE(self);
800-
view->readonly = !PyArray_ISWRITEABLE(self);
789+
/*
790+
* Set a requested buffer to readonly also if the array will be readonly
791+
* after a deprecation. This jumps the deprecation, but avoiding the
792+
* warning is not convenient here and a warning is given if a writeable
793+
* buffer is requested. (Warn on write is cleared if writeable is requested)
794+
*/
795+
view->readonly = (!PyArray_ISWRITEABLE(self) ||
796+
PyArray_CHKFLAGS(self, NPY_ARRAY_WARN_ON_WRITE));
801797
view->internal = NULL;
802798
view->len = PyArray_NBYTES(self);
803799
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {

0 commit comments

Comments
 (0)