[Pyrex] Cannot cdef __init__?

Paul Prescod paul at prescod.net
Mon Jan 19 02:30:03 CET 2004


How about this variant:

cdef extern from "foo.h":
         cdef struct pair:
                 int x
         cdef int libblah_init_pair(pair **p, int y)
         int libblah_decref(pair *p)
         int libblah_incref(pair *p)

cdef class Pair:
         cdef pair *p
         def __init__(self, x, y):
                 rc = libblah_init_pair(&self.p, y)
                 if not rc:
                         raise "Error initializing p", "More info here"
                 self.p.x = x

         def __del__(self):
                 libblah_decref(self.p)

cdef class Fred:
         cdef Pair p

         def __init__(self, Pair p):
                 self.p = p

         def foo(self):
                 return self.p.p.x + self.p.p.y

def test(a, b):
         return Fred(Pair(a, b)).foo()

for i in range(1, 100):
         for j in range(2, 100):
                 assert test(i, j) == i+j

print "Done, exception should follow"
test(0, 0)






More information about the Pyrex mailing list