[Pyrex] Total newbie needs code review

len-l at telus.net len-l at telus.net
Fri Sep 9 05:36:36 CEST 2005


On 8 Sep 2005 at 11:57, Chris Stromberger wrote:

> I just discovered Pyrex when I decided I wanted to call a couple of C
> functions from a 3rd party library from Python.  Using examples found
> on this list and elsewhere, I was able to get something working.  Now
> I want to be sure that it is working *correctly* (eg, no mem
> management issues I am overlooking, etc).  If anyone can give this a
> once-over I'd appreciate it.  The C lib has only two functions and two
> structs I am interested in.
> 
> cdef extern from "3rd_party_lib.h":
>   cdef struct inputs:
>     char   *s
>    ... (other primitives snipped)
> 
>   cdef struct outputs:
>       int     x
>       double  y
> 
>   outputs *cfunc(inputs *x, outputs *y)

cfunc is ambiguous. Is this the way it is used in C?

struct inputs i;
struct outputs o, *p_rval;

/* fill in input fields */
i.s = "Some string";
...

p_rval = cfunc(*inputs, *outputs);  /* p_rval == &outputs ? */

>   int init(char* fname)
> 
> class Analyzer:
>   def __init__(self, fname):
>     ret = init(fname)
>     if ret != 0:
>       raise "init failed, returned %d" % ret
> 
>   def pyfunc(self, pyinputs):
>     cdef inputs* cinputs
>     cinputs.s = pyinputs.s
>     ...(copy all members over to C struct)
> 
>     cdef outputs* o
>     o = cfunc(i, o)
>     return o.x, o.y
> 
More than likely pyfunc should be:

    def pyfunc(self, pyinputs):
        cdef  inputs cinputs   # An instance, not a pointer
        cinputs.s = pyinputs.s
        ...

        cdef outputs o      # Also an instance

        cfunc(*cinputs, *o)   # Return value not needed; is just pointer to o
        return o.x, o.y

but  only if I guessed right on how cfunc works.

> class PyInputs:
>   def __init__(self):
>     self.s = ""
>    ...(repeat same primitives as C inputs struct)
> 

Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list