24

I'm considering moving my code (around 30K LOC) from CPython to Jython, so that I could have better integration with my java code.

Is there a checklist or a guide I should look at, to help my with the migration? Does anyone have experience with doing something similar?

From reading the Jython site, most of the problems seem too obscure to bother me.

I did notice that:

  • thread safety is an issue
  • Unicode support seems to be quite different, which may be a problem for me
  • mysqldb doesn't work and needs to be replaced with zxJDBC

Anything else?

Related question: What are some strategies to write python code that works in CPython, Jython and IronPython

Community
  • 1
  • 1
itsadok
  • 28,822
  • 30
  • 126
  • 171

6 Answers6

10

First off, I have to say the Jython implementation is very good. Most things "just work".

Here are a few things that I have encountered:

  • C modules are not available, of course.

  • open('file').read() doesn't automatically close the file. This has to do with the difference in the garbage collector. This can cause issues with too many open files. It's better to use the "with open('file') as fp" idiom.

  • Setting the current working directory (using os.setcwd()) works for Python code, but not for Java code. It emulates the current working directory for everything file-related but can only do so for Jython.

  • XML parsing will try to validate an external DTD if it's available. This can cause massive slowdowns in XML handling code because the parser will download the DTD over the network. I reported this issue, but so far it remains unfixed.

  • The __ del __ method is invoked very late in Jython code, not immediately after the last reference to the object is deleted.

There is an old list of differences, but a recent list is not available.

Frederik
  • 14,156
  • 10
  • 45
  • 53
  • 5
    About the `__del__` method: the spec actually doesn't define when exactly the method should be invoked, so you should not be relying on it in your code at all. (I believe there is a notice about this on the Python documentation somewhere.) – Sasha Chedygov Apr 05 '10 at 20:41
8

So far, I have noticed two further issues:

  • String interning 'a' is 'a' is not guaranteed (and it is just an implementation fluke on CPython). This could be a serious problem, and really was in one of the libraries I was porting (Jinja2). Unit tests are (as always) your best friends!
Jython 2.5b0 (trunk:5540, Oct 31 2008, 13:55:41)
>>> 'a' is 'a'
True
>>> s = 'a'
>>> 'a' is s
False
>>> 'a' == s   
True
>>> intern('a') is intern(s)
True

Here is the same session on CPython:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
>>> 'a' is 'a'
True
>>> s = 'a'
>>> 'a' is s
True
>>> 'a' == s
True
>>> intern('a') is intern(s)
True

  • os.spawn* functions are not implemented. Instead use subprocess.call. I was surprised really, as the implementation using subprocess.call would be easy, and I am sure they will accept patches.

(I have been doing a similar thing as you, porting an app recently)

Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
  • 4
    As I know "is" must be used just to check if objects are stored in the same place in the memory. Using "is" instead of "==" is bad habit. – Alex Bolotov Apr 15 '09 at 21:13
  • oh, yes, you are right about that. But don't forget about "is None"/"is not None", it seems a bit different from the "is-same-object" case you are describing, it is more like "is-null" (I think). – shylent Oct 17 '09 at 18:13
  • @shylent: "None" (i.e. null in other languages) is a singleton (source: http://docs.python.org/c-api/none.html) – Hut8 Dec 16 '10 at 21:16
5

I'm starting this as a wiki collected from the other answers and my experience. Feel free to edit and add stuff, but please try to stick to practical advice rather than a list of broken things. Here's an old list of differences from the Jython site.

Resource management

Jython does not use reference counting, and so resources are released as they are garbage collected, which is much later then you'd see in the equivalent CPython program

  • open('file').read() doesn't automatically close the file. Better use the with open('file') as fp idiom.
  • The __ del __ method is invoked very late in Jython code, not immediately after the last reference to the object is deleted.

MySQL Integration

mysqldb is a c module, and therefore will not work in jython. Instead, you should use com.ziclix.python.sql.zxJDBC, which comes bundled with Jython.

Replace the following MySQLdb code:

connection = MySQLdb.connect(host, user, passwd, db, use_unicode=True, chatset='utf8')

With:

url = "jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" % (host, db)
connections = zxJDBC.connect(url, user, passwd, "com.mysql.jdbc.Driver")

You'll also need to replace all _mysql_exception with zxJDBC.

Finally, you'll need to replace the query placeholders from %s to ?.

Unicode

  • You can't express illegal unicode characters in Jython. Trying something like unichr(0xd800) would cause an exception, and having a literal u'\ud800' in your code will just wreak havoc.

Missing things

  • C modules are not available, of course.
  • os.spawn* functions are not implemented. Instead use subprocess.call.

Performance

  • For most workloads, Jython will be much slower than CPython. Reports are anything between 3 to 50 times slower.

Community

The Jython project is still alive, but is not fast-moving. The dev mailing list has about 20 messages a month, and there seem to be only about 2 developers commiting code lately.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
itsadok
  • 28,822
  • 30
  • 126
  • 171
3

You might also want to research JPype. I'm not sure how mature it is compared to Jython, but it should allow CPython to access Java code.

Cerin
  • 60,957
  • 96
  • 316
  • 522
3

When I switched a project from CPython to Jython some time ago I realized a speed-down of up to 50x for time-critical sections. Because of that I stayed with CPython.

However, that might have changed now with the current versions.

wr.
  • 2,841
  • 1
  • 23
  • 27
  • 1
    speaking in 2015 I'd say this is still a problem. Both Jython's execution and launch are painful. But against that you have to set the brilliance of the Jython idea. There are (obvious) strategies: write time-critical stuff directly in Java, use CPython for initial running and testing new bits not involving any Java classes. The holy grail is presumably hassle-free CPython-Jython integration... – mike rodent May 22 '15 at 05:45
2

Recently, I worked on a project for a professor at my school with a group. Early on, it was decided that we would write the project in Python. We definitely should have used CPython. We wrote the program in Python and all of our unit tests eventually worked. Because most people already have Java installed on their computers, and not Python, we decided to just deploy it as a Jython jar. We therefore wrote the GUI with Swing, because that's included in Java's standard library.

The first time I ran the program with Jython, it crashed immediately. For one, csv.reader's ".fieldnames" always seemed to be None. Therefore I had to change several parts of our code to work around this.

A different section of my code crashed as well, which worked fine with CPython. Jython accused me of referencing a variable before it was assigned anything (which drove me nuts and really wasn't the case). This is one example: ActiveState's Code Recipe's external sort

Worse yet, the performance was awful. Basically this code combined several CSV files, one of which was about 2 GB. In CPython, it ran in 8.5 minutes. In Jython, it ran in 25 minutes.

These problems happened with 2.5.2rc2 (the latest at the time of writing this post).

Hut8
  • 6,080
  • 4
  • 42
  • 59