Pyrex without Python (was Re: calling Pyrex results from C)

A.M. Kuchling amk at
Wed Jan 21 13:47:10 CET 2004

On Tue, 20 Jan 2004 22:15:18 -0500, 
	François Pinard <pinard at> wrote:
> Maybe the fact that one can build and debug prototypes in Python, with
> the agenda of later transforming them into C, somehow, without rewriting
> so much of them, for environments where the whole of Python, for various
> reasons, is not welcome.

At work I've done a much simpler version of this.  We produce code for
mathematical algorithms or state machines that runs inside the Linux kernel;
code can be written either in C or in a tiny subset of Python (basically,
assignments, if...elif...else and for loops -- I forget if 'while' is
implemented or not). Instead of using Pyrex, I just use the compiler
package to get an AST for a chunk of Python code, and then have a code
generator that walks over the AST and produces C code.  

The nice thing is that the code generator knows the C-level types of
variables, so if a and b are arrays, a = a + b generates a 'for' loop in the
C code.  

In theory you could test them in Python, too, letting you debug problems
before having to compile C code and insert it into the kernel.  A long-term
pipe dream is to look at code and figure out which cases to test; for
example, if x is an input parameter and you have the statement 'if 0<x<5:
...', you should probably test with x=0, 1, 4, 5 to be sure all the edge
cases are correct.  


More information about the Python-list mailing list