|
|
Log in / Subscribe / Register

Jython Native Interface (JyNI)

By Jake Edge
April 22, 2015

Python Language Summit

In his session at the 2015 Python Language Summit, Stefan Richthofer described a project he has been working on for the last two years to enable Jython (Python on the Java virtual machine) to use Python's C extensions. It is called Jython Native Interface (JyNI) and it provides an adaption layer between Jython, which, like Java, uses the Java Native Interface (JNI) to access native code, and Python's C API.

Jython reimplements various built-in Python types (e.g. PyIntObject, PyStringObject, ...) in Java. The idea behind JyNI is to create a C-compatible view into these objects that stays in sync with them. The C API is "large and complex", which is one obstacle that needs to be surmounted. In addition, CPython uses macros in some places to access data objects; those macros cannot be switched out at runtime by dynamically loaded functions.

[Stefan Richthofer]

There are also a few minor obstacles to JyNI, including exception handling and the global interpreter lock (GIL), but the biggest problem area is garbage collection. Making it all work without leaking memory is challenging, he said.

There are three approaches that are being used to unify objects between Java and Python (i.e. to allow access as either a PyObject or a jobject). For Python object types that have Java versions available, a PyObject wrapper for the jobject is used. If there is no object of that type available in Java, the reverse is done (a jobject wraps a PyObject). The third option is used when macros are used to access the object in Python. It is a data structure that mirrors the object state between a PyObject and a jobject version. JyNI tries to avoid mirroring but it cannot be avoided for some kinds of objects.

CPython provides garbage collection for extensions, so some extensions may be relying on that for their memory management. Java does not provide any garbage collection to native code, so extensions must manage their own memory. Resolving that will, seemingly, be the biggest challenge for JyNI going forward.


Index entries for this article
ConferencePython Language Summit/2015


to post comments

Jython Native Interface (JyNI)

Posted Apr 23, 2015 11:20 UTC (Thu) by intgr (subscriber, #39733) [Link] (1 responses)

Is anyone actually using Jython for serious work?
I hear its performance isn't any better than CPython; why would anyone use Jython?

(I guess it's a bit of a chicken-and-egg problem and JyNI will probably help with that)

Jython Native Interface (JyNI)

Posted Apr 30, 2015 15:17 UTC (Thu) by ssokolow (guest, #94568) [Link]

My understanding was that Jython is mostly used as an alternative to something in-house and XML-based for providing an embedded control language in Java apps, similar to using Lua, Python, or Squirrel for the logic-layer language in a C++ game engine.


Copyright © 2015, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds