skip to navigation
skip to content

Planet Jython

Last update: January 27, 2012 06:47 PM

January 02, 2012


Frank Wierzbicki

Jython dev notes part I: The Jython Exposer

One of my new years resolutions is to make Jython more friendly to new developers. One way to do that is to write up some notes on bits of Jython that are particularly mysterious to newcomers. I've boldly titled this post "Jython dev notes part I" to push myself to create more than one of these :)

It should be noted that I'm not shying away from making these notes highly technical - but I'm happy to edit them to make them more manageable later. Hopefully if I write enough of these up they can make up the beginnings of an advanced dev guide for Jython.

Recently I was asked how the Jython exposer works. The Jython exposer is the code that exposes the types that are written in Java as Python types in the Jython interpreter. It does this by rewriting the .class files during build time and adding the special methods that are seen at runtime. The augmented .class files end up in the build/exposed/ directory, and are ultimately built into the jython.jar distribution file. You can run the exposer from the Jython source like so:

ant expose


The exposer reads through each of the classes listed in CoreExposed.include and rewrites each of them. The rewritten classes get new methods generated for the Jython runtime to use as Python classes and methods. The exposer finds the code that it needs to operate on by finding Java annotations that have been defined for this purpose. An example is the easiest way to explain:

The string type in Python is called "str" and is implemented in Jython by src/org/python/core/PyString.java -- at the top of PyString.java is:


@ExposedType(name = "str", doc = BuiltinDocs.str_doc)


Which tells the exposer to expose PyString.java as the "str" type and take its "__doc__" attribute from BuiltinDocs.str_doc. Note that there currently appears to be a bug that str.__doc__ ends up as None right now, but it will show up if you run:


>>> help(str)


Further down in the source of PyString.java:


@ExposedNew
static PyObject str_new(PyNewWrapper new_, boolean init, PyType subtype, ...


Exposes the str_new Java method as the Python str __new__ method. (The __new__ method is a special class level method that acts as a str factory).

Next we'll look at some examples of ExposedMethod, which is the most common annotation that the exposer uses. ExposedMethod exposes general methods for Python classes.


@ExposedMethod(doc = BuiltinDocs.str___len___doc)
final int str___len__() {


The above code exposes the __len__ method, which is how the str method will respond to a call to the builtin len() function. Since the object itself is the only argument, the ExposedMethod annotation only needs a doc.


@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.str___eq___doc)
final PyObject str___eq__(PyObject other) {


The above exposes the __eq__ method. MethodType.Binary indicates that this is a binary method that will take 2 arguments. The exposer has special handling for common types like this.


@ExposedMethod(defaults = {"null", "-1"}, doc = BuiltinDocs.str_split_doc)
final PyList str_split(String sep, int maxsplit) {


The above implements the split method of str, which takes 2 optional arguments. the "defaults" attribute assigns default values to the optional arguments.

There is much more to the exposer, but unfortunately the code is the only real documentation other than this post at this time. The source for the exposer engine is in:

src/org/python/expose/

And the source for the generator and ant task are here:

src/org/python/expose/generate/

January 02, 2012 05:28 PM

December 29, 2011


Josh Juneau

Java 7 Recipes: A Problem-Solution Approach

This is a quick post to mention a new book that will be published soon entitled Java 7 Recipes. I had the pleasure of leading a team of excellent authors: Carl Dea, Mark Beaty, Freddy Guime, and John O' Conner in the authoring of this book. It features a problem-solution approach demonstrating how to get up and running quickly with the Java language. The book features some of the most common questions for newbies starting to develop with Java, and provides answers in an easy-to-learn and reuse manner. It also features some of the most common intermediate and advanced problems and solutions, as well as material that is new to the Java 7 release.

Carl Dea wrote several chapters on JavaFX 2.0, and the material covers enough information to get started with JavaFX 2.0 and then delves into some advanced examples as well. If you are looking to learn the latest in Java desktop and rich internet client development, you will want to check out the chapters on JavaFX 2.0.

Mark Beaty wrote a chapter covering data structures and iteration. He is an expert in object oriented Java concepts, and this chapter covers some of the most fundamental parts of the Java language. Mark also was a technical reviewer for the book, and he did an excellent job of working through the material and solidifying it. I would also like to give a shout out to David Coffin who also performed technical review on several of the chapters...great work!

Freddy Guime is a Java expert who wrote chapters on input/output, exceptions, concurrency, unit testing, and more. Freddy has presented at JavaOne several times and his material exploits his knowledge of the Java language.

John O' Conner has been an avid Java developer for years, and is seasoned in the language. He began his career with Sun Microsystems and helped to develop the internationalization and Unicode support libraries of the core Java SE platform. He authored chapters on those topics for the book, and more.

I authored chapters varying from Strings, object orientation, numbers and dates, database development, and more. We even threw in a chapter on Android so that interested Java developers could take a look at how to develop applications for the Android platform.

Check this book out soon, or pre-order now on Amazon the Apress site. This is a must-have for any Java developer!

I am currently working on my latest Apress book entitled Java EE 7 Recipes...it should be published late next year...stay tuned for more updates on that book!

December 29, 2011 08:01 AM

December 16, 2011


James Abley

svn merging on OSX

I don't always use svn as a version control system with which I'll need to merge branches, but when I do, I use fmdiff.

$ brew install fmdiff

One minor annoyance - fmmerge (used for interactive conflict resolution) doesn't work. The number of arguments passed to the script has changed since it was first written. I patched it locally, but it still didn't work. FileMerge was launched, I could edit files, etc; but it kept saying that the merge needed resolving. Instead, I just postpone all merge conflicts during the merge, and then use fmresolve and svn resolve to resolve any individual merge conflicts.

[1] I like to branch by feature typically, but occasionally, branch by VCS is used.

December 16, 2011 02:20 PM