[Numpy-discussion] Ctypes support in NumPy

Albert Strasheim fullung at gmail.com
Tue Jul 4 07:58:05 CDT 2006

Hello all

On Tue, 04 Jul 2006, Thomas Heller wrote:

> Albert Strasheim schrieb:
> > Hey Thomas
> > 
> > Thomas Heller wrote:
> >> Thomas Heller schrieb:
> >> > 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.
> >> 
> >> Thi swas misleading:  I mean that this limitation should probably be
> >> removed, because it prevents a lot of things one could do.
> > 
> > What's your thinking on getting these changes made to ctypes and on ctypes'
> > future development in general?
> > 
> > Presumably you can't change it too much with the Python 2.5 release coming
> > up, but it would be a shame if we had to wait until Python 2.6 to get the
> > changes you suggested (and other goodies, like the array interface).
> I have asked on python-dev, let's wait for the answer.
> I hope that at least the limitation that I mentioned can be removed in Python 2.5.

Sounds great.
> The goal of my post was to show that (without this restriction) a lot can
> already be done in Python, of course it would be better if this could be
> implemented in C and integrated in ctypes.
> For the numpy/ctypes inegration I'm not absolutely sure what would be needed most:
> Is there a need to convert between ctypes and numpy arrays?  If numpy arrays can
> be passed to ctypes foreign functions maybe there is no need at all for the conversion.
> We could probably even live with helper code like that I posted outside of ctypes...

I think there are basically two ways for a C library to work with 
regards to memory allocation:

1. let the user allocate the array/struct/whatever and pass a pointer to 
the library to manipulate
2. let the library allocate the array/struct/whatever, manipulate it and 
return the pointer to the user

I think the first case is pretty much covered. Where in the past you 
would create the array or struct on the stack or allocate it on the heap 
with malloc, you now create a ctypes Structure or a NumPy array and pass 
that to the C function.

In the second case, one would want to wrap a NumPy array around the 
ctype so that you can manipulate the data returned by the library. I 
don't know if this second scenario is very common -- hopefully not. If 
not, then having ctypes implement the array interface isn't too 
critical, since you wouldn't typically need to make a NumPy array from 
existing data.

What do you think?



More information about the Numpy-discussion mailing list