Python -> C++ compilation

Martin von Loewis loewis at informatik.hu-berlin.de
Sun May 27 10:43:30 EDT 2001


Roman Suzi <rnd at onego.ru> writes:

> Is there any tool that could translate Python code to C++? (We all know
> C++ is an Assembly, compared to Python ;-)

There is the mysterious "Compiler" tool by Andreas Otto,

http://home.t-online.de/home/aotto/comp-main_E.html

who claims that extending it to Python using the "Token Stream"
technology is no problem.

Then we have Jonathan Riehl's PyFront,

This is a technology I trust more: it essentially unwraps the ceval
loop, and glues pieces of C code together for each byte code
instruction, making additional optimizations where possible.

Next we have John Aycock's 211,

http://www.foretec.com/python/workshops/1998-11/proceedings/papers/aycock-211/aycock211.html

It translates parts of the application into C code, and assigns the
new opcode 211 as an entry point into the various compiled routines.
Please have a look at the numbers above section three, John's
conclusion is that the code size increase is unacceptable.

> Of course, assuming no exec's or evals (implicit or explicit) are
> used.  And type information is provided by some means.

The latter two compilers work correctly even with execs and evals, and
without any additional type information.

> I do not think it is possible without embedding, but everything happens.
> Probably, this post is utterly silly, but... OTOH, There are tools like
> Pascal->C, Basic->C, Fortran->C, why not Python->C++?

Mostly because nobody cares enough. It does work, yes, but gives too
much problems for too little gain.

The basic question is why you'd want to have such a thing. To make a
stand-alone executable? Use freeze, or the installer. To gain
execution speed? In your typical Python program, much execution speed
can be gained with small modifications - although those modifications
are often hard to find. Because it's cool? That certain was the
primary motivation for the two IPC authors. Because you could sell it
for a lot of money? Dunno, you may ask Andreas Otto about that.

Regards,
Martin






More information about the Python-list mailing list