compiling to python byte codes

Maurice LING mauriceling at acm.org
Thu Sep 2 19:10:11 EDT 2004


Hi Martin,

Martin v. Löwis wrote:
> Michael Foord wrote:
> 
>> But that still doesn't answer the OPs question which is about writing
>> code in another language to generate python bytecode....
> 
> 
> I did. I told him about the compile() function, and indeed,
> 
> compile("2+4","<string>","eval")
> 
> generates Python bytecode.

Can I feed a python source file into compile(), line by line, and expect 
it to generate a working .pyc file? I suppose my intended use is to be 
able to handle python codes written at run time, to execute python codes 
line by line, in a python program. It is somewhat like a tracer routine 
that can interpret a line of python code, read out the variables, before 
going to the next line of python code. Can compile() do this, or do I 
have to use pexpect to run an instance of python?

> 
>> Is python bytecode *that* different to Java bytecode (not in detail
>> but in concept ?).
> 
> 
> Yes. Java bytecode is typed; Python bytecode is not.
> 
>> There's no reason why another compiler couldn't
>> emit python bytecode to run on the 'python virtual machine' ?

I was thinking that it may be simpler to say, write a PHP-to-Python 
compiler which compiles PHP into an intermediate form, which is then 
converted into python bytecodes, rather than trying to automate source 
code conversion from PHP to Python. Well, PHP is just an off-hand 
example, it may be COBOL or Pascal. Any ideas?

Regards
Maurice

> 
> 
> It is certainly possible. Indeed, the Python compiler does generate
> Python bytecode from source code, so it must be possible :-)
> 
> Regards,
> Martin
> 



More information about the Python-list mailing list