Skip to content

Commit

Permalink
Merge pull request numpy#6556 from shoyer/fix-broadcast-arrays
Browse files Browse the repository at this point in the history
BUG: error in broadcast_arrays with as_strided array
  • Loading branch information
charris committed Oct 24, 2015
2 parents 2d899ea + 9fa0dba commit cec39ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions numpy/lib/stride_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions numpy/lib/tests/test_stride_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cec39ef

Please sign in to comment.