Python bytecode compatibility between interpreter versions

Jon Perez jbperez808 at yahoo.com
Sun Mar 21 11:23:54 EST 2004


Andrew MacIntyre wrote:

> This becomes obvious when you consider the possibilities associated
> with Jython, which could (already does?) compile Python source to jar
> files directly.

A .jar file is just a .zip file composed of a collection of .class files
(JVM bytecodes) plus an additional text-based manifest file.

Jython _does_ compile Python source to .class files (because that is
really the only way to get it to run on the JVM), and the same will apply
for any other language, like Kawa (a Scheme implementation), that run 'on
top of Java' (i.e. gain access to the huge functionality present in
the Java libraries).

Essentially, whenever someone says so-and-so language runs on Java, it means
that they have a compiler for that langauge that emits JVM bytecodes.  These
bytecodes can then be run on any implementation of the JVM, whether a JIT-compiling
one or purely interpreted.  Further, you can also opt to take that bytecode / .class
file and compile it into native code via a native-code compiler like gcj.

The separation of the layers is very nice and clean and you can substitute
those coming from different sources and expect things to work:


Language Compiler (javac, Jikes, Pizza, Jython, Kawa, JIcon, etc...)
       |
       |  emits .class files which can be packaged into .jars
       |
       V
JVM implementation + Runtime Environment / native code compiler
  (Hotspot, Jalapeno, etc...)               (gcj, JRockit, etc...)
                                                     |
                                                     |
                                                     V
                                              native executable

Thus it is reasonable to take the exact same .class file compiled by, say,
Jython or Kawa on a Mac and expect it to run perfectly well on a JVM
implementation running on an x86 or an AS/400 minicomputer.  *This*
is what would be nice to have with .pyc files.

Note that there is one other consideration besides the presence of a JVM,
and that is the Runtime Environment (the APIs).  Swing may not have been
implemented on the AS/400, but it is implemented on X Window so the
Jython-Swing app you compiled on a Mac would work and look exactly alike
( sluggishly, that is... ;-) ) on a Solaris, FreeBSD, or Linux box.



More information about the Python-list mailing list