Skip to content

Commit

Permalink
Merge pull request #303 from psader/feature/fix_reflext_indexerror
Browse files Browse the repository at this point in the history
Fix bad JavaException in _getitem
  • Loading branch information
tito committed Dec 11, 2017
2 parents 1418004 + 3ce729b commit 5d1e563
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions jnius/jnius_conversion.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar
j_env, argtype[1:], py_arg)


cdef convert_jstring_to_python(JNIEnv *j_env, jstring string):
c_str = <char *>j_env[0].GetStringUTFChars(j_env, string, NULL)
py_str = <bytes>c_str
j_env[0].ReleaseStringUTFChars(j_env, string, c_str)
if PY_MAJOR_VERSION < 3:
return py_str
else:
return py_str.decode('utf-8')


cdef convert_jobject_to_python(JNIEnv *j_env, definition, jobject j_object):
# Convert a Java Object to a Python object, according to the definition.
# If the definition is a java/lang/Object, then try to determine what is it
Expand Down
2 changes: 1 addition & 1 deletion jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ cdef void check_exception(JNIEnv *j_env) except *:
getStackTrace = j_env[0].GetMethodID(j_env, cls_throwable, "getStackTrace", "()[Ljava/lang/StackTraceElement;");

e_msg = j_env[0].CallObjectMethod(j_env, exc, getMessage);
pymsg = None if e_msg == NULL else convert_jobject_to_python(j_env, <bytes> 'Ljava/lang/String;', e_msg)
pymsg = None if e_msg == NULL else convert_jstring_to_python(j_env, e_msg)

pystack = []
_append_exception_trace_messages(j_env, pystack, exc, getCause, getStackTrace, toString)
Expand Down
2 changes: 1 addition & 1 deletion jnius/reflect.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _getitem(self, index):
# initialize the subclass before getting the Class.forName
# otherwise isInstance does not know of the subclass
mock_exception_object = autoclass(e.classname)()
if Class.forName("java.lang.IndexOutOfBoundsException").isInstance(mock_exception_object):
if find_javaclass("java.lang.IndexOutOfBoundsException").isInstance(mock_exception_object):
# python for...in iteration checks for end of list by waiting for IndexError
raise IndexError()
else:
Expand Down

0 comments on commit 5d1e563

Please sign in to comment.