8000 Merge pull request #11979 from hmaarrfk/block_single_array_copy_test · shoyer/numpy@db5f9d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit db5f9d3

Browse files
authored
Merge pull request numpy#11979 from hmaarrfk/block_single_array_copy_test
MAINT: Ensure that a copy of the array is returned when calling `block`.
2 parents 86a7acc + 2beafe7 commit db5f9d3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

numpy/core/shape_base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,11 @@ def block(arrays):
617617
_block_format_index(bottom_index)
618618
)
619619
)
620-
return _block(arrays, list_ndim, max(arr_ndim, list_ndim))
620+
result = _block(arrays, list_ndim, max(arr_ndim, list_ndim))
621+
if list_ndim == 0:
622+
# Catch an edge case where _block returns a view because
623+
# `arrays` is a single numpy array and not a list of numpy arrays.
624+
# This might copy scalars or lists twice, but this isn't a likely
625+
# usecase for those interested in performance
626+
result = result.copy()
627+
return result

numpy/core/tests/test_shape_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ def test_2D_array2(self):
191191

192192

193193
class TestConcatenate(object):
194+
def test_returns_copy(self):
195+
a = np.eye(3)
196+
b = np.concatenate([a])
197+
b[0, 0] = 2
198+
assert b[0, 0] != a[0, 0]
199+
194200
def test_exceptions(self):
195201
# test axis must be in bounds
196202
for ndim in [1, 2, 3]:
@@ -367,6 +373,12 @@ def test_stack():
367373

368374

369375
class TestBlock(object):
376+
def test_returns_copy(self):
377+
a = np.eye(3)
378+
b = np.block(a)
379+
b[0, 0] = 2
380+
assert b[0, 0] != a[0, 0]
381+
370382
def test_block_simple_row_wise(self):
371383
a_2d = np.ones((2, 2))
372384
b_2d = 2 * a_2d

0 commit comments

Comments
 (0)
0