Skip to main content
QUICK REVIEW

[Paper Review] JyNI - Using native CPython-Extensions in Jython

Stefan Richthofer|arXiv (Cornell University)|Apr 25, 2014
Computational Physics and Python ApplicationsComputer Science5 citations
TL;DR

JyNI enables Jython to natively load and execute CPython extensions like NumPy and SciPy without recompilation or forking by implementing a compatibility layer that bridges the Python C-API to Jython’s JVM runtime. It allows seamless integration of existing CPython extensions into Jython, unlocking scientific computing workloads on the Java platform.

ABSTRACT

Jython is a Java based Python implementation and the most seamless way to integrate Python and Java. However, it does not support native extensions written for CPython like NumPy or SciPy. Since most scientific Python code fundamentally depends on exactly such native extensions directly or indirectly, it usually cannot be run with Jython. JyNI (Jython Native Interface) aims to close this gap. It is a layer that enables Jython users to load native CPython extensions and access them from Jython the same way as they would do in CPython. In order to leverage the JyNI functionality, you just have to put it on the Java classpath when Jython is launched. It neither requires you to recompile the extension code, nor to build a customized Jython fork. That means, it is binary compatible with existing extension builds. At the time of writing, JyNI does not fully implement the Python C-API and it is only capable of loading simple examples that only involve most basic built-in types. The concept is rather complete though and our goal is to provide the C-API needed to load NumPy as soon as possible. After that we will focus on SciPy and others. We expect that our work will also enable Java developers to use CPython extensions like NumPy in their Java code.

Motivation & Objective

  • Enable Jython, a Java-based Python implementation, to natively load and execute CPython extensions such as NumPy and SciPy.
  • Overcome the limitation that Jython cannot load binary CPython extensions due to C-API incompatibility.
  • Provide a binary-compatible solution that works with existing CPython extension builds without requiring code changes or custom Jython forks.
  • Lay the foundation for scientific Python workloads to run in industrial Java environments by enabling access to core scientific libraries.
  • Achieve full compatibility with the Python C-API for essential extensions, starting with NumPy and its dependencies.

Proposed method

  • Implement a compatibility layer (JyNI) that maps CPython C-API calls to equivalent operations in Jython’s JVM-based runtime.
  • Use Jython’s hooking mechanism to inject JyNI into the classpath at runtime, enabling transparent extension loading.
  • Mirror CPython objects and stubs in Java, maintaining reference connectivity through JyGCHead objects to enable Java garbage collection of native objects.
  • Leverage Java’s reference handling (e.g., PhantomReference with ReferenceQueue) to manage cleanup of native objects without relying on finalizers.
  • Support dynamic loading of native libraries via the classpath or system library path, enabling automatic discovery of libJyNI.so and libJyNI-loader.so.
  • Implement selective C-API support, starting with basic types and critical modules like datetime and ctypes, to enable dependency chains for NumPy.

Experimental results

Research questions

  • RQ1Can a compatibility layer be built to enable Jython to load and execute CPython binary extensions without modifying the extensions or Jython itself?
  • RQ2How can the Python C-API be emulated in a JVM environment to support complex scientific libraries like NumPy?
  • RQ3What mechanisms can ensure correct memory management and garbage collection for mirrored CPython objects in a mixed Java/Python runtime?
  • RQ4How can reference cycles involving both Jython and native CPython objects be detected and cleaned up to prevent memory leaks?
  • RQ5To what extent can existing CPython extension binaries be reused in Jython with minimal changes?

Key findings

  • JyNI successfully enables Jython to load and execute the native CPython datetime module, as evidenced by the correct "Fast implementation of the datetime type." docstring and proper object behavior.
  • The solution is binary-compatible with existing CPython extension builds, requiring no recompilation or forking of Jython or extensions.
  • JyNI supports loading extensions via standard classpath injection, with no changes to the launch command beyond adding JyNI.jar and native libraries to the classpath.
  • The architecture uses JyGCHead objects and PhantomReference-based cleanup to integrate native object lifecycle management with Java’s garbage collector.
  • JyNI is currently limited to Linux, but the design supports future cross-platform expansion once core compatibility is achieved.
  • The approach enables the potential for Java developers to use CPython extensions like NumPy directly in Java applications through Jython.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.