summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/stride_tricks.py9
-rw-r--r--numpy/lib/tests/test_stride_tricks.py8
2 files changed, 14 insertions, 3 deletions
diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py
index 416776ff4..f4b43a5a9 100644
--- a/numpy/lib/stride_tricks.py
+++ b/numpy/lib/stride_tricks.py
@@ -62,11 +62,14 @@ 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'
broadcast = np.nditer(
- (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'],
- op_flags=['readonly'], itershape=shape, order='C').itviews[0]
+ (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
+ op_flags=[op_flag], itershape=shape, order='C').itviews[0]
result = _maybe_view_as_subclass(array, broadcast)
- if not readonly and array.flags.writeable:
+ if needs_writeable and not result.flags.writeable:
result.flags.writeable = True
return result
diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py
index aad0695be..06e659002 100644
--- a/numpy/lib/tests/test_stride_tricks.py
+++ b/numpy/lib/tests/test_stride_tricks.py
@@ -390,6 +390,14 @@ def test_writeable():
_, result = broadcast_arrays(0, original)
assert_equal(result.flags.writeable, False)
+ # regresssion test for GH6491
+ shape = (2,)
+ strides = [0]
+ tricky_array = as_strided(np.array(0), shape, strides)
+ other = np.zeros((1,))
+ first, second = broadcast_arrays(tricky_array, other)
+ assert_(first.shape == second.shape)
+
def test_reference_types():
input_array = np.array('a', dtype=object)