return (*env)->ThrowNew( env, exClass, message );\r
}\r
\r
+jint throwPythonException( JNIEnv *env, char *message ) {\r
+ return throwException( env, PYTHON_EXCEPTION, message );\r
+}\r
+\r
jint throwIllegalArgumentException( JNIEnv *env, char *message ) {\r
return throwException( env, ILLEGAL_ARGUMENT_EXCEPTION, message );\r
}\r
(*env)->SetObjectArrayElement(env, array, i, value);\r
}\r
else {\r
- throwException(env, RUNTIME_EXCEPTION, "List item not a string");\r
+ throwPythonException(env, "List item not a string");\r
return NULL;\r
}\r
}\r
(*env)->SetDoubleArrayRegion(env, array, i, 1, &value);\r
}\r
else {\r
- throwException(env, RUNTIME_EXCEPTION, "List item not a floating point value");\r
+ throwPythonException(env, "List item not a floating point value");\r
return NULL;\r
}\r
}\r
(*env)->SetBooleanArrayRegion(env, array, i, 1, &value);\r
}\r
else {\r
- throwException(env, RUNTIME_EXCEPTION, "List item not a boolean");\r
+ throwPythonException(env, "List item not a boolean");\r
return NULL;\r
}\r
}\r
(*env)->SetIntArrayRegion(env, array, i, 1, &value);\r
}\r
else {\r
- throwException(env, RUNTIME_EXCEPTION, "List item not an integer");\r
+ throwPythonException(env, "List item not an integer");\r
return NULL;\r
}\r
}\r
(*env)->SetLongArrayRegion(env, array, i, 1, &value);\r
}\r
else {\r
- throwException(env, RUNTIME_EXCEPTION, "List item not an integer");\r
+ throwPythonException(env, "List item not an integer");\r
return NULL;\r
}\r
}\r
int i;\r
\r
if (len > JAVA_MAXINT) {\r
- throwException(env, RUNTIME_EXCEPTION, "Array too large");\r
+ throwPythonException(env, "Array too large");\r
return NULL;\r
}\r
\r
PyObject *module = (PyObject*)contextID;\r
\r
if (!hasNumpy) {\r
- throwException(env, RUNTIME_EXCEPTION, "Importing numpy failed");\r
+ throwPythonException(env, "Importing numpy failed");\r
return;\r
}\r
\r
\r
PyObject *exceptionType = PyErr_Occurred();\r
if (exceptionType != NULL) {\r
- PyObject *exception, *stackTrace;\r
- char *message;\r
- PyErr_Fetch(&exceptionType, &exception, &stackTrace);\r
- message = PyUnicode_AsUTF8(exception);\r
- throwException(env, RUNTIME_EXCEPTION, message);\r
+ PyObject *exception, *traceback;\r
+ PyErr_Fetch(&exceptionType, &exception, &traceback);\r
+\r
+ {\r
+ PyObject *tracebackModule = PyImport_ImportModule("traceback");\r
+ if (tracebackModule != NULL) {\r
+ PyObject *formatExc = PyDict_GetItemString(PyModule_GetDict(tracebackModule), "format_exception");\r
+ if (formatExc != NULL) {\r
+ PyObject *args = PyTuple_Pack(3, exceptionType, exception, traceback);\r
+ PyObject *message = PyObject_CallObject(formatExc, args);\r
+ if (message != NULL) {\r
+ PyObject *emptyStr = PyUnicode_FromString("");\r
+ PyObject *joined = PyUnicode_Join(emptyStr, message);\r
+ char *messageStr = PyUnicode_AsUTF8(joined);\r
+ throwPythonException(env, messageStr);\r
+ Py_DECREF(joined);\r
+ Py_DECREF(emptyStr);\r
+ Py_DECREF(message);\r
+ }\r
+ else {\r
+ throwPythonException(env, "Internal error, no message");\r
+ }\r
+ Py_DECREF(args);\r
+ Py_DECREF(formatExc);\r
+ }\r
+ else {\r
+ throwPythonException(env, "Internal error, no format_exc function");\r
+ }\r
+ Py_DECREF(tracebackModule);\r
+ }\r
+ else {\r
+ throwPythonException(env, "Internal error, no traceback module");\r
+ }\r
+ }\r
}\r
\r
(*env)->ReleaseStringUTFChars(env, statement, utfchars);\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0;\r
}\r
\r
if (!PyUnicode_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a string");\r
+ throwPythonException(env, "Python variable not a string");\r
return 0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0;\r
}\r
\r
if (!PySequence_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence");\r
+ throwPythonException(env, "Python variable not a sequence");\r
return 0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0;\r
}\r
\r
if (!PyBool_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a boolean");\r
+ throwPythonException(env, "Python variable not a boolean");\r
return 0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0;\r
}\r
\r
if (!PySequence_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence");\r
+ throwPythonException(env, "Python variable not a sequence");\r
return 0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0;\r
}\r
\r
if (!PyLong_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not an integer");\r
+ throwPythonException(env, "Python variable not an integer");\r
return 0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return NULL;\r
}\r
\r
if (!PySequence_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence");\r
+ throwPythonException(env, "Python variable not a sequence");\r
return NULL;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return NULL;\r
}\r
\r
if (!PySequence_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence");\r
+ throwPythonException(env, "Python variable not a sequence");\r
return NULL;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return 0.0;\r
}\r
\r
if (!PyFloat_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a float");\r
+ throwPythonException(env, "Python variable not a float");\r
return 0.0;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return NULL;\r
}\r
\r
if (!PySequence_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence");\r
+ throwPythonException(env, "Python variable not a sequence");\r
return NULL;\r
}\r
\r
PyObject *module = (PyObject*)contextID;\r
\r
if (!hasNumpy) {\r
- throwException(env, RUNTIME_EXCEPTION, "Importing numpy failed");\r
+ throwPythonException(env, "Importing numpy failed");\r
return NULL;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return NULL;\r
}\r
\r
if (!PyArray_Check(value)) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not an ndarray");\r
+ throwPythonException(env, "Python variable not an ndarray");\r
return NULL;\r
}\r
\r
if (PyArray_TYPE((PyArrayObject*)value) != NPY_DOUBLE) {\r
- throwException(env, RUNTIME_EXCEPTION, "Only ndarrays of type double are supported");\r
+ throwPythonException(env, "Only ndarrays of type double are supported");\r
return NULL;\r
}\r
\r
\r
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);\r
if (value == NULL) {\r
- throwException(env, RUNTIME_EXCEPTION, "Python variable not found");\r
+ throwPythonException(env, "Python variable not found");\r
return NULL;\r
}\r
\r