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;
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);
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) {
{
PyObject *module = getModule(contextID);
- jclass sclReportingWriterClass = (*env)->FindClass(env, SCL_REPORTING_WRITER_CLASS);
- jmethodID constructor = (*env)->GetMethodID(env, sclReportingWriterClass, "<init>", "()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);
PyEval_SaveThread();
(*env)->ReleaseStringUTFChars(env, statement, utfchars);
- if (sclWriter != NULL) {
- (*env)->CallVoidMethod(env, sclWriter, flushMethod);
- }
-
currentEnv = NULL;
- sclWriter = NULL;
return result != NULL ? 0 : 1;
}