[Pyrex] How to use C functions in many Pyrex files?

Eric Huss e-huss at netmeridian.com
Wed Apr 12 22:26:09 CEST 2006


> > Another method is to create an extension type, put your C function
> > as a method of the extension type, create a pxd file, and use
> > cimport.  (All described in
> > http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/version/Doc/sharing.h
> >tml).
>
> I tried it, but I failed. I did it as follows:
>
> --- p1.pxd ---
> cdef class SomeClass:
>   cdef public void C_function( int i )
>
> --- p1.pyx ---
> cdef class SomeClass:
>   cdef public void C_function( int i ):
>     ...
>
> cdef public SomeClass instance  # I have also tried without "cdef public",
> 				# but with the same result
> instance = SomeClass()
>
> --- p2.pyx ---
> cimport p1
> import p1
>
> def fun( int i ):
>   p1.instance.C_function( i )

FYI, you need to create the instance in p2.pyx.  Also, the "public"
attributes in p1 are not necessary.

So, remove creating the instance in p1.pyx, and then p2.pyx would look
something like this:

--- p2.pyx ---

cimport p1

cdef p1.SomeClass instance

instance = p1.SomeClass()

def fun(int i):
    instance.C_function(i)

--------------

-Eric




More information about the Pyrex mailing list