From: Thomas H. <th...@py...> - 2006-07-04 09:56:54
|
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. Thomas |