Improve diagnostics for PythonException
Status: Beta
Brought to you by:
devilwolf
When PythonException is thrown, it is not always obvious what might be wrong. For example: the test class does not have the required write() method:
class Test(object):
pass
try:
from jpype import *
startJVM(getDefaultJVMPath())
MyStream_proxy = JClass("MyStream_proxy")
python_adapter = Test()
java_proxy = MyStream_proxy(JProxy("MyStream_adapter", inst = python_adapter))
java_proxy.write(5)
except Exception:
import traceback
traceback.print_exc()
Output:
Traceback (most recent call last):
File "F:\temp\jpype\bugs\3-NonInformativeErrorMessage\test.py", line 10, in ?
java_proxy.write(5)
java.lang.ExceptionPyRaisable: java.lang.RuntimeException: Python exception thrown
The exception thrown by Python code is lost.
test.py
Logged In: YES
user_id=1558596
Originator: YES
File Added: test.py
MyStream_proxy.java
Logged In: YES
user_id=1558596
Originator: YES
File Added: MyStream_proxy.java
Logged In: YES
user_id=1558596
Originator: YES
Proposed hack (I do not know how to extend the traceback information):
PythonException::PythonException()
{
...
PyErr_Fetch(&m_ExceptionClass, &m_ExceptionValue, &traceback);
//This PyErr_Display() is added for better diagnostics,
//for some reason PyErr_Restore() does not restore the full traceback stack
PyErr_Display(m_ExceptionClass, m_ExceptionValue, traceback);
Py_INCREF(m_ExceptionClass);
Py_INCREF(m_ExceptionValue);
...