[Pyrex] How to reuse pyrex functions?

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Fri Jul 28 17:30:45 UTC 2006



Daniele Varrazzo wrote:
>> > i am trying to declare Pyrex function in a module called "asn1" and use
>> > it in a module called "mdc". I can't put a function declaration in a
>> > .pyd file, so i must use a "cdef public". Anyway i can't make it work
>> > smoothly.
>>
>> I guess what you are looking for is my Pyrex patch for external C
>> APIs. It
>> allows different Python C extensions to directly call C functions of each
>> other without requiring any compile-time linking.
> 
> It looks what i was looking for, but i didn't managed to use it. Maybe
> i'm just misunderstood its usage at all...
> 
> i tried adding the function declarations i wanted to export in a .pyd
> file, such
> 
>    cdef object OCTET_STRING_GetString(OCTET_STRING_t *s)
> 
> but i still get errors:
> 
>    Only 'extern' C function or variable declaration allowed in .pxd file
> 
> isn't the patch intended to overtake this limitation?


Well, as far as the second module is concerned (the one calling the public
functions), the functions /are/ more or less 'extern'. Note that the patched
Pyrex generates a header file for the public functions. You must then create a
.pxd that contains the Pyrex declarations of the public functions as

cdef extern from "theheaderfile.h":
   ... your functions here ...

Here is an example using the generated header file "etree.h":

http://codespeak.net/svn/lxml/branch/capi/src/lxml/etreepublic.pxd


> Anyway i read the thred i think you refer to and it almost convinced
> me to avoid dynamic linking and create a single library.

You should, mainly because it's the most portable solution (and it works).

Stefan




More information about the Pyrex mailing list