return throwException( env, ILLEGAL_ARGUMENT_EXCEPTION, message );\r
}\r
\r
+PyObject* getModule(jlong contextID) {\r
+ return PyState_FindModule((PyModuleDef*) contextID);\r
+// return PyImport_AddModule("__main__");\r
+}\r
+\r
int moduleCount = 0;\r
int initCalled = 0;\r
int hasNumpy = 0;\r
\r
+static JNIEnv *currentEnv = NULL;\r
+jobject sclWriter = NULL;\r
+\r
+static PyObject *\r
+writeToSCL(PyObject *self, PyObject *args)\r
+{\r
+ if (currentEnv != NULL && sclWriter != NULL) {\r
+ JNIEnv *env = currentEnv;\r
+\r
+ Py_UNICODE *what;\r
+ Py_ssize_t length;\r
+ if (!PyArg_ParseTuple(args, "u#", &what, &length))\r
+ return NULL;\r
+\r
+ {\r
+ jclass writerClass = (*env)->FindClass(env, WRITER_CLASS);\r
+ jmethodID writeMethod = (*env)->GetMethodID(env, writerClass, "write", "([CII)V");\r
+ jcharArray chars = (*env)->NewCharArray(env, (jsize)length);\r
+\r
+ (*env)->SetCharArrayRegion(env, chars, 0, length, what);\r
+ (*env)->CallVoidMethod(env, sclWriter, writeMethod, chars, 0, length);\r
+ }\r
+ }\r
+\r
+ return Py_BuildValue("");\r
+}\r
+\r
+static PyMethodDef sclWriterMethods[] = {\r
+ {"write", writeToSCL, METH_VARARGS, "Write something."},\r
+ {NULL, NULL, 0, NULL}\r
+};\r
+\r
+\r
JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_createContextImpl(JNIEnv *env, jobject thisObj) {\r
char name[16];\r
\r
Py_Initialize();\r
initCalled = 1;\r
\r
+ {\r
+ static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "sclwriter", NULL, -1, sclWriterMethods, };\r
+ PyObject *m = PyModule_Create(&moduledef);\r
+\r
+ if (m == NULL) throwException(env, PYTHON_EXCEPTION, "Failed to create SCL writer module");\r
+ PySys_SetObject("stdout", m);\r
+ PySys_SetObject("stderr", m);\r
+ }\r
+\r
hasNumpy = _import_array();\r
hasNumpy = hasNumpy != -1;\r
}\r
sprintf(name, "SCL_%d", ++moduleCount);\r
\r
{\r
- PyObject *module = PyModule_New(name);\r
- PyObject *main = PyImport_AddModule("__main__");\r
+ PyObject *module;\r
+ PyModuleDef *modDef = malloc(sizeof(PyModuleDef));\r
+ memset(modDef, 0, sizeof(PyModuleDef));\r
+ modDef->m_name = strdup(name);\r
+ modDef->m_doc = NULL;\r
+ modDef->m_size = -1;\r
\r
- PyDict_Merge(PyModule_GetDict(module), PyModule_GetDict(main), 0);\r
+ module = PyModule_Create(modDef);\r
+ Py_INCREF(module);\r
+ PyState_AddModule(module, modDef);\r
\r
- return (jlong)module;\r
+ {\r
+ PyObject *builtin = PyImport_AddModule("builtins");\r
+ PyObject *dict = PyModule_GetDict(module);\r
+ PyDict_SetItemString(dict, "__builtin__", builtin);\r
+ PyDict_SetItemString(dict, "__builtins__", builtin);\r
+\r
+ return (jlong)modDef;\r
+ }\r
}\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_deleteContextImpl(JNIEnv *env, jobject thisObj, jlong contextID) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyModuleDef *modDef = (PyModuleDef*)contextID;\r
+ PyObject *module = PyState_FindModule(modDef);\r
Py_XDECREF(module);\r
+ PyState_RemoveModule(modDef);\r
+ free((char*)modDef->m_name);\r
+ free(modDef);\r
}\r
\r
PyObject *getPythonBool(jboolean value) {\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonBooleanVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jboolean value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonBool(value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonBooleanArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jbooleanArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonBooleanList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonLongVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jlong value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = PyLong_FromLongLong(value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonIntegerArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jintArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonIntegerList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonLongArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jlongArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonLongList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonDoubleVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jdouble value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = PyFloat_FromDouble(value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonFloatArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jfloatArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonFloatList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonDoubleArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jdoubleArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonDoubleList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonStringVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jstring value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonString(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonStringArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobjectArray value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonStringList(env, value);\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonNDArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobject value) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
if (!hasNumpy) {\r
throwPythonException(env, "Importing numpy failed");\r
}\r
\r
JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonVariantVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobject value, jobject binding) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
PyObject *val = getPythonObject(env, value, binding);\r
}\r
\r
JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring statement) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
const char *utfchars = (*env)->GetStringUTFChars(env, statement, NULL);\r
\r
PyErr_Clear();\r
{\r
+ jclass sclReportingWriterClass = (*env)->FindClass(env, SCL_REPORTING_WRITER_CLASS);\r
+ jmethodID constructor = (*env)->GetMethodID(env, sclReportingWriterClass, "<init>", "()V");\r
+ jmethodID flushMethod = (*env)->GetMethodID(env, sclReportingWriterClass, "flush", "()V");\r
+\r
PyObject *globals;\r
\r
globals = PyModule_GetDict(module);\r
\r
+ currentEnv = env;\r
+ if (sclReportingWriterClass && constructor)\r
+ sclWriter = (*env)->NewObject(env, sclReportingWriterClass, constructor);\r
+ else\r
+ sclWriter = NULL;\r
+\r
{\r
PyObject *result = PyRun_String(utfchars, Py_file_input, globals, globals);\r
\r
\r
(*env)->ReleaseStringUTFChars(env, statement, utfchars);\r
\r
+ if (sclWriter != NULL) {\r
+ (*env)->CallVoidMethod(env, sclWriter, flushMethod);\r
+ }\r
+\r
+ currentEnv = NULL;\r
+ sclWriter = NULL;\r
+\r
return result != NULL ? 0 : 1;\r
}\r
}\r
}\r
\r
JNIEXPORT jstring JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonStringVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jobjectArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonStringArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jboolean JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonBooleanVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jbooleanArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonBooleanArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonLongVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jintArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonIntegerArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jlongArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonLongArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jdouble JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonDoubleVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jdoubleArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonDoubleArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonNDArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
if (!hasNumpy) {\r
throwPythonException(env, "Importing numpy failed");\r
}\r
\r
JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariantVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
\r
}\r
\r
JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariableTypeImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
PyObject *dict = PyModule_GetDict(module);\r
\r
PyObject *pythonName = getPythonString(env, variableName);\r
}\r
\r
JNIEXPORT jobjectArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariableNamesImpl(JNIEnv *env, jobject thisObj, jlong contextID) {\r
- PyObject *module = (PyObject*)contextID;\r
+ PyObject *module = getModule(contextID);\r
PyObject *dict = PyModule_GetDict(module);\r
\r
PyObject *keys = PyDict_Keys(dict);\r