Question to python C API

Stefan Behnel stefan_ml at behnel.de
Thu Apr 16 18:39:53 CEST 2009


Andreas Otto wrote:
>   the problem with such kind of framework is usually
>   that you start with the easy stuff and than (after a couple
>   of days/weeks) you come to the difficult stuff and you
>   have to figure out that this kind of problem does not
>   fit into the tool.

That is a very common problem with so-called "wrapper generators", such as
SWIG, PyBindGen, sip, and some others. They get you to a 1:1 wrapper
quickly, but as soon as you try to abstract your wrapper from the
underlying C API, you start reaching the limits of the tools almost as
quickly (or at least have a hard time pushing the tool the way you want).


>   stuff what I do is:
> 
>         1. create objects on the fly as connection handle
>         2. do callbacks from C to Python
>         3. create and delete threads or manage to continue
>                 work after an fork 
>         4. is server mode start an event-loop and never come
>                 back

Not uncommon for a C library wrapper at all. I assume that the "server
mode" event-loop calls back into Python from time to time? You will sooo
love the "with gil" function feature in Cython...


>   what I want:
> 
>         I want to use this tool but still be able to intermix
>         parts of my "C" helper code with the python code
> 
>   question:
> 
>         it is possible to write C and python code into the 
>         same file ?

I'm glad you asked :)

That's basically what the Cython language is all about. Think of it as a
programming language that is almost Python, but at the same time allows you
to work with C data types and do direct calls into C code. All of that gets
translated into very fast C code that you can easily hand-tune to your
needs. You basically have all the freedom of the Python language with all
the freedom of the C language.

Stefan



More information about the Python-list mailing list