Skip to content

Commit c24e43c

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 ce5bd78 commit c24e43c

3 files changed

Lines changed: 43 additions & 15 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`` qualifier should be used with such arrays to avoid
98+
the warning (e.g. ``cdef const double[::1] view``).
99+
94100

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

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

Lines changed: 11 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,17 @@ 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+
* If a read-only buffer is requested on a read-write array, we return a
791+
* read-write buffer as per buffer protocol.
792+
* We set a requested buffer to readonly also if the array will be readonly
793+
* after a deprecation. This jumps the deprecation, but avoiding the
794+
* warning is not convenient here. A warning is given if a writeable
795+
* buffer is requested since `PyArray_FailUnlessWriteable` is called above
796+
* (and clears the `NPY_ARRAY_WARN_ON_WRITE` flag).
797+
*/
798+
view->readonly = (!PyArray_ISWRITEABLE(self) ||
799+
PyArray_CHKFLAGS(self, NPY_ARRAY_WARN_ON_WRITE));
801800
view->internal = NULL;
802801
view->len = PyArray_NBYTES(self);
803802
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {

‎numpy/lib/tests/test_stride_tricks.py‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,21 @@ def test_writeable():
417417

418418
# but the result of broadcast_arrays needs to be writeable, to
419419
# preserve backwards compatibility
420-
for results in [broadcast_arrays(original),
421-
broadcast_arrays(0, original)]:
420+
for is_broadcast, results in [(False, broadcast_arrays(original,)),
421+
(True, broadcast_arrays(0, original))]:
422422
for result in results:
423423
# This will change to False in a future version
424-
if any([s == 0 for s in result.strides]):
424+
if is_broadcast:
425425
with assert_warns(FutureWarning):
426426
assert_equal(result.flags.writeable, True)
427427
with assert_warns(DeprecationWarning):
428428
result[:] = 0
429429
# Warning not emitted, writing to the array resets it
430430
assert_equal(result.flags.writeable, True)
431+
else:
432+
# No warning:
433+
assert_equal(result.flags.writeable, True)
434+
431435
for results in [broadcast_arrays(original),
432436
broadcast_arrays(0, original)]:
433437
for result in results:
@@ -451,6 +455,25 @@ def test_writeable():
451455
assert_(first.shape == second.shape)
452456

453457

458+
def test_writeable_memoryview():
459+
# The result of broadcast_arrays exports as a non-writeable memoryview
460+
# because otherwise there is no good way to opt in to the new behaviour
461+
# (i.e. you would need to set writeable to False explicitly).
462+
# See gh-13929.
463+
original = np.array([1, 2, 3])
464+
465+
for is_broadcast, results in [(False, broadcast_arrays(original,)),
466+
(True, broadcast_arrays(0, original))]:
467+
for result in results:
468+
# This will change to False in a future version
469+
if is_broadcast:
470+
# memoryview(result, writable=True) will give warning but cannot
471+
# be tested using the python API.
472+
assert memoryview(result).readonly
473+
else:
474+
assert not memoryview(result).readonly
475+
476+
454477
def test_reference_types():
455478
input_array = np.array('a', dtype=object)
456479
expected = np.array(['a'] * 3, dtype=object)

0 commit comments

Comments
 (0)