Skip to content

Commit

Permalink
Update repr test after rebase, add check for repr in attr
Browse files Browse the repository at this point in the history
  • Loading branch information
bsteffensmeier committed Sep 3, 2021
1 parent 6589210 commit 87753ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions src/main/c/Objects/pyjobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/python/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<jep.PyJObject object at", repr(Object()))
self.assertIn("<java.lang.Object object at", repr(Object()))

def test_del_throws_exception(self):
o = Object()
Expand Down

0 comments on commit 87753ab

Please sign in to comment.