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

Eric Huss e-huss at netmeridian.com
Wed Apr 5 21:23:59 CEST 2006


You currently can't do it.

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

Greg has alluded that this might be supported in the future, but I don't
see how it would be possible.  Let's say module A wants to access a C
method in module B, when module A is loaded, the dynamic linker needs to
resolve the symbols.  Since module B is not loaded, then it can't resolve
the symbols, and the import fail.  Putting "import B" inside module A
won't work because the code in module A doesn't get run until all the
symbols are resolved.

There are several ways to work around this.  One method that I have used
(which I think is very ugly) is to create a void * wrapper around your C
function using PyCObject_FromVoidPtr.  Then, in the module where you want
to call it, import the other module, get the value (which is a regular
Python object), and then use PyCObject_AsVoidPtr with all the correct
casting, and you now have a pointer to the function which you can call.

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

HTH,
-Eric

On Wed, 5 Apr 2006, Tomasz Primke wrote:

> > If you want to have the literal equivalent of a C include, you can use:
> >
> >     include "p1.pyx"
> >
> > Wherever you want to use that function.
>
> I'm not sure, what do you mean by "literal equivalent", so I'll try to
> describe what I want to achieve.
>
> I have a Pyrex module, called p1 (p1.pyx). I can "compile" it with pyrexc,
> so I'll get p1.c file. The file is then used to make a Python extension
> module, with my setup.py file - so I get the p1.so file (I work on a
> GNU/Linux box).
>
> Now: I have also other Pyrex modules, called p2 (p2.pyx), p3 (p3.pyx), and
> so on. I want to use that C_function function, defined in the p1 module, in
> my p2.pyx, p3.pyx (and so on) files (modules). In general: p2, p3 modules
> should use some functions from the p1 module. Or in another words: I need
> my modules to *share* some C (Pyrex) functions.
>
> I have already tried:
>
> --- p1.pyx file ---
> cdef public void C_function( int i ):
>   print "i =", i
>
> --- p2.pyx file ---
> #include "p1.pxi"
>
> def Cfun( int ):
>   C_function( i )
>
>
>
> And I was able to compile it and make Python modules (p1.so and p2.so), but
> when I did:
>
> >>> import p2
>
> I got some message about unresolved symbols or something... In general -
> Python wasn't able to find out, that the p1.so shared library should be
> also loaded (because it has the needed functionality).
>
>
> I realize, that I could do just:
>
> #include "p1.pyx"
>
>
> but wouldn't it cause code duplication in p2.so file? In the real life my
> modules will be much more complicated, so the code duplication is not a
> good idea...
>
>
> Best regards,
>
> 	Tomek
>
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
>



More information about the Pyrex mailing list