[Numpy-discussion] Support for understand ctypes data-types now in SVN

Travis Oliphant oliphant at ee.byu.edu
Fri Nov 17 15:42:56 EST 2006


Stefan van der Walt wrote:

>On Fri, Nov 17, 2006 at 12:43:45PM -0700, Travis Oliphant wrote:
>  
>
>>The NumPy dtype command now understands ctypes data-types including 
>>derived structures and arrays.
>>
>>This means you can specify data-types using ctypes method of building 
>>structures and pass those classes whenever NumPy expects a data-type 
>>object.
>>
>>Simple example:
>>
>>import ctypes as ct
>>import numpy as np
>>
>>np.array([1,2,3],ct.c_short)
>>
>>More complicated:
>>
>>class POINT(ct.Structure):
>>         _fields_ = [("x", ct.c_int),
>>                           ("y", ct.c_int)]
>>
>>np.array([(3,4),(5,6),(7,8)], dtype=POINT)
>>    
>>
>
>Is the memory layout of the array above the same as what an array of
>POINTs would be in C, or are they not padded? 
>
I just changed this in SVN.  Previously it would not use padding because 
the array_descriptor format assumed all padding was specified.  I 
changed this so that array_descriptor format can also now account for 
padding. 

Then, if the object has a _fields_ attribute, then it automatically does 
padded construction.

As a result, now using ctypes objects will guarantee padding.

-Travis




More information about the NumPy-Discussion mailing list