Skip to content

Commit

Permalink
Check for exception in getInt/Long/DoubleValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmao-denver committed Mar 24, 2023
1 parent 87b090d commit 510252a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/c/jni/org_jpy_PyLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,10 @@ JNIEXPORT jint JNICALL Java_org_jpy_PyLib_getIntValue

pyObject = (PyObject*) objId;
value = (jint) JPy_AS_CLONG(pyObject);
if (value == -1 && PyErr_Occurred()) {
JPy_DIAG_PRINT(JPy_DIAG_F_ALL, "Java_org_jpy_PyLib_getIntValue: error: failed to convert Python object to Java int\n");
PyLib_HandlePythonException(jenv);
}

JPy_END_GIL_STATE

Expand All @@ -1217,7 +1221,10 @@ JNIEXPORT jlong JNICALL Java_org_jpy_PyLib_getLongValue

pyObject = (PyObject*) objId;
value = JPy_AS_CLONG(pyObject);

if (value == -1 && PyErr_Occurred()) {
JPy_DIAG_PRINT(JPy_DIAG_F_ALL, "Java_org_jpy_PyLib_getLongValue: error: failed to convert Python object to Java long\n");
PyLib_HandlePythonException(jenv);
}
JPy_END_GIL_STATE

return value;
Expand Down Expand Up @@ -1262,6 +1269,10 @@ JNIEXPORT jdouble JNICALL Java_org_jpy_PyLib_getDoubleValue

pyObject = (PyObject*) objId;
value = (jdouble) PyFloat_AsDouble(pyObject);
if (value == -1.0 && PyErr_Occurred()) {
JPy_DIAG_PRINT(JPy_DIAG_F_ALL, "Java_org_jpy_PyLib_getDoubleValue: error: failed to convert Python object to Java double\n");
PyLib_HandlePythonException(jenv);
}

JPy_END_GIL_STATE

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jpy/PyObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,11 @@ public void errorIfUsageAfterClose() {
obj.close();
obj.getPointer();
}

@Test(expected = RuntimeException.class)
public void errorGetInValue_IntExpr() throws Exception {
PyObject pyObject = PyObject.executeCode("None", PyInputMode.EXPRESSION);
pyObject.getIntValue();
}

}

0 comments on commit 510252a

Please sign in to comment.