[Numpy-discussion] Ctypes support in NumPy

Thomas Heller theller at python.net
Tue Jul 4 04:56:45 CDT 2006

Travis Oliphant schrieb:
> I've been playing a bit with ctypes and realized that with a little 
> help, it could be made much easier to interface with NumPy arrays.  
> Thus, I added a ctypes attribute to the NumPy array.  If ctypes is 
> installed, this attribute returns a "conversion" object otherwise an 
> AttributeError is raised.
> The ctypes-conversion object has attributes which return c_types aware 
> objects so that the information can be passed directly to c-code (as an 
> integer, the number of dimensions can already be passed using c-types).
> The information available and it's corresponding c_type is
> data           -  c_void_p
> shape, strides -  c_int * nd  or c_long * nd or c_longlong * nd 
> depending on platform

I've also played a little, and I think one important limitation in ctypes
is that items in the argtypes list have to be ctypes types.  If that
limitation is removed (see the attached trivial patch) one can write a class
that implements 'from_param' and accepts ctypes arrays as well as numpy arrays
as argument in function calls (Maybe the _as_parameter_ stuff needs cleanup
as well).

The attached shape.py script implements this class, and has two examples.
The 'from_param' method checks the correct shape and itemtype of the arrays
that are passed as parameter.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: shape.py
Url: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060704/5d81e687/attachment.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: from_param.patch
Url: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060704/5d81e687/attachment-0001.pl 

More information about the Numpy-discussion mailing list