Friday 12:15 p.m.–12:30 p.m.

JyNI - Prepare Jython for scientific code

Stefan Richthofer

Audience level:


Jython is the most seamless Java/Python integration that currently exists. Its main drawback is the missing support for CPython's C-extension API - a fact that prevents most scientific code from running. While the C-API is hard to implement/emulate for various reasons, JyNI (Jython Native Interface) explores solutions for each of them and demonstrates a workable approach.



Jython is a Java-based Python implementation and the most seamless way to integrate Python and Java. It achieves high efficiency by compiling Python-code to Java byte-code and thus letting Java's JIT optimize it - an approach that enables Python code to call Java functions or to subclass Java classes. It enables Python-code to leverage Java's multithreading-features and utilizes Java's built-in garbage collection. However, it currently does not support CPython's C-API and thus does not support native extensions like NumPy and Scipy. Since most scientific code depends on such extensions, it is usually not runnable with Jython.


For various reasons, implementing CPython's C-API is not an easy task (cf. [1]). Just to name a few issues - it offers macros to access CPython internals, uses a global interpreter lock in contrast to Jython and lets extensions perform reference-counting-based garbage collection, which is incompatible to Java's gc-approach. For each of the arising issues, JyNI proposes a feasible solution; most remarkable it emulates CPython's reference counting garbage collection on top of Java's mark and sweep-based approach (taking care of adjacent concepts like finalizers and weak references and their interference with Jython). (Note that there are vague considerations around to switch to mark-and-sweep-based gc in a future CPython too, cf. [2]. So this algorithm might one day be even relevant to CPython in terms of running legacy modules.) All this is designed to be binary compatible with existing extension-builds so that Jython can import the original C-extensions (i.e. the same .dll- or .so-file that CPython would use).


The talk will sketch the mechanisms that CPython and Jython use for extensions and explains how JyNI bridges them. So as a side-effect it will provide entrance points for listeners who are interested to get into writing extensions. Then it will focus on aspects that are interesting from a computer-science point of view or are of relevance for other equivalent projects. The corresponding approaches will be outlined in an abstract fashion. Most remarkable is how reference-counting garbage collection (i.e. the approach in CPython) is emulated on top of mark-and-sweep-based garbage collection (the Java, PyPy, Jython and IronPython approach). So there will be a specific section about this. Finally a live usage demonstration will be given.


[1] Romain Guillebert: PyMetabiosis, Python Language Summit 2015, PyCon 2015,, URL:, web 2015-05-15

[2] Larry Hastings: Making Python 3 more attractive, Python Language Summit 2015, PyCon 2015,, URL:, web 2015-05-15