@@ -67,9 +67,7 @@ def __hash__(self) -> int:
67
67
CPU_DEVICE = Device ()
68
68
ALL_DEVICES = (CPU_DEVICE , Device ("device1" ), Device ("device2" ))
69
69
70
- # See https://github.com/data-apis/array-api-strict/issues/67 and the comment
71
- # on __array__ below.
72
- _allow_array = True
70
+ _default = object ()
73
71
74
72
75
73
class Array :
@@ -163,28 +161,17 @@ def __repr__(self) -> str:
163
161
# This was implemented historically for compatibility, and removing it has
164
162
# caused issues for some libraries (see
165
163
# https://github.com/data-apis/array-api-strict/issues/67).
166
- def __array__ (
167
- self , dtype : None | np .dtype [Any ] = None , copy : None | bool = None
168
- ) -> npt .NDArray [Any ]:
169
- # We have to allow this to be internally enabled as there's no other
170
- # easy way to parse a list of Array objects in asarray().
171
- if _allow_array :
172
- if self ._device != CPU_DEVICE :
173
- raise RuntimeError (f"Can not convert array on the '{ self ._device } ' device to a Numpy array." )
174
- # copy keyword is new in 2.0.0; for older versions don't use it
175
- # retry without that keyword.
176
- if np .__version__ [0 ] < '2' :
177
- return np .asarray (self ._array , dtype = dtype )
178
- elif np .__version__ .startswith ('2.0.0-dev0' ):
179
- # Handle dev version for which we can't know based on version
180
- # number whether or not the copy keyword is supported.
181
- try :
182
- return np .asarray (self ._array , dtype = dtype , copy = copy )
183
- except TypeError :
184
- return np .asarray (self ._array , dtype = dtype )
185
- else :
186
- return np .asarray (self ._array , dtype = dtype , copy = copy )
187
- 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 anything to do here?
188
175
189
176
# These are various helper functions to make the array behavior match the
190
177
# spec in places where it either deviates from or is more strict than
0 commit comments