Monday, January 02, 2012

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/

Tuesday, November 22, 2011

Contributing to Jython

About a year and a half ago my dream job of doing nothing but Jython all day and night came to an end (By the way, does anyone want to pay me to do Jython all day and night? It's about the only thing that could pull me from my awesome job at Canonical which I'm overdue on writing about here). Anyway to re-integrate myself into society I had to go cold turkey on Jython for a while so I could learn how to have a regular job again. I've contributed to Jython here and there by coding some of this and that, but I've failed to take care of the most important part: helping new people that want to get involved in Jython. I've let that go on for too long and I need to turn things around and get back to doing that. Recently a frustrated patch author sent an email about how hard it is to become a Jython contributor. He has some patches that have been sitting around for a long time and I'm pretty ashamed that that is the normal course of things lately. So, as I start giving Jython a bit more of my spare time again, I plan to make it a priority to review patches and try to figure out how to grow the Jython developer community again. So send patches and I promise to look at them. In particular, if anyone wants to put together patches that fix failing tests in the default Jython branch that targets 2.6 compatibility, I'll be right on them. I'll put together another post on contributing to Jython soon.

Friday, February 18, 2011

This Weblog is Now Licensed Under a Creative Commons License

I've been meaning to add a creative commons license to this site for a long time. Since the default copyright laws are not conducive to any sort of digital sharing, I think anyone that wants to share their work should take the time to find a license that makes sense for them. These days, with the creative commons form, it only takes a few minutes to do. You just pick the rights you want to grant and the chooser gives you an html snippet that you can drop onto your site or weblog or whatever. I encourage anyone that wants their words or work to be shareable to have a look at the great work that the creative commons folks have done.

Tuesday, September 14, 2010

Jython 2.5.2 beta 2 is released!

The title says it all! See the details at Jim Baker's blog. There is a reasonable chance that this will become the 2.5.2 final release and that we can start work on the next 2.x version of Jython.

Tuesday, August 24, 2010

Leaving Sauce Labs

My time at Sauce Labs is at an end, and so I am looking for a new opportunity. I had a great time at Sauce Labs where I worked on building a cloud based infrastructure for test automation with Selenium. I have been working in software for more than twelve years, often in a lead role. I am well regarded in the Open Source world, where I have participated in and helped build communities. I have been the Jython project lead for six years. I am a committer on the Python project, and a member of the Python Software Foundation. I have done a wide variety of work in recent history: development and leadership work in Java and Python, from parsing Python source and compiling to Java bytecodes, to coding and administrating distributed cloud based web applications. I am able to do work in any of these areas, on either a full time or consulting/contracting basis. My contact information can be found on my Google profile. My LinkedIn profile is a good summary of my credentials.

Wednesday, February 03, 2010

My New Job at Sauce Labs

I joined Sauce Labs as of January 3 this year. I met the people at Sauce Labs during the recent startup crawl in San Francisco (what a great way to find an employer, eh?). Sauce Labs is based around support for the open source Selenium functional testing framework. The inventor of Selenium, Jason Huggins is a founder. I joined Sauce Labs at the same time as Jim Baker and Raymond Hettinger, see here for more. The entire team at Sauce Labs is mind blowingly amazing - which is the main reason that I had to join.

As for the technology, there is a very cool Python backend that supports lots and lots of parallel virtual workloads. I am currently working on Sauce IDE, an extension of Selenium IDE that adds support for Sauce Lab's OnDemand service to the IDE. This lets you record your functional tests from Firefox, and then push those tests up to Sauce Labs where they can be run on various operating systems and browsers in parallel. How cool is that?

Readers of this blog are probably wondering what this means for my role as lead maintainer of Jython. First of all, this is nothing like my former role at Sun Microsystems. Jython is definitely not going to be my day job the way it was at Sun. Since Sauce Labs is an early stage startup, I will be very busy helping the company succeed. Having said that, Sauce Labs is a company based on the use of open source software, and has a strong commitment to giving back to the open source community. They support my continued involvement in the development of Jython. What this means in practice will need to evolve over time.

I'm pretty excited to switch focus from implementing a Python to putting Python code into production. I fully expect that this new job will deepen my understanding of real world Python coding and make my Jython work more productive.

Wednesday, November 04, 2009

Leaving Sun

I've always regarded working full time on Jython at Sun as a miraculous sabbatical that might come to an end at any moment. Sadly that time has come. I've worked with many amazing people and had a great time: Sun provided me with one of the greatest professional experiences I have had, for which I will always be grateful.

I'm very pleased with how far Jython has come during my tenure at Sun. Jython is now a modern version of Python, and has the momentum to continue its growth. A far larger group of developers than ever before contribute regularly, making Jython a very healthy project. Jython runs many more of the key frameworks and applications that are popular in the Python world. In the future we will be making Jython better, faster, and more complete. I started working on Jython long before I joined Sun, and I certainly plan be a part of Jython's future.

I am looking for a new opportunity, and am open to many possibilities. I have been working in software for more than twelve years, often in a lead role. I am well regarded in the Open Source world, where I have participated in and helped build communities. I have been the Jython project lead for almost five years. I am a committer on the Python project, and a member of the Python Software Foundation. I have done a wide variety of work in recent history: development and leadership work in Java and Python, from the abstract level of parsing Python source and compiling to Java bytecodes, to the more concrete work of web development. I am able to do work in any of these areas, on either a full time or consulting/contracting basis. My contact information can be found on my Google profile and my LinkedIn profile is a good summary of my credentials.