From 87753abd4a1dc335c05833c1079258e20e6890ca Mon Sep 17 00:00:00 2001 From: Ben Steffensmeier Date: Thu, 2 Sep 2021 21:09:53 -0500 Subject: [PATCH] Update repr test after rebase, add check for repr in attr --- src/main/c/Objects/pyjobject.c | 35 ++++++++++++++++++---------------- src/test/python/test_object.py | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/c/Objects/pyjobject.c b/src/main/c/Objects/pyjobject.c index ffd151be..8ad1fc14 100644 --- a/src/main/c/Objects/pyjobject.c +++ b/src/main/c/Objects/pyjobject.c @@ -287,25 +287,28 @@ static PyObject* pyjobject_repr(PyJObject *self) //check if __repr__ method exists on java object if (self->object && self->clazz) { - env = pyembed_get_env(); + PyObject *repr = PyDict_GetItemString(self->attr, "__repr__"); + if (repr) { + env = pyembed_get_env(); - Py_BEGIN_ALLOW_THREADS - if (JNI_METHOD(__repr__, env, self->clazz, "__repr__", "()Ljava/lang/String;")) { - jrepr = (jstring) (*env)->CallObjectMethod(env, self->object, __repr__); - } else { - //method does not exist, clear exception - if ((*env)->ExceptionCheck(env)) { - (*env)->ExceptionClear(env); + Py_BEGIN_ALLOW_THREADS + if (JNI_METHOD(__repr__, env, self->clazz, "__repr__", "()Ljava/lang/String;")) { + jrepr = (jstring) (*env)->CallObjectMethod(env, self->object, __repr__); + } else { + //method does not exist, clear exception + if ((*env)->ExceptionCheck(env)) { + (*env)->ExceptionClear(env); + } } + Py_END_ALLOW_THREADS + if (process_java_exception(env)) { + return NULL; + } + if (jrepr != NULL) { + pyres = jstring_As_PyString(env, jrepr); + } + (*env)->DeleteLocalRef(env, jrepr); } - Py_END_ALLOW_THREADS - if (process_java_exception(env)) { - return NULL; - } - if (jrepr != NULL) { - pyres = jstring_As_PyString(env, jrepr); - } - (*env)->DeleteLocalRef(env, jrepr); } if (pyres == NULL) { diff --git a/src/test/python/test_object.py b/src/test/python/test_object.py index d6a417c1..d3d309c1 100644 --- a/src/test/python/test_object.py +++ b/src/test/python/test_object.py @@ -17,7 +17,7 @@ def test_str(self): def test_repr(self): self.assertEquals(repr(TestPyJObject.ReprClass()), "ReprClass") self.assertEquals(repr(TestPyJObject.ReprSubClass()), "ReprSubClass") - self.assertIn("