File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 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:
158158 # Instead of `__array__` we now implement the buffer protocol.
159159 # Note that it makes array-apis-strict requiring python>=3.12
160160 def __buffer__ (self , flags ):
161- print ('__buffer__' )
162161 if self ._device != CPU_DEVICE :
163162 raise RuntimeError (f"Can not convert array on the '{ self ._device } ' device to a Numpy array." )
164163 return memoryview (self ._array )
165164 def __release_buffer (self , buffer ):
166- print ('__release__' )
167165 # 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+ f"Interoperation with NumPy requires python >= 3.12. Please upgrade."
172+ )
168173
169174 # These are various helper functions to make the array behavior match the
170175 # 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():
369369 with pytest .raises ((RuntimeError , TypeError )):
370370 asarray ([a ])
371371
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+ na = np .array (a )
385+
372386
373387def test_allow_newaxis ():
374388 a = ones (5 )
You can’t perform that action at this time.
0 commit comments