X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.pythonlink.win32.x86_64%2Fsrc%2Fsclpy.c;h=b10b36f5d3c1103aeda3c6e7582ff44bdb6670b5;hb=c09eb1d21fa19556b14301ca70b622b8a2e1d080;hp=0db1e388c792b1909059d03895f1f77a8bea8e66;hpb=15304b7c3168ef89c47dcfcfb4cf34c72000a75f;p=simantics%2Fpython.git diff --git a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c index 0db1e38..b10b36f 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -24,6 +24,10 @@ jint throwException( JNIEnv *env, char *className, char *message ) return (*env)->ThrowNew( env, exClass, message ); } +jint throwPythonException( JNIEnv *env, char *message ) { + return throwException( env, PYTHON_EXCEPTION, message ); +} + jint throwIllegalArgumentException( JNIEnv *env, char *message ) { return throwException( env, ILLEGAL_ARGUMENT_EXCEPTION, message ); } @@ -442,6 +446,9 @@ PyObject *getPythonObject(JNIEnv *env, jobject value, jobject binding) { jclass untionBinding = (*env)->FindClass(env, UNIONBINDING_CLASS); jclass variantBinding = (*env)->FindClass(env, VARIANTBINDING_CLASS); + if (value == NULL) + return Py_None; + if ((*env)->IsInstanceOf(env, binding, booleanBinding)) { return getPythonBooleanObject(env, value, binding); } @@ -578,7 +585,7 @@ jobjectArray pythonSequenceAsStringArray(JNIEnv *env, PyObject *seq) { (*env)->SetObjectArrayElement(env, array, i, value); } else { - throwException(env, RUNTIME_EXCEPTION, "List item not a string"); + throwPythonException(env, "List item not a string"); return NULL; } } @@ -600,7 +607,7 @@ jdoubleArray pythonSequenceAsDoubleArray(JNIEnv *env, PyObject *seq) { (*env)->SetDoubleArrayRegion(env, array, i, 1, &value); } else { - throwException(env, RUNTIME_EXCEPTION, "List item not a floating point value"); + throwPythonException(env, "List item not a floating point value"); return NULL; } } @@ -659,7 +666,7 @@ jbooleanArray pythonSequenceAsBooleanArray(JNIEnv *env, PyObject *seq) { (*env)->SetBooleanArrayRegion(env, array, i, 1, &value); } else { - throwException(env, RUNTIME_EXCEPTION, "List item not a boolean"); + throwPythonException(env, "List item not a boolean"); return NULL; } } @@ -681,7 +688,7 @@ jintArray pythonSequenceAsIntegerArray(JNIEnv *env, PyObject *seq) { (*env)->SetIntArrayRegion(env, array, i, 1, &value); } else { - throwException(env, RUNTIME_EXCEPTION, "List item not an integer"); + throwPythonException(env, "List item not an integer"); return NULL; } } @@ -703,7 +710,7 @@ jlongArray pythonSequenceAsLongArray(JNIEnv *env, PyObject *seq) { (*env)->SetLongArrayRegion(env, array, i, 1, &value); } else { - throwException(env, RUNTIME_EXCEPTION, "List item not an integer"); + throwPythonException(env, "List item not an integer"); return NULL; } } @@ -726,7 +733,7 @@ jobject pythonArrayAsNDArray(JNIEnv *env, PyArrayObject *array) { int i; if (len > JAVA_MAXINT) { - throwException(env, RUNTIME_EXCEPTION, "Array too large"); + throwPythonException(env, "Array too large"); return NULL; } @@ -869,7 +876,7 @@ JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonNDAr PyObject *module = (PyObject*)contextID; if (!hasNumpy) { - throwException(env, RUNTIME_EXCEPTION, "Importing numpy failed"); + throwPythonException(env, "Importing numpy failed"); return; } @@ -906,11 +913,40 @@ JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_executePython PyObject *exceptionType = PyErr_Occurred(); if (exceptionType != NULL) { - PyObject *exception, *stackTrace; - char *message; - PyErr_Fetch(&exceptionType, &exception, &stackTrace); - message = PyUnicode_AsUTF8(exception); - throwException(env, RUNTIME_EXCEPTION, message); + PyObject *exception, *traceback; + PyErr_Fetch(&exceptionType, &exception, &traceback); + + { + PyObject *tracebackModule = PyImport_ImportModule("traceback"); + if (tracebackModule != NULL) { + PyObject *formatExc = PyDict_GetItemString(PyModule_GetDict(tracebackModule), "format_exception"); + if (formatExc != NULL) { + PyObject *args = PyTuple_Pack(3, exceptionType, exception, traceback); + PyObject *message = PyObject_CallObject(formatExc, args); + if (message != NULL) { + PyObject *emptyStr = PyUnicode_FromString(""); + PyObject *joined = PyUnicode_Join(emptyStr, message); + char *messageStr = PyUnicode_AsUTF8(joined); + throwPythonException(env, messageStr); + Py_DECREF(joined); + Py_DECREF(emptyStr); + Py_DECREF(message); + } + else { + throwPythonException(env, "Internal error, no message"); + } + Py_DECREF(args); + Py_DECREF(formatExc); + } + else { + throwPythonException(env, "Internal error, no format_exc function"); + } + Py_DECREF(tracebackModule); + } + else { + throwPythonException(env, "Internal error, no traceback module"); + } + } } (*env)->ReleaseStringUTFChars(env, statement, utfchars); @@ -927,12 +963,12 @@ JNIEXPORT jstring JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonS PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0; } if (!PyUnicode_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a string"); + throwPythonException(env, "Python variable not a string"); return 0; } @@ -949,12 +985,12 @@ JNIEXPORT jobjectArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPy PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0; } if (!PySequence_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence"); + throwPythonException(env, "Python variable not a sequence"); return 0; } @@ -971,12 +1007,12 @@ JNIEXPORT jboolean JNICALL Java_org_simantics_pythonlink_PythonContext_getPython PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0; } if (!PyBool_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a boolean"); + throwPythonException(env, "Python variable not a boolean"); return 0; } @@ -990,12 +1026,12 @@ JNIEXPORT jbooleanArray JNICALL Java_org_simantics_pythonlink_PythonContext_getP PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0; } if (!PySequence_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence"); + throwPythonException(env, "Python variable not a sequence"); return 0; } @@ -1012,12 +1048,12 @@ JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonLon PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0; } if (!PyLong_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not an integer"); + throwPythonException(env, "Python variable not an integer"); return 0; } @@ -1034,12 +1070,12 @@ JNIEXPORT jintArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPytho PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return NULL; } if (!PySequence_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence"); + throwPythonException(env, "Python variable not a sequence"); return NULL; } @@ -1056,12 +1092,12 @@ JNIEXPORT jlongArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPyth PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return NULL; } if (!PySequence_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence"); + throwPythonException(env, "Python variable not a sequence"); return NULL; } @@ -1078,12 +1114,12 @@ JNIEXPORT jdouble JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonD PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return 0.0; } if (!PyFloat_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a float"); + throwPythonException(env, "Python variable not a float"); return 0.0; } @@ -1100,12 +1136,12 @@ JNIEXPORT jdoubleArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPy PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return NULL; } if (!PySequence_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not a sequence"); + throwPythonException(env, "Python variable not a sequence"); return NULL; } @@ -1119,7 +1155,7 @@ JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonN PyObject *module = (PyObject*)contextID; if (!hasNumpy) { - throwException(env, RUNTIME_EXCEPTION, "Importing numpy failed"); + throwPythonException(env, "Importing numpy failed"); return NULL; } @@ -1128,17 +1164,17 @@ JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonN PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return NULL; } if (!PyArray_Check(value)) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not an ndarray"); + throwPythonException(env, "Python variable not an ndarray"); return NULL; } if (PyArray_TYPE((PyArrayObject*)value) != NPY_DOUBLE) { - throwException(env, RUNTIME_EXCEPTION, "Only ndarrays of type double are supported"); + throwPythonException(env, "Only ndarrays of type double are supported"); return NULL; } @@ -1156,7 +1192,7 @@ JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonV PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); if (value == NULL) { - throwException(env, RUNTIME_EXCEPTION, "Python variable not found"); + throwPythonException(env, "Python variable not found"); return NULL; }