[Pyrex] Total newbie needs code review

Chris Stromberger chris.stromberger at gmail.com
Thu Sep 8 18:57:23 CEST 2005


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)
  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

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


Thanks,
Chris



More information about the Pyrex mailing list