C++ and Python

Thomas Heller theller at ctypes.org
Sat Mar 10 17:50:17 CET 2007

Alex Martelli schrieb:
> hg <hg at nospam.org> wrote:
>    ...
>> target but rather C: I need to integrate a printer driver and and would
>> like if possible to avoid all of the .h stuff involved with SWIG (I am not
>> being sarcastic): if I can setup my prototypes directly in python, why go
>> through an extra layer ?
>> Aren't ctypes better suited to such an application ?
> One advantage of solutions generating C code that gets compiled (SWIG,
> Pyrex, writing C-API code directly, etc) is that the C compiler can warn
> you, or produce compile-time mistakes, for many kinds of programming
> errors; with ctypes, you will instead be finding every such error at
> runtime, each time by noticing that Python crashed and facing a
> substantial amount of debugging and code inspection to find out why.

In principle this is true, some points however I would like to point out:

- The ctypes codegenerator, based on GCCXML, is in a pretty good shape.
  A lot of boilerplate (Python) code can be generated automatically.
  The codegenerator is available from CVS, with 'easy_install ctypeslib'.
  GCCXML has to be installed separately.

- Not always does Python crash on programming errors.  On windows, calls
  to foreign functions are 'protected' by windows structured exception
  handling.  In other words, most of the time you get a Python traceback
  instead of a segfault.

- There are some large libraries and also smaller ones that are already
  wrapped with ctypes.  PyOpenGL is probably the most promiment example,
  comtypes (although still in heavy development) another one.

As the ctypes author I'm of course biased.


More information about the Python-list mailing list