Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught exception in PyObject getIntValue() showes up in the next PyListWrapper.size() call #98

Closed
jmao-denver opened this issue Mar 24, 2023 · 1 comment · Fixed by #99
Assignees
Labels
bug Something isn't working

Comments

@jmao-denver
Copy link
Contributor

In Python,

def foo():
    return [[], None]

In Java, after calling foo() and let 'obj' receive the return value as PyObject

obj.get(1).getIntValue();
obj.get(0).asList().size()

would throw an exception

java.lang.RuntimeException: Error in Python interpreter:
Type: <class 'SystemError'>
Value: <method-wrapper '__len__' of list object at 0x158099dc0> returned a result with an exception set
Line: <not available>
Namespace: <not available>
File: <not available>
Traceback (most recent call last):

        at org.jpy.PyLib.callAndReturnObject(Native Method)
        at org.jpy.PyObject.callMethod(PyObject.java:432)
        at org.jpy.PyListWrapper.size(PyListWrapper.java:36)
@jmao-denver jmao-denver self-assigned this Mar 24, 2023
@jmao-denver
Copy link
Contributor Author

In org_jpy_PyLib.c

/*
 * Class:     org_jpy_python_PyLib
 * Method:    getIntValue
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_org_jpy_PyLib_getIntValue
  (JNIEnv* jenv, jclass jLibClass, jlong objId)
{
    PyObject* pyObject;
    jint value;

    JPy_BEGIN_GIL_STATE

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

    JPy_END_GIL_STATE

    return value;
}

This code doesn't bother to check for errors after the call to JPy_AS_CLONG() and there are a bunch of other methods behave the same. From a performance POV, if we insist that to void such issue, callers need to call the PyObject.isXXX() to check for the expected type first before calling the corresponding getXXX(), this would lead to one more Java/Python boundary crossing and worse performance than if the getXXX() would check for errors in the C function and raise the Java exception accordingly.

@jmao-denver jmao-denver added the bug Something isn't working label Mar 24, 2023
@jmao-denver jmao-denver changed the title Uncaught exceptions in PyObject getIntValue() showed up in the next PyListWrapper.size() call Uncaught exception in PyObject getIntValue() showes up in the next PyListWrapper.size() call Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant