Skip to content

Commit

Permalink
Merge pull request #802 from Thrameos/backport
Browse files Browse the repository at this point in the history
Backport from other branches
  • Loading branch information
marscher committed Aug 7, 2020
2 parents 4a48459 + d45baf5 commit 3f0a518
Show file tree
Hide file tree
Showing 70 changed files with 946 additions and 986 deletions.
8 changes: 7 additions & 1 deletion jpype/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def startJVM(*args, **kwargs):
try:
_jpype.startup(jvmpath, tuple(args),
ignoreUnrecognized, convertStrings)
_JVM_started = True
initializeResources()
except RuntimeError as ex:
source = str(ex)
if "UnsupportedClassVersion" in source:
Expand All @@ -222,6 +222,9 @@ def startJVM(*args, **kwargs):
jvmpath, version)) from ex
raise


def initializeResources():
global _JVM_started
_jpype._java_lang_Class = None
_jpype._java_lang_Object = _jpype.JClass("java.lang.Object")
_jpype._java_lang_Throwable = _jpype.JClass("java.lang.Throwable")
Expand Down Expand Up @@ -299,6 +302,9 @@ def startJVM(*args, **kwargs):
_jpype.JClass('org.jpype.JPypeKeywords').setKeywords(
list(_pykeywords._KEYWORDS))

# Everything successed so started is now true.
_JVM_started = True


def shutdownJVM():
""" Shuts down the JVM.
Expand Down
10 changes: 2 additions & 8 deletions jpype/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ class that can produce reducers as needed.
"""

def __init__(self, dispatch):
cl = _jpype.JClass(
'org.jpype.classloader.JPypeClassLoader').getInstance()
self._encoder = _jpype.JClass(
cl.loadClass('org.jpype.pickle.Encoder'))()
self._encoder = _jpype.JClass('org.jpype.pickle.Encoder')()
self._builder = JUnserializer()
self._dispatch = dispatch

