File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -158,13 +158,18 @@ def __repr__(self: Array, /) -> str:
158
158
# Instead of `__array__` we now implement the buffer protocol.
159
159
# Note that it makes array-apis-strict requiring python>=3.12
160
160
def __buffer__ (self , flags ):
161
- print ('__buffer__' )
162
161
if self ._device != CPU_DEVICE :
163
162
raise RuntimeError (f"Can not convert array on the '{ self ._device } ' device to a Numpy array." )
164
163
return memoryview (self ._array )
165
164
def __release_buffer (self , buffer ):
166
- print ('__release__' )
167
165
# XXX anything to do here?
166
+ pass
167
+
168
+ def __array__ (self , * args , ** kwds ):
169
+ # a stub for python < 3.12; otherwise numpy silently produces object arrays
170
+ raise TypeError (
171
+ "Interoperation with NumPy requires python >= 3.12. Please upgrade."
172
+ )
168
173
169
174
# These are various helper functions to make the array behavior match the
170
175
# spec in places where it either deviates from or is more strict than
Original file line number Diff line number Diff line change @@ -369,6 +369,20 @@ def test_array_conversion():
369
369
with pytest .raises ((RuntimeError , TypeError )):
370
370
asarray ([a ])
371
371
372
+ # __buffer__ should work for now for conversion to numpy
373
+ a = ones ((2 , 3 ))
374
+ na = np .array (a )
375
+ assert na .shape == (2 , 3 )
376
+ assert na .dtype == np .float64
377
+
378
+ @pytest .mark .skipif (not sys .version_info .major * 100 + sys .version_info .minor < 312 ,
379
+ reason = "conversion to numpy errors out unless python >= 3.12"
380
+ )
381
+ def test_array_conversion_2 ():
382
+ a = ones ((2 , 3 ))
383
+ with pytest .raises (TypeError ):
384
+ np .array (a )
385
+
372
386
373
387
def test_allow_newaxis ():
374
388
a = ones (5 )
You can’t perform that action at this time.
0 commit comments