[Numpy-discussion] ctypes, numpy-array

Albert Strasheim fullung at gmail.com
Tue Jul 25 07:03:42 CDT 2006


Hello all

> -----Original Message-----
> From: numpy-discussion-bounces at lists.sourceforge.net [mailto:numpy-
> discussion-bounces at lists.sourceforge.net] On Behalf Of Lars Friedrich
> Sent: 25 July 2006 13:55
> To: numpy-discussion at lists.sourceforge.net
> Subject: Re: [Numpy-discussion] ctypes, numpy-array
> 
> > What's might be happening here is that a.ctypes.data is in fact being
> passed
> > to your function via ctypes's from_param magic (check the ctypes
> tutorial
> > for details).
> >
> > In [10]: x = N.array([])
> >
> > In [11]: x.ctypes.data
> > Out[11]: c_void_p(15502816)
> >
> > In [12]: x._as_parameter_
> > Out[12]: 15502816
> >
> OK, I did not know about x.ctypes.data...

Travis added this quite recently. Somebody (probably me) still has to update
the wiki to reflect these changes.

> > So what's probably happening here is that you already have a pointer to
> the
> > array's data which you then cast to a PyArrayObject pointer.
> Dereferencing
> > myPtr->data is looking for a pointer inside the array's data, which
> contains
> > zeros.
> 
> I understand.
> 
> > - look at ctypes's PyDLL option if you want to pass around Python
> objects
> 
> ???

You can read about PyDLL here:

http://docs.python.org/dev/lib/ctypes-loading-shared-libraries.html

I think PyDLL might turn out to be an interesting alternative to traditional
extension modules. But for wrapping C code, I think writing functions that
operate on pointers to ints and floats and whatnot works nicely.
 
> > - Write your function as:
> >
> > int foo(int* x);
> >
> > Then do something like this:
> >
> > x = N.array([...], dtype=N.intc)
> > mydll.foo.restype = None

Slight typo on my part. For this example it should be:

mydll.foo.restype = c_int

> > mydll.foo.argtypes = [POINTER(c_int)]
> > mydll.foo(x.ctypes.data)
> 
> I did that, and it worked fine for me. Thank you very much! This is
> really great.

Cool. Enjoy!

Regards,

Albert





More information about the Numpy-discussion mailing list