[Pyrex] C-level constructors

David McNab david at rebirthing.co.nz
Wed Nov 7 11:38:49 CET 2007


Hi,

Is there any way to implement an extension type's constructor so that it
can be called with C data types (even if this means not being able to
invoke the constructor from Python code outside of the Pyrex
environment)?

For example:

  cdef class Foo:
      cdef int foo
      def __init__(self, int foo):
          self.foo = foo

This is straightforward, the compiler turns it into fairly efficient C.

But what if an extension type has attributes that don't map easily to
Python objects, for example:

   cdef class Foo:
       cdef unsigned short **audioBufs
       cdef __init__(self, short **audioBufs):
           self.audioBufs = audioBufs

which is illegal.

For now, I'm implementing my extensions with no __init__ or __cinit__,
and instead adding a method '_init_', eg:

   cdef class Foo:
       cdef unsigned short **audioBufs
       cdef _init_(self, short **audioBufs):
           self.audioBufs = audioBufs

and when constructing, I do:

   cdef Foo myfoo
   cdef short **bufs
   ...
   myfoo = Foo(); myfoo._init_(bufs)

But - is there a better way to do this?

What I'm aiming for is to be able to construct these objects with
absolute minimum overhead, since they will be getting created/destroyed
within tight loops.

Thanks if you can help

Cheers
David






More information about the Pyrex mailing list