[Pyrex] How to pass the data element of a Numeric Array to a c function

Greg Ewing greg at cosc.canterbury.ac.nz
Mon May 24 03:46:28 CEST 2004


Vineet Jain <vineet at eswap.com>:

> #define FDATA(p) ((float *) (((PyArrayObject *)p)->data))
> localCvariable = FDATA(NumericPythonArray);
> some_c_function(localCVariable)
> 
> How can I do the same in pyrex.

Macros which do type casting can't be translated into Pyrex. You'll
have to write the cast out explicitly wherever it's needed, e.g.

  localCvariable = <float *>NumericPythonArray.data

> some_c_function(NumericPythonArray.data)
> 
> but that gives me an exception that cannot convert python object to
> (float(*))

That sounds like you have another problem as well.  NumericPythonArray
will need to have been declared as referencing a Numeric array object,
defined as an external extension type. Have you looked at the Numeric
example in the Demos directory?

> when I do
>
>  from numeric import zeros, searchsorted
>
> when I call zeros does it call the c function directly or does it have to go
> through some python interface.

It will make a Python call. But you should be able to access Numeric's
C API if you make the appropriate cdef extern declarations (and
remember to call import_array() before using any of them).

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list