cdef extern from "cythonjni.h" : ctypedef struct RET_jlong : pass ctypedef struct RET_jint : pass ctypedef struct RET_void : pass RET_jlong long_toRET(long) RET_jint int_toRET(int) cdef extern from "jni.h" : ctypedef struct JNIEnv : pass ctypedef struct jlong : pass ctypedef struct jint : pass ctypedef struct jclass : pass ctypedef struct jobject : pass ctypedef struct JavaVM : pass RET_jint JNI_VERSION_1_2 cdef extern from "Python.h" : void Py_Initialize() cdef extern void init_hello () cdef public RET_jint JNI_OnLoad (JavaVM* jvm, void* reserved) : """Function called by the JVM just after _hello.so is loaded. It initializes the Python runtime as well as module _hello. """ Py_Initialize() init_hello() ## uncomment this line to observe ImportError on missing _Py_ZeroStruct #import hello return JNI_VERSION_1_2 import hello _instances = {} cdef long last = 0 cdef public RET_jlong Java_Hello_pyinit (JNIEnv * env, jclass cls, jint base) : """Wrapper for hello.Hello.__init__. Each created instance is assigned a number that will be used to reference the object from the Java side. This number is maintained as long value 'last' above, instances are kept in dict '_instances' above. """ global last cdef long ident = last last += 1 _instances[ident] = hello.Hello(base) return long_toRET(ident) cdef public RET_jlong Java_Hello_pydel (JNIEnv * env, jobject obj, jlong ident) : """Function called when the Java object is garbage collected in order to delete the corresponding Python object. """ del _instances[ident] #print "*** GC removed object #%s" % (ident) return long_toRET(ident) cdef public RET_jint Java_Hello_pyadd (JNIEnv * env, jobject obj, jlong ident, jint nbr) : """Wrapper for method hello.Hello.add """ cdef int ret ret = _instances[ident].add(nbr) return int_toRET(ret)