Skip to content

Commit

Permalink
Backporting bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Jun 16, 2019
1 parent eb1af8a commit 7e30331
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 56 deletions.
14 changes: 7 additions & 7 deletions native/common/include/jp_methodoverload.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#ifndef _JPMETHODOVERLOAD_H_
#define _JPMETHODOVERLOAD_H_
Expand Down Expand Up @@ -62,10 +62,10 @@ class JPMethodOverload
*
* @param isInstance is true if the first argument is an instance object.
* @param args is a list of arguments including the instance.
*
*
*/
JPMatch matches(bool isInstance, JPPyObjectVector& args) ;
JPPyObject invoke(JPMatch& match, JPPyObjectVector& arg);
JPPyObject invoke(JPMatch& match, JPPyObjectVector& arg, bool instance);
JPValue invokeConstructor(JPMatch& match, JPPyObjectVector& arg);

bool isStatic() const
Expand Down Expand Up @@ -111,14 +111,14 @@ class JPMethodOverload
bool checkMoreSpecificThan(JPMethodOverload* other) const;

/** Used to determine if a bean get property should be added to the class.
*
* FIXME This does not check for begins with "get"
*
* FIXME This does not check for begins with "get"
*/
bool isBeanAccessor();

/** Used to determine if a bean set property should be added to the class.
*
* FIXME This does not check for begins with "set" or "is"
*
* FIXME This does not check for begins with "set" or "is"
*/
bool isBeanMutator();

Expand Down
2 changes: 1 addition & 1 deletion native/common/jp_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ JPPyObject JPMethod::invoke(JPPyObjectVector& args, bool instance)
{
JP_TRACE_IN("JPMethod::invoke");
JPMatch match = findOverload(args, instance);
return match.overload->invoke(match, args);
return match.overload->invoke(match, args, instance);
JP_TRACE_OUT;
}

Expand Down
6 changes: 3 additions & 3 deletions native/common/jp_methodoverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#include <jpype.h>

Expand Down Expand Up @@ -244,7 +244,7 @@ void JPMethodOverload::packArgs(JPMatch& match, vector<jvalue>& v, JPPyObjectVec
JP_TRACE_OUT;
}

JPPyObject JPMethodOverload::invoke(JPMatch& match, JPPyObjectVector& arg)
JPPyObject JPMethodOverload::invoke(JPMatch& match, JPPyObjectVector& arg, bool instance)
{
JP_TRACE_IN("JPMethodOverload::invoke");
ensureTypeCache();
Expand All @@ -268,7 +268,7 @@ JPPyObject JPMethodOverload::invoke(JPMatch& match, JPPyObjectVector& arg)
JPValue* selfObj = JPPythonEnv::getJavaValue(arg[0]);
jobject c = selfObj->getJavaObject();
jclass clazz = NULL;
if (!m_IsAbstract)
if (!m_IsAbstract && !instance)
clazz = m_Class->getJavaClass();
return retType->invoke(frame, c, clazz, m_MethodID, &v[0]);
}
Expand Down
5 changes: 4 additions & 1 deletion native/python/include/pyjp_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#ifndef _PYMETHOD_H_
#define _PYMETHOD_H_
Expand All @@ -30,6 +30,9 @@ struct PyJPMethod

static PyObject* __new__(PyTypeObject* self, PyObject* args, PyObject* kwargs);
static void __dealloc__(PyJPMethod* o);
static int traverse(PyJPMethod *self, visitproc visit, void *arg);
static int clear(PyJPMethod *self);

static PyObject* __get__(PyJPMethod* self, PyObject* obj, PyObject* type);
static PyObject* __str__(PyJPMethod* o);
static PyObject* __call__(PyJPMethod* self, PyObject* args, PyObject* kwargs);
Expand Down
4 changes: 3 additions & 1 deletion native/python/include/pyjp_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#ifndef _PYJPPROXY_H_
#define _PYJPPROXY_H_
Expand All @@ -29,6 +29,8 @@ struct PyJPProxy
static PyObject* __new__(PyTypeObject* self, PyObject* args, PyObject* kwargs);
static int __init__(PyJPProxy* self, PyObject* args, PyObject* kwargs);
static void __dealloc__(PyJPProxy* self);
static int traverse(PyJPProxy *self, visitproc visit, void *arg);
static int clear(PyJPProxy *self);
static PyObject* __str__(PyJPProxy* self);

