Skip to content
Permalink
Browse files

Dealing with overflow error.

  • Loading branch information
Thrameos committed Mar 20, 2020
1 parent c371eb0 commit f1d07bdc62854ce6d899947098a63d5e34cbf2e2
Showing with 339 additions and 232 deletions.
  1. +12 −12 jpype/_core.py
  2. +1 −1 native/common/include/jp_arrayclass.h
  3. +1 −1 native/common/include/jp_booleantype.h
  4. +1 −0 native/common/include/jp_boxedtype.h
  5. +1 −1 native/common/include/jp_bytetype.h
  6. +1 −1 native/common/include/jp_chartype.h
  7. +9 −3 native/common/include/jp_class.h
  8. +1 −1 native/common/include/jp_doubletype.h
  9. +1 −1 native/common/include/jp_floattype.h
  10. +1 −1 native/common/include/jp_inttype.h
  11. +1 −1 native/common/include/jp_longtype.h
  12. +20 −8 native/common/include/jp_primitive_accessor.h
  13. +3 −0 native/common/include/jp_primitivetype.h
  14. +1 −1 native/common/include/jp_proxy.h
  15. +1 −1 native/common/include/jp_shorttype.h
  16. +1 −1 native/common/include/jp_stringtype.h
  17. +1 −1 native/common/include/jp_voidtype.h
  18. +10 −2 native/common/jp_arrayclass.cpp
  19. +7 −7 native/common/jp_booleantype.cpp
  20. +23 −0 native/common/jp_boxedtype.cpp
  21. +11 −8 native/common/jp_bytetype.cpp
  22. +15 −8 native/common/jp_chartype.cpp
  23. +51 −25 native/common/jp_class.cpp
  24. +12 −8 native/common/jp_doubletype.cpp
  25. +2 −1 native/common/jp_exception.cpp
  26. +11 −7 native/common/jp_floattype.cpp
  27. +13 −8 native/common/jp_inttype.cpp
  28. +11 −8 native/common/jp_longtype.cpp
  29. +2 −2 native/common/jp_method.cpp
  30. +24 −0 native/common/jp_primitivetype.cpp
  31. +2 −2 native/common/jp_proxy.cpp
  32. +11 −8 native/common/jp_shorttype.cpp
  33. +13 −9 native/common/jp_stringtype.cpp
  34. +1 −1 native/common/jp_voidtype.cpp
  35. +1 −2 native/python/include/pyjp.h
  36. +1 −1 native/python/pyjp_array.cpp
  37. +16 −9 native/python/pyjp_class.cpp
  38. +4 −4 native/python/pyjp_method.cpp
  39. +2 −2 native/python/pyjp_module.cpp
  40. +17 −23 native/python/pyjp_number.cpp
  41. +0 −48 native/python/pyjp_value.cpp
  42. +5 −0 test/jpypetest/test_jbyte.py
  43. +5 −0 test/jpypetest/test_jint.py
  44. +5 −0 test/jpypetest/test_jlong.py
  45. +7 −4 test/jpypetest/test_jshort.py
@@ -250,6 +250,18 @@ def startJVM(*args, **kwargs):
_jpype._java_lang_Float = _jpype.JClass("java.lang.Float")
_jpype._java_lang_Double = _jpype.JClass("java.lang.Double")

# Bind types
_jpype.JString.class_ = _jpype._java_lang_String
_jpype.JObject.class_ = _jpype._java_lang_Object
_jtypes.JBoolean.class_ = _jpype._java_lang_Boolean.TYPE
_jtypes.JByte.class_ = _jpype._java_lang_Byte.TYPE
_jtypes.JChar.class_ = _jpype._java_lang_Character.TYPE
_jtypes.JShort.class_ = _jpype._java_lang_Short.TYPE
_jtypes.JInt.class_ = _jpype._java_lang_Integer.TYPE
_jtypes.JLong.class_ = _jpype._java_lang_Long.TYPE
_jtypes.JFloat.class_ = _jpype._java_lang_Float.TYPE
_jtypes.JDouble.class_ = _jpype._java_lang_Double.TYPE

# Table for automatic conversion to objects "JObject(value, type)"
_jpype._object_classes = {}
_jpype._object_classes[bool] = _jpype._java_lang_Boolean
@@ -283,18 +295,6 @@ def startJVM(*args, **kwargs):
_jpype._type_classes[_jpype.JObject] = _jpype._java_lang_Object
_jinit.runJVMInitializers()

