Skip to content

Commit e9527d5

Browse files
committed
ENH: rm __array__, add __buffer__
1 parent e4b6bfe commit e9527d5

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

array_api_strict/_array_object.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __hash__(self):
6868

6969
# See https://github.com/data-apis/array-api-strict/issues/67 and the comment
7070
# on __array__ below.
71-
_allow_array = True
71+
_allow_array = False
7272

7373
class Array:
7474
"""
@@ -161,26 +161,17 @@ def __repr__(self: Array, /) -> str:
161161
# This was implemented historically for compatibility, and removing it has
162162
# caused issues for some libraries (see
163163
# https://github.com/data-apis/array-api-strict/issues/67).
164-
def __array__(self, dtype: None | np.dtype[Any] = None, copy: None | bool = None) -> npt.NDArray[Any]:
165-
# We have to allow this to be internally enabled as there's no other
166-
# easy way to parse a list of Array objects in asarray().
167-
if _allow_array:
168-
if self._device != CPU_DEVICE:
169-
raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.")
170-
# copy keyword is new in 2.0.0; for older versions don't use it
171-
# retry without that keyword.
172-
if np.__version__[0] < '2':
173-
return np.asarray(self._array, dtype=dtype)
174-
elif np.__version__.startswith('2.0.0-dev0'):
175-
# Handle dev version for which we can't know based on version
176-
# number whether or not the copy keyword is supported.
177-
try:
178-
return np.asarray(self._array, dtype=dtype, copy=copy)
179-
except TypeError:
180-
return np.asarray(self._array, dtype=dtype)
181-
else:
182-
return np.asarray(self._array, dtype=dtype, copy=copy)
183-
raise ValueError("Conversion from an array_api_strict array to a NumPy ndarray is not supported")
164+
165+
# Instead of `__array__` we now implement the buffer protocol.
166+
# Note that it makes array-apis-strict requiring python>=3.12
167+
def __buffer__(self, flags):
168+
print('__buffer__')
169+
if self._device != CPU_DEVICE:
170+
raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.")
171+
return memoryview(self._array)
172+
def __release_buffer(self, buffer):
173+
print('__release__')
174+
# XXX what now? do we `del self._a` here?
184175

185176
# These are various helper functions to make the array behavior match the
186177
# spec in places where it either deviates from or is more strict than

0 commit comments

Comments
 (0)