[Pyrex] How to use C functions in many Pyrex files?
Tomasz Primke
tprimke at interia.pl
Thu Apr 6 16:16:09 CEST 2006
> What Josiah is recommending is to use the Pyrex "include" capability,
> which will essentially include the source of the other module verbatim.
> You will thus end up with two copies of the same code (one in each
> module).
And that's why I don't like the idea.
> <snip>
>
> 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 )
It had compiled, but then I tried:
>>> import p2
>>> p2.fun( 1 )
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "p2.pyx", line 5, in p2.fun
p1.instance.C_function( i )
AttributeError: 'module' object has no attribute 'instance'
So I decided to find a different solution.
So far, the only way to "work around" this problem was to write a Python
function wrapper for the C one in p1 module:
--- p1.pyx ---
cdef C_function( int i ):
...
def Cfun( int i ):
C_function( i )
--- p2.pyx ---
import p1
# now it's possible to call the p1.Cfun function
It was the only solution, that worked. So far I'm happy with that, but I
wouldn't mind, if some more convenient way is developed.
Thank you all for your help.
Best regards,
Tomek
More information about the Pyrex
mailing list