distutils and ctypes

jtravs at gmail.com jtravs at gmail.com
Wed Jan 10 03:36:26 EST 2007


Martin v. Löwis wrote:
> Robert Kern schrieb:
> > To which I say that doing the type-checking and error handling is much easier in
> > Python than using the C API. Add to that the tediousness of the edit-compile-run
> > cycle of C and the finickiness of refcounting.
>
> Sure: edit-compile-run is tedious, but in the given case, it is
> necessary anyway. Also, refcounting is finicky, but with ctypes,
> you often have to use just as finicky memory management APIs.
> (such as using specific allocation and deallocation routines,
> or having to do memory management at all when in C you would
> do stack allocation).
>
> My main concern with ctypes is that you have to duplicate
> information from the header files, which is error-prone,
> especially when the header files may change (either across
> versions or across target systems).
>

I have looked at: building an extension module, using pyrex and using
ctypes. Initially ctypes won because I was under the impression that to
use pyrex or build an extension module required the same compiler as
python was compiled with. This is not a problem on linux, but under
windows this is much too great a problem for potential users (who will
also need to compile the module). I now have discovered that I can use
mingw32 (without patching) under windows (with python2.5) though I'm
not clear if this is by chance or has been implemented as a new feature
(can't find suitable documentation).

Anyway, at this point I think I will stick with ctypes purely out of
simplicity. My c library is for some legacy lab hardware control. I
only need to call three functions. With ctypes, I can check the data in
python (which I do anyway to validate it) and pass it onto the library
in about 10 lines of code. Writing an extension module I believe would
be much more difficult.

Anyway,
Thanks both for your help.
John




More information about the Python-list mailing list