views:

1083

answers:

4

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

+4  A: 

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 A
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.
Oleksandr Bolotov
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
+1  A: 

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
+3  A: 

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
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.)
musicfreak
+1  A: 

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.

Chris S