[Pyrex] Pyrex versus ctypes

Bryan Weingarten bryan.weingarten at pobox.com
Sun Jun 13 09:01:34 CEST 2004


David McNab wrote:

> Hi,
>
> After a year or so's hacking with Pyrex, I've come across the 'ctypes' 
> package (http://starship.python.net/crew/theller/ctypes/).
>
> ctypes allows the calling of C functions from *pure-python* code, 
> without requiring the generation/compilation of a C wrapper module.
>
> Pure python code using ctypes can even receive callbacks from C funcs, 
> and manipulate C data structures, pretty much most of what one would 
> need for interfacing with C.
>
> All this feels too good to be true.
>
> Can anyone offer some thoughts on the ideal demarcation lines between 
> Pyrex and ctypes, and some thoughts on pros and cons, and which 
> situations indicate Pyrex over ctypes and vice versa?
>
> For instance, how severe are ctypes' overheads with constructing the C 
> call frame, and rendering the return back to python?
>

i've used both pyrex and ctypes to wrap a complex c library at work.   
by complex, i mean the library had functions that returned structures to 
pointers to functions that passed in complex structures (again with 
pointers to functions) :)  on windows, it was pretty much a wash.  i had 
both working within a short period of time.   also, pyrex required a bit 
less code to write.   but on solaris and linux, it wasn't smooth at 
all.  on unix ctypes requires other libraries such as libffi  it took a 
day before we could even get the ctypes implementation to run, then it 
core dumped part way in.  on linux, it never even ran.  but pyrex just 
worked first time on all platforms.  pyrex also doesn't require any 
other libraries, just the one small  c module that you compile.  this is 
a *huge* advantage when distributing the code to your users.    but for 
windows only projects that are light weight to mediem complexity, i 
would probably try ctypes first since you do get to stay in the python 
environment.

bryan




More information about the Pyrex mailing list