summaryrefslogtreecommitdiff
path: root/numpy/lib/stride_tricks.py
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2019-06-28 18:27:42 -0700
committerSebastian Berg <sebastian@sipsolutions.net>2019-06-28 18:27:42 -0700
commit59ebfbbe32bcac4a278c39a9a24b80a42d47a8f6 (patch)
treeabcdbeac29cc19a7bb839ab4578a08668e6eaef5 /numpy/lib/stride_tricks.py
parente7383b5d5dff636ed92667a28751ce18f7086097 (diff)
downloadnumpy-59ebfbbe32bcac4a278c39a9a24b80a42d47a8f6.tar.gz
ENH: Deprecate writeable broadcast_array (#12609)
When the base is not an array (or generally when the flag of the base array was toggled), it is OK to allow setting the writeable flag to True, as long as any ancestor (especially the last one) is writeable. This commit also slightly change the behaviour of the base attribute. --- * ENH: Deprecate writeable broadcast_array * ENH: Make writeable flag enabling more reliable for non-array bases When the base is not an array (or generally when the flag of the base array was toggled), it is OK to allow setting the writeable flag to True, as long as any ancestor (especially the last one) is writeable. * Update doc/release/1.17.0-notes.rst Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net> * Update doc/release/1.17.0-notes.rst Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net> * Update numpy/lib/tests/test_stride_tricks.py Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net> * Update numpy/core/tests/test_multiarray.py Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net> * DOC: improve warning (from review)
Diffstat (limited to 'numpy/lib/stride_tricks.py')
-rw-r--r--numpy/lib/stride_tricks.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py
index fd401c57c..8aafd094b 100644
--- a/numpy/lib/stride_tricks.py
+++ b/numpy/lib/stride_tricks.py
@@ -121,18 +121,18 @@ def _broadcast_to(array, shape, subok, readonly):
if any(size < 0 for size in shape):
raise ValueError('all elements of broadcast shape must be non-'
'negative')
- needs_writeable = not readonly and array.flags.writeable
- extras = ['reduce_ok'] if needs_writeable else []
- op_flag = 'readwrite' if needs_writeable else 'readonly'
+ extras = []
it = np.nditer(
(array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
- op_flags=[op_flag], itershape=shape, order='C')
+ op_flags=['readonly'], itershape=shape, order='C')
with it:
# never really has writebackifcopy semantics
broadcast = it.itviews[0]
result = _maybe_view_as_subclass(array, broadcast)
- if needs_writeable and not result.flags.writeable:
+ # In a future version this will go away
+ if not readonly and array.flags._writeable_no_warn:
result.flags.writeable = True
+ result.flags._warn_on_write = True
return result
@@ -222,8 +222,15 @@ def broadcast_arrays(*args, **kwargs):
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
- broadcasted array may refer to a single memory location. If you
- need to write to the arrays, make copies first.
+ broadcasted array may refer to a single memory location. If you need
+ to write to the arrays, make copies first. While you can set the
+ ``writable`` flag True, writing to a single output value may end up
+ changing more than one location in the output array.
+
+ .. deprecated:: 1.17
+ The output is currently marked so that if written to, a deprecation
+ warning will be emitted. A future version will set the
+ ``writable`` flag False so writing to it will raise an error.
Examples
--------
@@ -260,7 +267,5 @@ def broadcast_arrays(*args, **kwargs):
# Common case where nothing needs to be broadcasted.
return args
- # TODO: consider making the results of broadcast_arrays readonly to match
- # broadcast_to. This will require a deprecation cycle.
return [_broadcast_to(array, shape, subok=subok, readonly=False)
for array in args]