# Bind types
_jpype.JString.class_ = _jpype._java_lang_String
_jpype.JObject.class_ = _jpype._java_lang_Object
_jtypes.JBoolean.class_ = _jpype._java_lang_Boolean.TYPE
_jtypes.JByte.class_ = _jpype._java_lang_Byte.TYPE
_jtypes.JChar.class_ = _jpype._java_lang_Character.TYPE
_jtypes.JShort.class_ = _jpype._java_lang_Short.TYPE
_jtypes.JInt.class_ = _jpype._java_lang_Integer.TYPE
_jtypes.JLong.class_ = _jpype._java_lang_Long.TYPE
_jtypes.JFloat.class_ = _jpype._java_lang_Float.TYPE
_jtypes.JDouble.class_ = _jpype._java_lang_Double.TYPE


def attachToJVM(jvm):
_jpype.attach(jvm)
@@ -31,7 +31,7 @@ class JPArrayClass : public JPClass
jint modifiers);
virtual~ JPArrayClass();

virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPMatch::Type findJavaConversion(JPMatch &match);

JPValue newInstance(JPJavaFrame& frame, int length);
@@ -43,7 +43,7 @@ class JPBooleanType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch& match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -47,6 +47,7 @@ class JPBoxedType : public JPClass
}

jobject box(JPJavaFrame &frame, jvalue v);
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;

protected:
JPPrimitiveType* m_PrimitiveType;
@@ -44,7 +44,7 @@ class JPByteType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -44,7 +44,7 @@ class JPCharType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -37,9 +37,9 @@ class JPClass : public JPResource
m_Host = JPPyObject(JPPyRef::_use, host);
}

PyObject* getHost()
PyTypeObject* getHost()
{
return m_Host.get();
return (PyTypeObject*) m_Host.get();
}

void setHints(PyObject* host)
@@ -132,9 +132,15 @@ class JPClass : public JPResource

/** Create a new Python object to wrap a Java value.
*
* Some conversion convert to a Python type such as Java boolean and char.
* Null pointers match to Python None. Objects convert automatically to
* the most derived type. To disable this behavior the cast option can be
* specified.
*
* @param cast force the wrapper to be the defined type.
* @return a new Python object.
*/
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val);
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast);