JPProxy* m_Proxy;
Expand Down
5 changes: 4 additions & 1 deletion native/python/include/pyjp_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#ifndef _PYJP_VALUE_H_
#define _PYJP_VALUE_H_
Expand All @@ -32,6 +32,9 @@ struct PyJPValue
static PyObject* __new__(PyTypeObject* self, PyObject* args, PyObject* kwargs);
static int __init__(PyJPValue* self, PyObject* args, PyObject* kwargs);
static void __dealloc__(PyJPValue* self);
static int traverse(PyJPValue *self, visitproc visit, void *arg);
static int clear(PyJPValue *self);

static PyObject* __str__(PyJPValue* self);
static PyObject* toString(PyJPValue* self);
static PyObject* toUnicode(PyJPValue* self);
Expand Down
6 changes: 3 additions & 3 deletions native/python/pyjp_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/

// FIXME PyJPArray should inherit from PyJPValue so that arrays are
// FIXME PyJPArray should inherit from PyJPValue so that arrays are
// properly specializations of value types.

#include <pyjp.h>
Expand Down Expand Up @@ -88,7 +88,7 @@ JPPyObject PyJPArray::alloc(JPArray* obj)
{
JPJavaFrame fame;
JP_TRACE_IN("PyJPArray::alloc");
PyJPArray* res = PyObject_New(PyJPArray, &PyJPArray::Type);
PyJPArray* res = (PyJPArray*) PyJPArray::Type.tp_alloc(&PyJPArray::Type, 0);
JP_PY_CHECK();
res->m_Array = obj;
return JPPyObject(JPPyRef::_claim, (PyObject*) res);
Expand Down
6 changes: 3 additions & 3 deletions native/python/pyjp_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/

#include <pyjp.h>
Expand Down Expand Up @@ -99,7 +99,7 @@ bool PyJPClass::check(PyObject* o)

JPPyObject PyJPClass::alloc(JPClass* cls)
{
PyJPClass* res = PyObject_New(PyJPClass, &PyJPClass::Type);
PyJPClass* res = (PyJPClass*) PyJPClass::Type.tp_alloc(&PyJPClass::Type, 0);
JP_PY_CHECK();
res->m_Class = cls;
return JPPyObject(JPPyRef::_claim, (PyObject*) res);
Expand Down Expand Up @@ -310,7 +310,7 @@ PyObject* PyJPClass::isAssignableFrom(PyJPClass* self, PyObject* arg)
ASSERT_JVM_RUNNING("PyJPClass::isSubClass");
JPJavaFrame frame;

// We have to lookup the name by string here because the
// We have to lookup the name by string here because the
// class wrapper may not exist. This is used by the
// customizers.
PyObject* other;
Expand Down
4 changes: 2 additions & 2 deletions native/python/pyjp_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#include <pyjp.h>

Expand Down Expand Up @@ -76,7 +76,7 @@ void PyJPField::initType(PyObject* module)

JPPyObject PyJPField::alloc(JPField* m)
{
PyJPField* res = PyObject_New(PyJPField, &PyJPField::Type);
PyJPField* res = (PyJPField*) PyJPField::Type.tp_alloc(&PyJPField::Type, 0);;
JP_PY_CHECK();
res->m_Field = m;
return JPPyObject(JPPyRef::_claim, (PyObject*) res);
Expand Down
30 changes: 19 additions & 11 deletions native/python/pyjp_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#include <pyjp.h>

Expand Down Expand Up @@ -45,10 +45,10 @@ PyTypeObject PyJPMethod::Type = {
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
/* tp_doc */ "Java Method",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_traverse */ (traverseproc) PyJPProxy::traverse,
/* tp_clear */ (inquiry) PyJPProxy::clear,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
Expand Down Expand Up @@ -78,7 +78,7 @@ void PyJPMethod::initType(PyObject* module)
JPPyObject PyJPMethod::alloc(JPMethod* m, PyObject* instance)
{
JP_TRACE_IN("PyJPMethod::alloc");
PyJPMethod* res = PyObject_New(PyJPMethod, &PyJPMethod::Type);
PyJPMethod* res = (PyJPMethod*) PyJPMethod::Type.tp_alloc(&PyJPMethod::Type, 0);;
JP_PY_CHECK();
res->m_Method = m;
res->m_Instance = instance;
Expand Down Expand Up @@ -145,16 +145,24 @@ PyObject* PyJPMethod::__call__(PyJPMethod* self, PyObject* args, PyObject* kwarg

void PyJPMethod::__dealloc__(PyJPMethod* self)
{
if (self->m_Instance != NULL)
{
JP_TRACE_PY("method dealloc (dec)", self->m_Instance);
Py_DECREF(self->m_Instance);
}
self->m_Instance = NULL;
PyObject_GC_UnTrack(self);
clear(self);
self->m_Method = NULL;
Py_TYPE(self)->tp_free(self);
}

int PyJPMethod::traverse(PyJPMethod *self, visitproc visit, void *arg)
{
Py_VISIT(self->m_Instance);
return 0;
}

int PyJPMethod::clear(PyJPMethod *self)
{
Py_CLEAR(self->m_Instance);
return 0;
}

PyObject* PyJPMethod::__str__(PyJPMethod* self)
{
try
Expand Down
35 changes: 25 additions & 10 deletions native/python/pyjp_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#include <pyjp.h>

Expand Down Expand Up @@ -40,10 +40,10 @@ PyTypeObject PyJPProxy::Type = {
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
/* tp_doc */ "Java Proxy",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_traverse */ (traverseproc) PyJPProxy::traverse,
/* tp_clear */ (inquiry) PyJPProxy::clear,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
Expand Down Expand Up @@ -151,16 +151,31 @@ void PyJPProxy::__dealloc__(PyJPProxy* self)
{
JP_TRACE_IN("PyJPProxy::dealloc");
delete self->m_Proxy;
if (self->m_Target != NULL)
Py_DECREF(self->m_Target);
if (self->m_Callable != NULL)
Py_DECREF(self->m_Callable);
self->m_Target = NULL;
self->m_Callable = NULL;
PyObject_GC_UnTrack(self);
clear(self);
// Free self
Py_TYPE(self)->tp_free(self);
JP_TRACE_OUT;
}

int PyJPProxy::traverse(PyJPProxy *self, visitproc visit, void *arg)
{
JP_TRACE_IN("PyJPProxy::traverse");
Py_VISIT(self->m_Target);
Py_VISIT(self->m_Callable);
return 0;
JP_TRACE_OUT;
}

int PyJPProxy::clear(PyJPProxy *self)
{
JP_TRACE_IN("PyJPProxy::clear");
Py_CLEAR(self->m_Target);
Py_CLEAR(self->m_Callable);
return 0;
JP_TRACE_OUT;
}

bool PyJPProxy::check(PyObject* o)
{
return Py_TYPE(o) == &PyJPProxy::Type;
Expand Down
32 changes: 20 additions & 12 deletions native/python/pyjp_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#include <pyjp.h>

Expand Down Expand Up @@ -42,14 +42,14 @@ PyTypeObject PyJPValue::Type = {
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
/* tp_doc */
"Wrapper of a java value which holds a class and instance of an object \n"
"or a primitive. This object is always stored as the attributed \n"
"__javavalue__. Anything with this type with that attribute will be\n"
"considered a java object wrapper.",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_traverse */ (traverseproc) PyJPValue::traverse,
/* tp_clear */ (inquiry) PyJPValue::clear,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
Expand Down Expand Up @@ -92,7 +92,7 @@ JPPyObject PyJPValue::alloc(JPClass* cls, jvalue value)
{
JPJavaFrame frame;
JP_TRACE_IN("PyJPValue::alloc");
PyJPValue* self = PyObject_New(PyJPValue, &PyJPValue::Type);
PyJPValue* self = (PyJPValue*) PyJPValue::Type.tp_alloc(&PyJPValue::Type, 0);
JP_PY_CHECK();

// If it is not a primitive we need to reference it
Expand Down Expand Up @@ -200,12 +200,7 @@ void PyJPValue::__dealloc__(PyJPValue* self)
JP_TRACE_IN("PyJPValue::__dealloc__");
JPValue& value = self->m_Value;
JPClass* cls = value.getClass();
JP_TRACE("Value", cls, &(value.getValue()));
if (self->m_Cache != NULL)
{
Py_DECREF(self->m_Cache);
self->m_Cache = NULL;
}

// This one can't check for initialized because we may need to delete a stale
// resource after shutdown.
if (cls != NULL && JPEnv::isInitialized() && dynamic_cast<JPPrimitiveType*> (cls) != cls)
Expand All @@ -218,11 +213,24 @@ void PyJPValue::__dealloc__(PyJPValue* self)
JP_TRACE("Dereference object");
JPJavaFrame::ReleaseGlobalRef(value.getValue().l);
}
JP_TRACE("free", Py_TYPE(self)->tp_free);
PyObject_GC_UnTrack(self);
clear(self);
Py_TYPE(self)->tp_free(self);
JP_TRACE_OUT;
}

int PyJPValue::traverse(PyJPValue *self, visitproc visit, void *arg)
{
Py_VISIT(self->m_Cache);
return 0;
}

int PyJPValue::clear(PyJPValue *self)
{
Py_CLEAR(self->m_Cache);
return 0;
}

void ensureCache(PyJPValue* self)
{
if (self->m_Cache != NULL)
Expand Down
Loading

0 comments on commit 7e30331

Please sign in to comment.