Expand Down Expand Up @@ -152,10 +149,7 @@ class does not match, usually as a result of a new jar
"""

def __init__(self, file, *args, **kwargs):
cl = _jpype.JClass(
'org.jpype.classloader.JPypeClassLoader').getInstance()
self._decoder = _jpype.JClass(
cl.loadClass('org.jpype.pickle.Decoder'))()
self._decoder = _jpype.JClass('org.jpype.pickle.Decoder')()
pickle.Unpickler.__init__(self, file, *args, **kwargs)

def find_class(self, module, cls):
Expand Down
2 changes: 1 addition & 1 deletion native/common/include/jp_arrayclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class JPArrayClass : public JPClass
virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual void getConversionInfo(JPConversionInfo &info) override;

JPValue newInstance(JPJavaFrame& frame, int length);
JPValue newArray(JPJavaFrame& frame, int length);

/**
* Create a new java array containing a set of items take from
Expand Down
4 changes: 2 additions & 2 deletions native/common/include/jp_booleantype.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class JPBooleanType : public JPPrimitiveType
return v.z;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Boolean;
}
Expand All @@ -54,7 +54,7 @@ class JPBooleanType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step,
PyObject *sequence) override;
Expand Down
2 changes: 1 addition & 1 deletion native/common/include/jp_bytetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class JPByteType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray, jsize start, jsize length, jsize step, PyObject*) override;
virtual JPPyObject getArrayItem(JPJavaFrame& frame, jarray, jsize ndx) override;
virtual void setArrayItem(JPJavaFrame& frame, jarray, jsize ndx, PyObject* val) override;
Expand Down
4 changes: 2 additions & 2 deletions native/common/include/jp_chartype.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JPCharType : public JPPrimitiveType
return v.c;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Character;
}
Expand All @@ -57,7 +57,7 @@ class JPCharType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray, jsize start, jsize length, jsize step, PyObject*) override;
virtual JPPyObject getArrayItem(JPJavaFrame& frame, jarray, jsize ndx) override;
virtual void setArrayItem(JPJavaFrame& frame, jarray, jsize ndx, PyObject* val) override;
Expand Down
2 changes: 1 addition & 1 deletion native/common/include/jp_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class JPClass : public JPResource
virtual void setField(JPJavaFrame& frame, jobject obj, jfieldID fid, PyObject* val);

JPClass* newArrayType(JPJavaFrame &frame, long d);
virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size);
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size);
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step, PyObject* vals);
virtual JPPyObject getArrayItem(JPJavaFrame& frame, jarray, jsize ndx);
Expand Down
3 changes: 2 additions & 1 deletion native/common/include/jp_classloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class JPClassLoader

private:
JPContext* m_Context;
JPClassRef m_ClassClass;
JPObjectRef m_SystemClassLoader;
JPObjectRef m_BootLoader;
jmethodID m_FindClass;
jmethodID m_ForNameID;
} ;

#endif // _JPCLASSLOADER_H_
18 changes: 7 additions & 11 deletions native/common/include/jp_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ class JPContext
friend class JPJavaFrame;
friend class JPypeException;
friend class JPClass;
friend class JPTypeFactory;

JPContext();
virtual ~JPContext();

// JVM control functions
bool isRunning();
void startJVM(const string& vmPath, const StringVector& args, bool ignoreUnrecognized, bool convertStrings);
void attachJVM(JNIEnv* env);
void initializeResources(JNIEnv* env);
void shutdownJVM();
void attachCurrentThread();
void attachCurrentThreadAsDaemon();
Expand Down Expand Up @@ -159,11 +160,6 @@ class JPContext
return m_ClassLoader;
}

JPReferenceQueue* getReferenceQueue()
{
return m_ReferenceQueue;
}

bool getConvertStrings() const
{
return m_ConvertStrings;
Expand Down Expand Up @@ -196,6 +192,7 @@ class JPContext
JPClass* _java_lang_reflect_Method;
JPClass* _java_lang_Throwable;
JPStringType* _java_lang_String;
JPClass* _java_nio_ByteBuffer;

private:

Expand All @@ -204,8 +201,6 @@ class JPContext
jint(JNICALL * CreateJVM_Method)(JavaVM **pvm, void **penv, void *args);
jint(JNICALL * GetCreatedJVMs_Method)(JavaVM **pvm, jsize size, jsize * nVms);

static JNIEXPORT void JNICALL onShutdown(JNIEnv *env, jobject obj, jlong contextPtr);

private:
JPContext(const JPContext& orig);

Expand All @@ -215,10 +210,8 @@ class JPContext
JPObjectRef m_JavaContext;

// Services
JPTypeFactory *m_TypeFactory;
JPTypeManager *m_TypeManager;
JPClassLoader *m_ClassLoader;
JPReferenceQueue *m_ReferenceQueue;

public:
JPClassRef m_RuntimeException;
Expand Down Expand Up @@ -269,9 +262,12 @@ class JPContext
jmethodID m_FloatValueID;
jmethodID m_DoubleValueID;

void onShutdown();

private:
bool m_Running;
bool m_ConvertStrings;
bool m_Embedded;
public:
JPGarbageCollection *m_GC;
} ;
Expand Down Expand Up @@ -328,4 +324,4 @@ JPRef<jref>& JPRef<jref>::operator=(const JPRef<jref>& other)
return *this;
}

#endif /* JP_CONTEXT_H */
#endif /* JP_CONTEXT_H */
4 changes: 2 additions & 2 deletions native/common/include/jp_doubletype.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JPDoubleType : public JPPrimitiveType
return v.d;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Double;
}
Expand All @@ -55,7 +55,7 @@ class JPDoubleType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step, PyObject*) override;
virtual JPPyObject getArrayItem(JPJavaFrame& frame, jarray, jsize ndx) override;
Expand Down
4 changes: 2 additions & 2 deletions native/common/include/jp_floattype.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JPFloatType : public JPPrimitiveType
return v.f;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Float;
}
Expand All @@ -55,7 +55,7 @@ class JPFloatType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step,
PyObject* sequence) override;
Expand Down
4 changes: 2 additions & 2 deletions native/common/include/jp_inttype.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JPIntType : public JPPrimitiveType
return v.i;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Integer;
}
Expand All @@ -55,7 +55,7 @@ class JPIntType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step,
PyObject* sequence) override;
Expand Down
3 changes: 3 additions & 0 deletions native/common/include/jp_javaframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ class JPJavaFrame
void ReleaseDoubleArrayElements(jdoubleArray, jdouble* v, jint mode);

// Object
jclass GetObjectClass(jobject obj);
jobject GetStaticObjectField(jclass clazz, jfieldID fid);
jobject GetObjectField(jobject clazz, jfieldID fid);
void SetStaticObjectField(jclass clazz, jfieldID fid, jobject val);
Expand Down Expand Up @@ -397,6 +398,8 @@ class JPJavaFrame
jarray getPackageContents(jobject pkg);

void newWrapper(JPClass* cls);
void registerRef(jobject obj, PyObject* hostRef);
void registerRef(jobject obj, void* ref, JCleanupHook cleanup);

} ;

Expand Down
4 changes: 2 additions & 2 deletions native/common/include/jp_longtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class JPLongType : public JPPrimitiveType
return v.j;
}

virtual JPClass* getBoxedClass(JPContext *context) const
virtual JPClass* getBoxedClass(JPContext *context) const override
{
return context->_java_lang_Long;
}
Expand All @@ -54,7 +54,7 @@ class JPLongType : public JPPrimitiveType
virtual JPPyObject getField(JPJavaFrame& frame, jobject c, jfieldID fid) override;
virtual void setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* val) override;

virtual jarray newArrayInstance(JPJavaFrame& frame, jsize size) override;
virtual jarray newArrayOf(JPJavaFrame& frame, jsize size) override;
virtual void setArrayRange(JPJavaFrame& frame, jarray,
jsize start, jsize length, jsize step,
PyObject *sequence) override;
Expand Down
15 changes: 10 additions & 5 deletions native/common/include/jp_primitive_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class JPPrimitiveArrayAccessor
}
}

jsize size()
{
return _frame.GetArrayLength(_array);
}

ptr_t get()
{
return _elem;
Expand Down Expand Up @@ -98,11 +103,11 @@ template <class type_t> PyObject *convertMultiArray(
}

// Reserve space for array.
jobjectArray contents = (jobjectArray) context->_java_lang_Object->newArrayInstance(frame, subs);
jobjectArray contents = (jobjectArray) context->_java_lang_Object->newArrayOf(frame, subs);
std::vector<Py_ssize_t> indices(view.ndim);
int u = view.ndim - 1;
int k = 0;
jarray a0 = cls->newArrayInstance(frame, base);
jarray a0 = cls->newArrayOf(frame, base);
frame.SetObjectArrayElement(contents, k++, a0);
jboolean isCopy;
void *mem = frame.getEnv()->GetPrimitiveArrayCritical(a0, &isCopy);
Expand Down Expand Up @@ -141,7 +146,7 @@ template <class type_t> PyObject *convertMultiArray(
if (j == u)
break;

a0 = cls->newArrayInstance(frame, base);
a0 = cls->newArrayOf(frame, base);
frame.SetObjectArrayElement(contents, k++, a0);
mem = frame.getEnv()->GetPrimitiveArrayCritical(a0, &isCopy);
JP_TRACE_JAVA("GetPrimitiveArrayCritical", mem);
Expand Down Expand Up @@ -171,7 +176,7 @@ class JPConversionLong : public JPIndexConversion
{
public:

virtual JPMatch::Type matches(JPClass *cls, JPMatch &match)
virtual JPMatch::Type matches(JPClass *cls, JPMatch &match) override
{
if (!PyLong_CheckExact(match.object) && !PyIndex_Check(match.object))
return match.type = JPMatch::_none;
Expand Down Expand Up @@ -204,7 +209,7 @@ class JPConversionLongNumber : public JPConversionLong<base_t>
{
public:

virtual JPMatch::Type matches(JPClass *cls, JPMatch &match)
virtual JPMatch::Type matches(JPClass *cls, JPMatch &match) override
{
if (!PyNumber_Check(match.object))
return match.type = JPMatch::_none;
Expand Down
7 changes: 0 additions & 7 deletions native/common/include/jp_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ class JPProxy

virtual JPPyObject getCallable(const string& cname) = 0;
static void releaseProxyPython(void* host);
static JNIEXPORT jobject JNICALL hostInvoke(
JNIEnv *env, jclass clazz,
jlong contextPtr, jstring name,
jlong hostObj,
jlong returnTypePtr,
jlongArray parameterTypePtrs,
jobjectArray args);

protected:
JPContext* m_Context;
Expand Down
21 changes: 3 additions & 18 deletions native/common/include/jp_reference_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,10 @@
#define JP_REFERENCE_QUEUE_H__
#include <jpype.h>

extern "C"
namespace JPReferenceQueue
{
typedef void (*JCleanupHook)(void*) ;
}

class JPReferenceQueue
{
friend class JPContext;
public:
explicit JPReferenceQueue(JPJavaFrame& frame);
~JPReferenceQueue();
void registerRef( jobject obj, PyObject* targetRef);
void registerRef(jobject obj, void* host, JCleanupHook func);

private:
JPContext* m_Context;
JPObjectRef m_ReferenceQueue;
jmethodID m_ReferenceQueueRegisterMethod;

void registerRef(JPJavaFrame &frame, jobject obj, PyObject* targetRef);
void registerRef(JPJavaFrame &frame, jobject obj, void* host, JCleanupHook func);
} ; // end of namespace JPReferenceQueue

#endif
Loading

0 comments on commit 3f0a518

Please sign in to comment.