[Numpy-discussion] ctypes, numpy-array
fullung at gmail.com
Tue Jul 25 07:03:42 CDT 2006
> -----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
> > to your function via ctypes's from_param magic (check the ctypes
> > for details).
> > In : x = N.array()
> > In : x.ctypes.data
> > Out: c_void_p(15502816)
> > In : x._as_parameter_
> > Out: 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
> > array's data which you then cast to a PyArrayObject pointer.
> > myPtr->data is looking for a pointer inside the array's data, which
> > zeros.
> I understand.
> > - look at ctypes's PyDLL option if you want to pass around Python
You can read about PyDLL here:
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.
More information about the Numpy-discussion