Skip to content

Commit 1914bbf

Browse files
committed
ENH: rm __array__, add __buffer__
1 parent cf3f717 commit 1914bbf

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

array_api_strict/_array_object.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ def __hash__(self):
6666

6767
_default = object()
6868

69-
# See https://github.com/data-apis/array-api-strict/issues/67 and the comment
70-
# on __array__ below.
71-
_allow_array = True
7269

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

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

0 commit comments

Comments
 (0)