/**
* Get the Java value representing as an object.
@@ -44,7 +44,7 @@ class JPDoubleType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame &frame, jclass, jmethodID, jvalue*) override;
@@ -44,7 +44,7 @@ class JPFloatType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame &frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -44,7 +44,7 @@ class JPIntType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch& match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -43,7 +43,7 @@ class JPLongType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch& match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -63,7 +63,7 @@ class JPPrimitiveArrayAccessor
((&_frame)->*_release)(a, _elem, JNI_ABORT);
}

};
} ;

template <class type_t> PyObject *convertMultiArray(
JPJavaFrame &frame,
@@ -145,7 +145,7 @@ template <class type_t> PyObject *convertMultiArray(
type = frame.findClassForObject(out);
jvalue v;
v.l = out;
return type->convertToPythonObject(frame, v).keep();
return type->convertToPythonObject(frame, v, false).keep();
}

template <typename base_t>
@@ -164,10 +164,19 @@ class JPConversionLong : public JPConversion
virtual jvalue convert(JPMatch &match) override
{
jvalue res;
jlong val = PyLong_AsLongLong(match.object);
if (val == -1)
JP_PY_CHECK();
base_t::field(res) = (typename base_t::type_t) base_t::assertRange(val);
if (match.type == JPMatch::_exact)
{
jlong val = (jlong) PyLong_AsUnsignedLongLongMask(match.object);
if (val == -1)
JP_PY_CHECK();
base_t::field(res) = (typename base_t::type_t) val;
} else
{
jlong val = (jlong) PyLong_AsLongLong(match.object);
if (val == -1)
JP_PY_CHECK();
base_t::field(res) = (typename base_t::type_t) base_t::assertRange(val);
}
return res;
}
} ;
@@ -194,10 +203,13 @@ class JPConversionLongNumber : public JPConversion
Py_DECREF(obj);
if (val == -1)
JP_PY_CHECK();
base_t::field(res) = (typename base_t::type_t) base_t::assertRange(val);
if (match.type == JPMatch::_exact)
base_t::field(res) = (typename base_t::type_t) val;
else
base_t::field(res) = (typename base_t::type_t) base_t::assertRange(val);
return res;
}
};
} ;

template <typename base_t>
class JPConversionLongWiden : public JPConversion
@@ -48,6 +48,9 @@ class JPPrimitiveType : public JPClass

virtual PyObject *newMultiArray(JPJavaFrame &frame,
JPPyBuffer& view, int subs, int base, jobject dims) = 0;

// Helper for Long types
PyObject *convertLong(PyTypeObject* wrapper, PyLongObject* tmp);
} ;

#endif
@@ -74,7 +74,7 @@ class JPProxyType : public JPClass
virtual~ JPProxyType();

public: // JPClass implementation
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;

private:
JPClassRef m_ProxyClass;
@@ -43,7 +43,7 @@ class JPShortType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
virtual JPValue getValueFromObject(const JPValue& obj) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
@@ -30,7 +30,7 @@ class JPStringType : public JPClass
virtual ~JPStringType();

public:
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;
JPMatch::Type findJavaConversion(JPMatch& match) override;
virtual JPValue newInstance(JPJavaFrame& frame, JPPyObjectVector& args) override;
} ;
@@ -30,7 +30,7 @@ class JPVoidType : public JPPrimitiveType
}

virtual JPMatch::Type findJavaConversion(JPMatch &match) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val) override;
virtual JPPyObject convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast) override;

virtual JPPyObject invokeStatic(JPJavaFrame& frame, jclass, jmethodID, jvalue*) override;
virtual JPPyObject invoke(JPJavaFrame& frame, jobject, jclass, jmethodID, jvalue*) override;
@@ -50,10 +50,18 @@ JPMatch::Type JPArrayClass::findJavaConversion(JPMatch &match)
JP_TRACE_OUT;
}

JPPyObject JPArrayClass::convertToPythonObject(JPJavaFrame& frame, jvalue val)
JPPyObject JPArrayClass::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast)
{
JP_TRACE_IN("JPArrayClass::convertToPythonObject");
return PyJPValue_create(frame, JPValue(this, val));
if (!cast)
{
if (value.l == NULL)
return JPPyObject::getNone();
}
JPPyObject wrapper = PyJPClass_create(frame, this);
JPPyObject obj = PyJPArray_create(frame, (PyTypeObject*) wrapper.get(), JPValue(this, value));
PyJPValue_assignJavaSlot(frame, obj.get(), JPValue(this, value));
return obj;
JP_TRACE_OUT;
}

@@ -28,7 +28,7 @@ JPBooleanType::~JPBooleanType()
{
}

JPPyObject JPBooleanType::convertToPythonObject(JPJavaFrame& frame, jvalue val)
JPPyObject JPBooleanType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast)
{
return JPPyObject(JPPyRef::_call, PyBool_FromLong(val.z));
}
@@ -113,14 +113,14 @@ JPPyObject JPBooleanType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID
{
jvalue v;
field(v) = frame.GetStaticBooleanField(c, fid);
return convertToPythonObject(frame, v);
return convertToPythonObject(frame, v, false);
}

JPPyObject JPBooleanType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
{
jvalue v;
field(v) = frame.GetBooleanField(c, fid);
return convertToPythonObject(frame, v);
return convertToPythonObject(frame, v, false);
}

JPPyObject JPBooleanType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val)
@@ -130,7 +130,7 @@ JPPyObject JPBooleanType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodI
JPPyCallRelease call;
field(v) = frame.CallStaticBooleanMethodA(claz, mth, val);
}
return convertToPythonObject(frame, v);
return convertToPythonObject(frame, v, false);
}

JPPyObject JPBooleanType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val)
@@ -143,7 +143,7 @@ JPPyObject JPBooleanType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz,
else
field(v) = frame.CallNonvirtualBooleanMethodA(obj, clazz, mth, val);
}
return convertToPythonObject(frame, v);
return convertToPythonObject(frame, v, false);
}

void JPBooleanType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj)
@@ -227,7 +227,7 @@ JPPyObject JPBooleanType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
frame.GetBooleanArrayRegion(array, ndx, 1, &val);
jvalue v;
field(v) = val;
return convertToPythonObject(frame, v);
return convertToPythonObject(frame, v, false);
}

void JPBooleanType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj)
@@ -291,4 +291,4 @@ PyObject *JPBooleanType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, i
frame, this, &pack, "z",
buffer, subs, base, dims);
JP_TRACE_OUT;
}
}
@@ -15,6 +15,7 @@
*****************************************************************************/
#include "jpype.h"
#include "pyjp.h"
#include "jp_boxedtype.h"

JPBoxedType::JPBoxedType(JPJavaFrame& frame, jclass clss,
@@ -58,3 +59,25 @@ jobject JPBoxedType::box(JPJavaFrame &frame, jvalue v)
{
return frame.NewObjectA(m_Class.get(), m_CtorID, &v);
}

JPPyObject JPBoxedType::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast)
{
JPClass *cls = this;
if (!cast)
{
// This loses type
if (value.l == NULL)
{
return JPPyObject::getNone();
}

cls = frame.findClassForObject(value.l);
if (cls != this)
return cls->convertToPythonObject(frame, value, true);
}

JPPyObject wrapper = PyJPClass_create(frame, cls);
JPPyObject obj = PyJPNumber_create(frame, wrapper, JPValue(cls, value));
PyJPValue_assignJavaSlot(frame, obj.get(), JPValue(cls, value));
return obj;
}

0 comments on commit f1d07bd

Please sign in to comment.
You can’t perform that action at this time.