JPython is a Java implementation of the object -oriented scripting language called Python that has been certified as 100% pure Java. In other words, JPython does not use any of the C code of the original Python implementation. It is a version of Python that runs on top of the Java Virtual Machine. Thus, it allows you to run Python on any Java platform and enables your JPython applications to have access to any Java library that you might need.
JPython offers the same language as Python, but it has a different implementation. Python is a scripting language whose interpreter is written in C, which means that this implementation is called CPython. On the other hand, JPython re-implements the Python parser and interpreter in Java. Actually, the interpreter is available as a Java application, which allows Python programs to be created and seamlessly integrated with the Java platform. Note however that both types of implementation (Python and JPython) have their own pace. Efforts are made to keep them in sync, but it is not guaranteed that this will always happen.
JPython carries a license that complies with the Open Source definition. It is freely available for both commercial and non-commercial use and is distributed with source code.
JPython was designed by Jim Hugunin, who is also the main author of the Numeric Python extensions. He has joined Guido at CNRI in 1998, leaving in mid-1999 to join the Xerox PARC team in California. When he left, Barry Warsaw took care of the JPython project, and he is the main person responsible for the project since then. Currently, JPython is maintained by Warsaw at BeOpen Python Labs.
At the Web site, you can always obtain the latest information about this implementation and download info. In case you are interested in downloading the source code, it is available via CVS. The latest version of JPython was released in January of 2000.
If you want to be in touch with other users, JPythoners have their own mailing list that you can use to discuss JPython, ask questions, and help other users as well.
JPython's core is an interpreter engine, similar to CPython's, that provides support to the greater part of the Standard Python Library. JPython has a set of features that includes
- High-level built-in data types
- Dynamic typing
- Optional static compilation (which allows the creation of beans, applets, servlets, and so forth)
- Java classes and packages support
- A set of support libraries
- Interactive compilation direct to Java bytecodes (which enables JPython programs to run directly on a JVM, including running as applets in Web browser windows).
JPython uses the Java's introspection mechanism to understand the contents of the package, which means that when you want to import a Java library, say something such as import java.awt JPython first tries to find a Python package named java. And in case it doesn't find one, it uses the Java mechanism to search in the CLASSPATH system variable for a .class or .jar file that meets the requirement.
Another important aspect of JPython is that you can create JPython classes that subclass Java classes, extending JPython functionality without the need for using wrapper generators (such as SWIG —Simplified Wrapper and Interface Generator), provided the functionality is already implemented in Java code. Otherwise, you probably need to use the JNI interface to make the code available to JPython. This allows you to easily use the same set of classes that a regular Java application has access to. The opposite is also true. Java has total integration with JPython, and because JPython is 100% pure Java certified, you might embed JPython in your Java applications without any worries of compromising the portability of your application. If necessary, you can even pass JPythonobject instances back to Java (executing callbacks), and manipulate these instances in the Java application because any Java code that processes Java objects is also able to process JPython objects.
Sometimes, you might find JPython useful for testing your Java components. As JPython has total integration to the Java implementation, you can use it to call, test, and debug the functionality of Java functions through the interpreter. JPython is the perfect choice for controlling Java through an interactive language.
JPython also handles memory management with care in order to remove from you the responsibility of being worried about object circular references. To do so, it uses the power of the Java memory management mechanism, a mechanism that implements a garbage collector, which is executed at runtime.
JPython's performance is still not as good as the performance of CPython, but there is a lot of effort going on in order to make JVMs run faster.
Both CPython and JPython are implementations of the same language: Python. Even though the development team tries to make both codes as compatible as possible, differences are inevitable. Each one of these languages is written using a different programming language: C and Java, respectively. This primary difference is more than enough to cause both codes to have many distinctive characteristics that are unlikely to disappear soon. The following is a short list of differences between both implementations:
- JPython uses a slightly modified interface to handle scientific notation. For example, if you type at the CPython interpreter the command print 9E+54, CPython will echo the number9e+054, whereas JPython will pop up 9.0E54.
- JPython has everything implemented to be an instance of a class, which is opposite of CPython.
- JPython doesn't provide access to the co_codeattribute of code objects because JPython code objects don't have access to any Python bytecodes as CPythoncode objects do. That's because the JVM hides their existence.
- JPython doesn't allow the use of Python extension modules written in C. If you want to use them, you need to rewrite them in Java, or use JNI to make the C code available to Java.
- JPython offers a Standard Python Library of modules slightly different than CPython does because the built-in modules, which are written in C, need to be ported to Java in order to be used by JPython. Some modules, such as cPickle, cStringIO, and binascii, have already been ported. Another possible option to access the built-in modules is by implementing a JNI bridge.
By typing import <modulename>, and later dir(<modulename>), you can check whether a module is available, and if so, what its interface looks like.
- JPython is able to catch the CTRL+C command when you want to leave the interpreter. However, CPython doesn't accept this shortcut to perform that same functionality.
- JPython implements the garbage collection mechanism native to Java, instead of the reference counting mechanism implemented by CPython.
- JPython doesn't provide any guarantees of telling you exactly when an object is about to be destroyed, as CPython does. Opposite to CPython, the __del__() method of the object is never called.
- JPython's interpreter has a set of command line options completely different from the ones recognized by the CPython interpreter.
The following Web page shows all known differences between the two implementations of the Python language, including the ones listed in this topic:
Python Server Pages
PSP is a freely available server-side scripting engine. It is 100% written in Java, thus, it is portable to several platforms. PSP is mentioned here because it uses JPython as its scripting language. In order to use PSP, you need to have a Web Server that supports Java Servlets, or uses JRun from Live Software, which is a Java Servlet engine recommended for use with PSP.