Skip to content

Commit d74450e

Browse files
committed
enable copy=False
Original NumPy Commit: 6535f88db0af9f881ab3b410ccf71c8531f6afb5
1 parent 576e43f commit d74450e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

array_api_strict/_manipulation_functions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@ def reshape(x: Array,
6363
6464
See its docstring for more information.
6565
"""
66-
if copy is False:
67-
raise NotImplementedError("copy=False is not yet implemented")
6866

6967
data = x._array
7068
if copy:
7169
data = np.copy(data)
7270

73-
return Array._new(np.reshape(data, shape))
71+
reshaped = np.reshape(data, shape)
72+
73+
if copy is False and not np.shares_memory(data, reshaped):
74+
raise AttributeError("Incompatible shape for in-place modification.")
75+
76+
return Array._new(reshaped)
7477

7578

7679
def roll(

array_api_strict/tests/test_manipulation_functions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ def test_stack_errors():
2323

2424

2525
def test_reshape_copy():
26-
a = asarray([1])
27-
b = reshape(a, (1, 1), copy=True)
28-
a[0] = 0
29-
assert all(b[0, 0] == 1)
30-
assert all(a[0] == 0)
31-
assert_raises(NotImplementedError, lambda: reshape(a, (1, 1), copy=False))
26+
a = asarray(np.ones((2,3)))
27+
b = reshape(a, (3, 2), copy=True)
28+
assert not np.shares_memory(a._array, b._array)
29+
30+
a = asarray(np.ones((2,3)))
31+
b = reshape(a, (3, 2), copy=False)
32+
assert np.shares_memory(a._array, b._array)
33+
34+
a = asarray(np.ones((2,3)).T)
35+
b = reshape(a, (3, 2), copy=True)
36+
assert_raises(AttributeError, lambda: reshape(a, (2, 3), copy=False))
3237

0 commit comments

Comments
 (0)