Skip to content

Sub-interpreter shared modules appear to be broken in Python 3.12 #645

Description

@ndjensen

Shared modules appear to be broken in Python 3.12. It is presumed they are broken in later releases of Python too.

Here is some sample code that illustrates the problem about 50% of the time. It is inconsistent, sometimes it works, sometimes it doesn't.

import jep.Jep;
import jep.JepConfig;
import jep.NDArray;
import jep.SubInterpreter;

public class TestHgt2Pres {

    public static void main(String[] args) {
        float[] floats = new float[300_000];
        try (Jep j = new SubInterpreter(
                new JepConfig().addSharedModules("numpy"))) {
            j.set("testarg", new NDArray<>(floats, 300_000));
            j.exec("import numpy");
            j.exec("numpy.piecewise(testarg, [testarg < 5, testarg >= 5], [lambda z: z + 1, lambda z: z - 1])");
            j.exec("print('finished')");
        }
    }
}

The error that is produced by this example is:

Exception in thread "main" jep.JepException: <class 'TypeError'>: _abc_impl is set to a wrong type
	at <frozen abc>.__subclasscheck__(<frozen abc>:123)
	at <frozen abc>.__instancecheck__(<frozen abc>:119)
	at /home/njensen/tmp/python3.12-sub-interpreters/jep_venv/lib64/python3.12/site-packages/numpy/core/numeric.isscalar(numeric.py:1933)
	at /home/njensen/tmp/python3.12-sub-interpreters/jep_venv/lib64/python3.12/site-packages/numpy/lib/function_base.piecewise(function_base.py:734)
	at <string>.<module>(<string>:1)
	at jep.Jep.exec(Native Method)
	at jep.Jep.exec(Jep.java:384)
	at TestHgt2Pres.main(TestHgt2Pres.java:23)

The same code runs fine if you switch it to a shared interpreter. The code also works fine on Python 3.11. We have seen other unexplainable errors but this one is the most reproducible one we have narrowed down so far.

Based on the error message also showing up on this ticket, python/cpython#134591, and the ensuing discussion there, I think that CPython changed sub-interpreter internals that shared modules was relying on.

  • OS Platform, Distribution, and Version: RHEL 8
  • Python Distribution and Version: 3.12.13
  • Java Distribution and Version: OpenJDK 17 and 21
  • Jep Version: 4.3.1
  • Python packages used (e.g. numpy, pandas, tensorflow): numpy

I tested with numpy 1.26.4 and numpy 2.4.6 and it made no difference.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions