Python – Jython
As we all know, Python has various implementation, there’s the mother of all C implementation CPython or just Python, there’s the Java version Jython, and there’s the .NET version IronPython. I have never worked with any other implementation other than C, until today that is.
Due to some existing environment, I have to convert my CPython codes to into Jython. I was thinking… both are still python, how hard can it be? Well… not that difficult, but do expect the unexpected. They may have same syntax, and look the same from the outside, but do note that they can be truly different in the implementation, which can lead to different behavior. The annoying thing is… these differences are not always mentioned, leaving you scratching your head figuring out why your Python code does not run on Jython.
Jython is certainly nothing new, so the following thoughts might came a little too late. But you know what they say, better be late than never.
The Good:
- Allows you to use many wonderful Java classes and packages
The Bad:
- CPython has tons of extensions that are written in C, including one of my favorites, Numpy. Well… Jython can’t use that, not now at least. Who knows if in the future somehow compiled C code can be transformed to java byte-codes.
- Every time you fire up your py with Jython, a JVM is also fired up to run your pycode. Starting JVM is slooooww… even on my i7.
- emacs does not support jython interpreter out of the box. You have to do some configuration that until now, I have yet to figure it out.
- Jython implementation is *usually* 2 version lagging behind Python’s. As I’m writing this, the production version for 2.x is 2.7 for CPython, and 2.5 for Jython
It seems like there’s more baddies than goodies when things are viewed from my glasses. But I must say, the single good thing could easily trump all the baddies I listed here.
Here’s some problem I encountered when converting my code. Hopefully those on a similar path as me does not have to suffer that much because of it.
- httplib.HTTPSConnection doesn’t work for self-signed websites.
In CPython, calling this method does not involve checking the validity of the certificate, and it is clearly stated in the manual. Jython displays the same manual, and saying it is also ignored. But the truth is, it’s not ignored. The certificate will be checked against it’s keystore located at JAVA_HOME/jre/lib/security/cacertsUnfortunately, there is no option for you to ignore it. So you have two option: 1. Ditch httplib and go with another library, or 2. add the self-signed certificate to your keystore manualy. I went with the first option, by using java package called htmlunit. I’ts just not practical to manually dozens of certificate into every machine I want to work with.
- xml.sax.handler.ContentHandler
This class behaves differently in CPython and Jython. If you have:
def startElement(self, name, attrs):
self.result.append(attrs)
you might need to modify the code to:
def startElement(self, name, attrs):
self.result.append(dict(attrs.items()))
This would make sure that you break any object reference that are still tied to attrs.
Anyway, those are just some examples. Moral of the story is: do watch out for these hidden ‘features’. While they are similar, they are not the same.
Filed under: Uncategorized | Leave a Comment
Tags: htmlunit, httplib, httpsconnection, jython, python
No Responses Yet to “Python – Jython”