diff options
author | Matti Picus <matti.picus@gmail.com> | 2018-10-09 13:31:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-09 13:31:20 +0300 |
commit | db5f9d3a61d639b4fdb95c161012678ea8847424 (patch) | |
tree | f57717d29b797fd9a8d44ecc9a0647bf2e4d5325 /numpy | |
parent | 86a7acc8582923604fac849bb19f0b08efc0a91a (diff) | |
parent | 2beafe73822cd6b47b4138ab83359585e0caa6b8 (diff) | |
download | numpy-db5f9d3a61d639b4fdb95c161012678ea8847424.tar.gz |
Merge pull request #11979 from hmaarrfk/block_single_array_copy_test
MAINT: Ensure that a copy of the array is returned when calling `block`.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/shape_base.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_shape_base.py | 12 |
2 files changed, 20 insertions, 1 deletions
diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 52717abda..feb1605bc 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -617,4 +617,11 @@ def block(arrays): _block_format_index(bottom_index) ) ) - return _block(arrays, list_ndim, max(arr_ndim, list_ndim)) + result = _block(arrays, list_ndim, max(arr_ndim, list_ndim)) + if list_ndim == 0: + # Catch an edge case where _block returns a view because + # `arrays` is a single numpy array and not a list of numpy arrays. + # This might copy scalars or lists twice, but this isn't a likely + # usecase for those interested in performance + result = result.copy() + return result diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py index 72b3451a4..df819b73f 100644 --- a/numpy/core/tests/test_shape_base.py +++ b/numpy/core/tests/test_shape_base.py @@ -191,6 +191,12 @@ class TestVstack(object): class TestConcatenate(object): + def test_returns_copy(self): + a = np.eye(3) + b = np.concatenate([a]) + b[0, 0] = 2 + assert b[0, 0] != a[0, 0] + def test_exceptions(self): # test axis must be in bounds for ndim in [1, 2, 3]: @@ -367,6 +373,12 @@ def test_stack(): class TestBlock(object): + def test_returns_copy(self): + a = np.eye(3) + b = np.block(a) + b[0, 0] = 2 + assert b[0, 0] != a[0, 0] + def test_block_simple_row_wise(self): a_2d = np.ones((2, 2)) b_2d = 2 * a_2d |