X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.pythonlink.win32.x86_64%2Fsrc%2Fsclpy.c;h=1592a2f56de443ddc298ad847a17f48175e04b02;hb=8885425046e0f89893c0e1ee0fe5c27948dcd2be;hp=428a45a35667fe34fcf70b0f0baae0f465c53557;hpb=1ea9a623d145875880b543e8eea62e801561fba9;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 428a45a..1592a2f 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -48,10 +48,10 @@ static PyObject * writeToSCL(PyObject *self, PyObject *args) { if (currentEnv != NULL && sclWriter != NULL) { - JNIEnv *env = currentEnv; - Py_UNICODE *what; Py_ssize_t length; + JNIEnv *env = currentEnv; + if (!PyArg_ParseTuple(args, "u#", &what, &length)) Py_RETURN_NONE; @@ -75,30 +75,62 @@ writeToSCL(PyObject *self, PyObject *args) Py_RETURN_NONE; } +static PyObject * +flushSCL(PyObject *self, PyObject *args) +{ + if (currentEnv != NULL && sclWriter != NULL) { + JNIEnv *env = currentEnv; + PyThreadState *my_ts = PyThreadState_Get(); + if (my_ts != main_ts) { + // TODO: Process calls from other threads + Py_RETURN_NONE; + } + + { + jclass writerClass = (*env)->FindClass(env, WRITER_CLASS); + jmethodID flushMethod = (*env)->GetMethodID(env, writerClass, "flush", "()V"); + + Py_BEGIN_ALLOW_THREADS + (*env)->CallVoidMethod(env, sclWriter, flushMethod); + Py_END_ALLOW_THREADS + } + } + + Py_RETURN_NONE; +} + static PyMethodDef sclWriterMethods[] = { {"write", writeToSCL, METH_VARARGS, "Write something."}, + {"flush", flushSCL, METH_VARARGS, "Flush output."}, {NULL, NULL, 0, NULL} }; +JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_initializePython(JNIEnv *env, jobject thisObj, jobject writer) { + Py_Initialize(); -JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_createContextImpl(JNIEnv *env, jobject thisObj) { - char name[16]; + { + static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "sclwriter", NULL, -1, sclWriterMethods, }; + PyObject *m = PyModule_Create(&moduledef); - if (!main_ts) { - Py_Initialize(); + sclWriter = (*env)->NewGlobalRef(env, writer); + + if (m == NULL) throwException(env, PYTHON_EXCEPTION, "Failed to create SCL writer module"); + + PySys_SetObject("stdout", m); + PySys_SetObject("stderr", m); + } + + hasNumpy = _import_array(); + hasNumpy = hasNumpy != -1; - { - static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "sclwriter", NULL, -1, sclWriterMethods, }; - PyObject *m = PyModule_Create(&moduledef); + main_ts = PyEval_SaveThread(); +} - if (m == NULL) throwException(env, PYTHON_EXCEPTION, "Failed to create SCL writer module"); - PySys_SetObject("stdout", m); - PySys_SetObject("stderr", m); - } +JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_createContextImpl(JNIEnv *env, jobject thisObj) { + char name[16]; - hasNumpy = _import_array(); - hasNumpy = hasNumpy != -1; - main_ts = PyEval_SaveThread(); + if (!main_ts) { + return 0; } sprintf(name, "SCL_%d", ++moduleCount); @@ -921,6 +953,37 @@ Java_org_simantics_pythonlink_PythonContext_setPythonVariantVariableImpl( PyEval_SaveThread(); } +static PyObject *getExceptionMessage(PyObject *exceptionType, PyObject *exception, PyObject *traceback) { + PyObject *formatExc = NULL, *args = NULL; + PyObject *tracebackModule = PyImport_ImportModule("traceback"); + if (!tracebackModule) { + return NULL; + } + + if (exception && traceback) { + formatExc = PyDict_GetItemString(PyModule_GetDict(tracebackModule), "format_exception"); + args = PyTuple_Pack(3, exceptionType, exception, traceback); + } + else if (exception) { + formatExc = PyDict_GetItemString(PyModule_GetDict(tracebackModule), "format_exception_only"); + args = PyTuple_Pack(2, exceptionType, exception); + } + + Py_DECREF(tracebackModule); + + if (formatExc != NULL && args != NULL) { + PyObject *result = PyObject_CallObject(formatExc, args); + Py_XDECREF(args); + Py_XDECREF(formatExc); + return result; + } + else { + Py_XDECREF(args); + Py_XDECREF(formatExc); + return NULL; + } +} + JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl( JNIEnv *env, jobject thisObj, jlong contextID, jstring statement) { @@ -931,63 +994,38 @@ Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl( { PyObject *module = getModule(contextID); - jclass sclReportingWriterClass = (*env)->FindClass(env, SCL_REPORTING_WRITER_CLASS); - jmethodID constructor = (*env)->GetMethodID(env, sclReportingWriterClass, "", "()V"); - jmethodID flushMethod = (*env)->GetMethodID(env, sclReportingWriterClass, "flush", "()V"); - PyObject *globals; globals = PyModule_GetDict(module); currentEnv = env; - if (sclReportingWriterClass && constructor) - sclWriter = (*env)->NewObject(env, sclReportingWriterClass, constructor); - else - sclWriter = NULL; { PyObject *result = PyRun_String(utfchars, Py_file_input, globals, globals); PyObject *exceptionType = PyErr_Occurred(); if (exceptionType != NULL) { - PyObject *exception, *traceback; + PyObject *exception, *traceback, *message; 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 { - PyTypeObject - *ty = (PyTypeObject *)exceptionType; - throwPythonException( - env, ty ? ty->tp_name - : "Internal error, null exception type"); - } - 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"); - } + message = getExceptionMessage(exceptionType, exception, traceback); + 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 { + PyTypeObject + *ty = (PyTypeObject *)exceptionType; + throwPythonException( + env, ty ? ty->tp_name + : "Internal error, null exception type"); } + Py_XDECREF(exceptionType); Py_XDECREF(exception); Py_XDECREF(traceback); @@ -996,12 +1034,7 @@ Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl( PyEval_SaveThread(); (*env)->ReleaseStringUTFChars(env, statement, utfchars); - if (sclWriter != NULL) { - (*env)->CallVoidMethod(env, sclWriter, flushMethod); - } - currentEnv = NULL; - sclWriter = NULL; return result != NULL ? 0 : 1; }