[SciPy-user] numpy array in ctype struct

Travis E. Oliphant oliphant@enthought....
Tue Jan 15 11:56:44 CST 2008


Thomas Heller wrote:
> Travis E. Oliphant schrieb:
>   
>> Paul Kienzle wrote:
>>     
>>> Hi,
>>>
>>> We are trying to create the following struct in ctypes:
>>>
>>>   struct {
>>>     int n,k;
>>>     double A[4][4], B[4][4];
>>>   } ;
>>>
>>> Easy enough:
>>>
>>>   class embedded_array(Structure):
>>>     _fields_ = [("n",c_int),("k",c_int),("A",c_double*16),("B",c_double*16)]
>>>   instance = embedded_array()
>>>
>>> Question:
>>>
>>>   Is there a way to map the data in A/B into a numpy array, so that we
>>>   can use it directly?
>>>
>>>   
>>>       
>> Yes.   You can create an array from a pointer to memory and a 
>> description of the data that is much like the ctypes structure.   At 
>> some point, there should be a direct conversion from ctypes objects to 
>> NumPy data-types, but nobody has written that yet.
>>     
>
> [...]
>
> Is there anything that should be added to ctypes (Python 2.6 isn't too
> far away) that would help with this conversion?
>   
One approach would for ctypes objects to describe their data-type using 
the new additions to the struct syntax (i.e. fully support the new 
buffer interface including data-type description as defined in PEP 
3118).   This should be going in Python 2.6 as well (but I need help in 
the implementation). 

Then, NumPy arrays will support consuming the syntax as well and it 
should be an easy thing to pass back and forth between NumPy array views 
and ctypes-objects views of memory.   I think such a thing would be very 
powerful.



-Travis O.



More information about the SciPy-user mailing list