Compiling a subset of Python

Martijn Faassen m.faassen at vet.uu.nl
Mon Sep 10 19:55:58 EDT 2001


Gerson Kurz <gerson.kurz at t-online.de> wrote:
> Has anybody tried to write a native c compiler for a restricted subset
> of Python?

No, not really; though the idea isn't new. I had it a couple of years
ago (called the concept 'Swallow'). Python2C which you were pointed
at does generate C code, but it's more like a translator than a compiler. The
resulting code doesn't give a large boost to execution time, as far as
I'm aware, which is presumably why you'd like to have a compiler.

> In my naive view, some things obviously are not easily compiled (such
> as exec() and eval()), and some constructions (such as modifying
> globals() etc.) I don't really need if I get a compiler for a subset
> that fits *most* needs instead. 

> So, can somebody please explain explicitly *which* constructs are
> actually responsible for python lacking a compiler? 

I think it's not only constructs but also lack of manpower/funding that
is responsible. :)

Compiling a dynamically typed language like Python is a lot harder than
compiling a statically typed language. While there has been language 
research involving type inferencing and the like, the results of this
research aren't readily applicable to Python (i.e takes time and manpower).

An interesting compromise approach might be that of what I call Swallow;
strictly subset Python to make it more easy to compile, and allow for
type annotations to help the compiler.

I think the pieces for such may be very slowly falling into place. I'm
slowly plodding away at something that may help (sort of a reverse Swig),
and Greg Ward recently released the impressive 'Grouch' system:

 http://www.mems-exchange.org/software/grouch/

which has a different purpose (integrity checking of object databases)
but does offer a working implementation of a simple type system for
Python.

Don't expect to see a Python compiler before we see the completion of the
Python CPAN equivalent, though. :)

(indicentally Greg Ward developed a crucial component for that too;
the distutils)

Regards,

Martijn 
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list