return throwException( env, ILLEGAL_ARGUMENT_EXCEPTION, message );
}
+// Returns a borrowed reference.
PyObject* getModule(jlong contextID) {
return PyState_FindModule((PyModuleDef*) contextID);
// return PyImport_AddModule("__main__");
{NULL, NULL, 0, NULL}
};
-JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_initializePython(JNIEnv *env, jobject thisObj, jobject writer) {
+JNIEXPORT void JNICALL
+Java_org_simantics_pythonlink_PythonContext_initializePython(
+ JNIEnv *env, jobject thisObj, jobject writer) {
Py_Initialize();
{
- static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "sclwriter", NULL, -1, sclWriterMethods, };
- PyObject *m = PyModule_Create(&moduledef);
+ static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT, "sclwriter", NULL, -1, sclWriterMethods
+ };
+ PyObject *m = PyModule_Create(&moduledef);
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);
+ if (m == NULL) {
+ throwException(env, PYTHON_EXCEPTION,
+ "Failed to create SCL writer module");
+ } else {
+ PySys_SetObject("stdout", m);
+ PySys_SetObject("stderr", m);
+ Py_DECREF(m);
+ }
}
hasNumpy = _import_array();
modDef->m_size = -1;
module = PyModule_Create(modDef);
- Py_INCREF(module);
PyState_AddModule(module, modDef);
-
{
PyObject *builtin = PyImport_AddModule("builtins");
PyObject *dict = PyModule_GetDict(module);
+ Py_DECREF(module);
PyDict_SetItemString(dict, "__builtin__", builtin);
PyDict_SetItemString(dict, "__builtins__", builtin);
}
}
-JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_deleteContextImpl(JNIEnv *env, jobject thisObj, jlong contextID) {
+JNIEXPORT void JNICALL
+Java_org_simantics_pythonlink_PythonContext_deleteContextImpl(
+ JNIEnv *env, jobject thisObj, jlong contextID) {
PyModuleDef *modDef = (PyModuleDef*)contextID;
- PyObject *module;
PyEval_RestoreThread(main_ts);
- module = PyState_FindModule(modDef);
- Py_XDECREF(module);
PyState_RemoveModule(modDef);
free((char*)modDef->m_name);
free(modDef);
PyList_SetItem(result, i, getPythonString(env, (jstring)item));
}
else {
+ Py_INCREF(Py_None);
PyList_SetItem(result, i, Py_None);
}
}
jobject componentObject = (*env)->CallObjectMethod(env, binding, getComponent, object, i);
jobject componentBinding = (*env)->CallObjectMethod(env, binding, getComponentBinding, i);
- PyObject *item = getPythonObject(env, componentObject, componentBinding);
- PyDict_SetItem(result, getPythonString(env, fieldName), item);
+ PyObject
+ *key = getPythonString(env, fieldName),
+ *item = getPythonObject(env, componentObject, componentBinding);
+ PyDict_SetItem(result, key, item);
+ Py_DECREF(key);
+ Py_DECREF(item);
}
return result;
jobject item = (*env)->CallObjectMethod(env, binding, getMethod, object, i);
if (item != NULL)
PyList_SetItem(result, i, getPythonObject(env, item, componentBinding));
- else
+ else {
+ Py_INCREF(Py_None);
PyList_SetItem(result, i, Py_None);
+ }
}
return result;
for (i = 0; i < size; i++) {
jobject key = (*env)->GetObjectArrayElement(env, keys, i);
jobject item = (*env)->GetObjectArrayElement(env, values, i);
- PyDict_SetItem(result, getPythonObject(env, key, keyBinding), getPythonObject(env, item, valueBinding));
+ PyObject
+ *pkey = getPythonObject(env, key, keyBinding),
+ *pitem = getPythonObject(env, item, valueBinding);
+ PyDict_SetItem(result, pkey, pitem);
+ Py_DECREF(pkey);
+ Py_DECREF(pitem);
}
(*env)->DeleteLocalRef(env, keys);
}
}
+// Steals refs to name & value.
void setPythonVariable(PyObject *module, PyObject *name, PyObject *value) {
if (name && value) {
PyDict_SetItem(PyModule_GetDict(module), name, value);
{
PyObject *result = PyRun_String(utfchars, Py_file_input, globals, globals);
-
PyObject *exceptionType = PyErr_Occurred();
+
if (exceptionType != NULL) {
PyObject *exception, *traceback, *message;
PyErr_Fetch(&exceptionType, &exception, &traceback);
// Returns a borrowed reference.
static PyObject *getPythonValue(
JNIEnv *env, jlong contextID, jstring variableName) {
- PyObject *module = getModule(contextID);
+ PyObject *module = getModule(contextID);
PyObject *pythonName = getPythonString(env, variableName);
PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName);