jython 2.7 and techniques for integrating with java - frank wierzbicki
DESCRIPTION
Jython 2.7 is scheduled to release by July 15. Frank will talk about the new features for Jython 2.7 and plans for Jython 3.x. He will also spend some time talking about techniques for integrating Jython with Java. Jython 2.7 release is sponsored by AdconionTRANSCRIPT
Jython 2.7 UpdateFrank Wierzbicki
Wednesday, May 9, 2012
Why use Jython?
• Massive amounts of Java code out there
• Some really useful Java libs
• Great JVM ecosystem
• But we’d all rather be writing Python code, right?
Wednesday, May 9, 2012
Status
• Jython 2.7 is progresing well
• bytearray and io are the last missing big features
• 2.7 Unit test compliance
• 684 failing tests out of 10786 (~6%)
• Most of these are due to bytearray/io
Wednesday, May 9, 2012
Dates
• Jython 2.7 work is being sponsored by Adconion. (Thanks!)
• Jython 2.7 alpha should be out this month
• The target for a production release is July 15 - which happens to be the final day of my contract to get Jython to 2.7 :)
Wednesday, May 9, 2012
Why is Adconion sponsoring Jython?• Adconion is a hybrid Java/Python shop
based out of LA
• They use Python version 2.7 and Jython 2.5
• They have internal Django apps that access Java libraries
• The 2.5/2.7 differences are a pain
• They’d like to give back to the community
Wednesday, May 9, 2012
Collaborators
• Major apps
• Implementations - CPython, PyPy
• Other Java dynamic languages - JRuby
• Tooling support
• Java Virtual Machine development
• Academic research - gradual typing
Wednesday, May 9, 2012
Features
• Abstract Base Classes
• Bytearray
• __format__
• OrderedDict
• lots of Lib updates
• See CPython 2.6/2.7 what’s new for more
Wednesday, May 9, 2012
Not in Jython 2.7
• multiprocessing
• buffer (so far never has been in Jython)
• memoryview (though there is a start, we’ll see)
Wednesday, May 9, 2012
Java Platform
• Starting to (optionally) use invokedynamic
• Everything is compiled to Java Bytecode
• Use a choice of Java garbage collectors
• Always uses Java native threads
• All cores are used
java.util.concurrentnot interpretedno GIL
-J for setting options like one of 20 or so GCsheap sizeetc
Wednesday, May 9, 2012
Java Integration
• Jython integrates well with Java
• Jython makes calling into Java look like standard Python code
• Java classes and interfaces can be subclassed from Jython
Wednesday, May 9, 2012
builtin Java interfaces
• Jython’s list implements Java List<Object>
• Jython’s dict implements Map and ConcurrentMap
• Jython’s set implements Java Set
Wednesday, May 9, 2012
//Java interfacepackage org.acme;
public interface MyInterface { public List strList();}
#From Python in file “acme.py”from org.acme import MyInterface
class pyobject(MyInterface): def strList(self): return [‘a’, ‘b’, ‘c’]
//From JavaPythonInterpreter interpreter = new PythonInterpreter ();interpreter.exec ("from acme import pyobject");MyInterface pythonObject = interpreter.get ("pyobject");
List pywords = pythonObject.strList()for (Object o : pyList){ String string = ((PyObject)o).__toJava__(String.class); //Do something with the now Java native strings.}
Wednesday, May 9, 2012
PlyJy
• For a more sophisticated approach to calling into Python code from Java in Jython see the PlyJy project
Wednesday, May 9, 2012
import org.plyjy.factory.JythonObjectFactory; JythonObjectFactory factory = JythonObjectFactory.getInstance();MyInterface pythonObject = (MyInterface) factory.createObject( MyInterface.class, "acme.pyobject");
Wednesday, May 9, 2012
Jython package cache
[frank jython]$ ./dist/bin/jython*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/jython-dev.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-2.7.7.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-3.1.3.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-runtime-3.1.3.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/asm-4.0.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/asm-commons-4.0.jar'
...
Wednesday, May 9, 2012
Java package cache
• Goes through all jars and classes and figures out Java package contents
• enables “from java.foo import *”
• Java does not have a package.getClasses()
• If cache is disabled “from java import *” will not work from Jython
• * import for pure python still works
Wednesday, May 9, 2012
zxJDBC
• zxJDBC implements DB-API
• integrated into Jython’s core
• A crucial piece for SQLAlchemy, Pyramid and Django support.
Wednesday, May 9, 2012
Modjy
• Modjy implements WSGI for Jython
• Acts as a bridge to Java Servlets
• integrated into Jython’s core
• Pyramid and Django support
Wednesday, May 9, 2012
Working apps
• SQLAlchemy
• Pyramid, DJango
• setuptools, ez_install, distribute
• virtualenv
• pip
• many more pure python apps
Wednesday, May 9, 2012
ez_install
• Get ez_setup.py
• run “jython ez_setup.py”
• In Jython’s bin directory will be an ez_install that runs from Jython
Wednesday, May 9, 2012
Install Django
• Get 1.3+ Django - run “jython setup.py install”
• From http://django-jython.googlecode.com get django-jython-1.3.tar.gz, untar it and run “jython setup.py install”
• see the django-jython project on googlecode.com for more detail
Wednesday, May 9, 2012
Deploy Django
• run “jython manage.py war --include-java-libs=jdbcdriver.jar”
• This will produce a “war file”
• Copy this war file to the auto-deploy directory for most application servers (like Tomcat, Jetty or Glassfish)
Wednesday, May 9, 2012
war format for doj|-- WEB-INF
| |-- lib | `-- lib-python | |-- Lib | |-- django | |-- doj | `-- mysite `-- media
Wednesday, May 9, 2012
Pyramid on Jython
• jython ez_install pyramid
• Note that you should not try to use Chameleon with Jython - use Mako for templates.
Wednesday, May 9, 2012
Creating an executable Jar
• Make a copy of jython.jar
• copy Lib/ into jython.jar
• copy a __run__.py file into new jar
• in the future it will be a __main__.py to fit with the new CPython convention as of 2.6/3.0
Wednesday, May 9, 2012
Add Jython install stuff to a new jar
$ cd $JYTHON_HOME$ cp jython.jar app.jar$ zip -r app.jar Lib
Wednesday, May 9, 2012
Add modules and paths to the jar file
$ cd $MY_APP_DIRECTORY$ zip app.jar *.py# Add path to additional jar file.$ jar ufm myapp.jar othermanifest.mf#Where, othermanifest.mf contains the following:Class-Path: ./otherjar.jar
Wednesday, May 9, 2012
Add runner file, and run!
$ zip myapp.jar __run__.py#in 2.7/3.x$ zip myapp.jar __main__.py$ java org.python.util.jython -jar app.jar
Wednesday, May 9, 2012
Jython 3000
• We already have a Jython 3 branch
• It has a nearly complete parser
• Not much else yet
• Can’t wait to delete old style classes and fake str suppot!
• I’d like to target 3.3 and support only JDK7 or above (maybe even JDK8 if it takes long)
Wednesday, May 9, 2012
Jython 3 changes?
• All of the same changes as Python3
• unicode for all strings
• no old style classes
• new metaclass syntax
• many iterators instead of lists
• much more
Wednesday, May 9, 2012
Jython specific changes
• Better way to call into Jython? Project Lambda maybe?
• Default to not scan packages?
• Get rid of swing specific helper functions
• Remove Lib gunk
Wednesday, May 9, 2012
Jython/CPython collaboration
• Break out the CPython standard Lib into a shared Lib - merge all of Jython’s customizations into CPython Lib
• Share CPython’s 3.3 import implementation
• PEP 420 implicit namespace packages for Java packages
Wednesday, May 9, 2012
Where to Find Out More
• http://www.jython.org
• http://wiki.python.org/jython
• http://fwierzbicki.blogspot.com
• Twitter: fwierzbicki
Wednesday, May 9, 2012