[Numpy-discussion] __array_interface__ / __array_struct__

Thomas Heller theller at ctypes.org
Wed Jan 23 14:15:38 EST 2008


Travis E. Oliphant schrieb:
> Thomas Heller wrote:
>> Travis E. Oliphant schrieb:
>>   
>>> Thomas Heller wrote:
>>>     
>>>> I am experimenting with implementing __array_interface__ and/or __array_struct__
>>>> properties for ctypes instances, and have problems to create numpy arrays
>>>> from them that share the memory.  Probably I'm doing something wrong;
>>>> what is the correct function in numpy to create these shared objects?
>>>>
>>>> I am using numpy.core.multiarray.array(ctypes-object), is that correct?
>>>>   
>>>>       
>>> Yes, this should work, as the array function goes through several checks 
>>> including looking for the __array_struct__ and/or __array_interface__ 
>>> attributes.    If you can point me to the code, I can probably help.
>>
>> The pure-python code, using __array_interface__, is here:
>>
>> http://ctypes-stuff.googlecode.com/svn/trunk/numpy/
>>
> 
> This is all very good.  The only thing missing that causes the former to 
> not share memory is you are missing a  copy=False argument to the 
> multi_array function. 
> 
> Thus:
> 
> return multi_array(obj, copy=0)
> 
> is what you need to use.

Cool! Thanks for that, it works now ;-).

> Also, the address of the memory is also available as
> arr.ctypes.data  if arr is a NumPy array (but this is less general than 
> using the array interface for sure). 

I'm not completely sure what you are suggesting here; I just use all the
info in the numpy array's __array_interface__ property to find the ctypes
type to construct.  And the address also is available in this property.

Thanks,
Thomas




More information about the NumPy-Discussion mailing list