From ed5759e22d06d82af4bc67e27239aa4e66310b8b Mon Sep 17 00:00:00 2001 From: Timo Korvola Date: Mon, 30 Jan 2017 16:27:13 +0200 Subject: [PATCH] All getters now use GIL as well. More macro madness. Hopefully locking is now OK. Change-Id: I3f487cc74154574032492b9dd5c4f46b27ee7a4e --- .../jnipython.dll | Bin 63488 -> 63488 bytes .../src/sclpy.c | 362 +++++------------- 2 files changed, 101 insertions(+), 261 deletions(-) diff --git a/org.simantics.pythonlink.win32.x86_64/jnipython.dll b/org.simantics.pythonlink.win32.x86_64/jnipython.dll index a605b4e9da45bd49bbe4119bef0885bb37f6b1c7..261bac38ed2f79d35f1a626e8e57ccc00aa9a418 100644 GIT binary patch delta 5542 zcmai24OCRs7C!ef7Y1Zx5UwGD4LB(D)KvTd6GIo^Rq=^tGWq<$KlGWUJsBfK2N_WD zhG9or_Li$KE&uapDu3R~#xOu7O~9};b7?)t&_bW@iKgE7oij6lXzQ`o{`NlS?0wEY z`|P{V8S)!_@*90h8>3Q3y=5VuzeW;mY9|H|@*X-SxjQX1SePng(AR{{!X#QEyew4E zyFzziF^v+ti`%=`LwsjY}gc-4w8}9|o?Tp_KuBMaLQXdq9ym_>7~VU3@^7I~7cCjzCCi z{PA?vsi@=48EU#&fyMa>S}Q*<_MYkp?RX?Q`sdAvd(Lo0O))1Zs^KJ>iaNrmBr5ux zE>J(b*>O8zog|vybF3b6(odZI4$VwkCSF}n>%nHPr zy)$aJ5a#$W-B%DgId+Zi>|;w%DpSlRmMTzmso18xV<#c$aNojr$>vISy~-4DUa^Wj z&m2pvVnZt1VXUGi>(%^i7)6p0W05nbBeOz3v+DkdrZFp1LohrWROt3B+4ZSI)8d^) zPV0san%ob|vAIk*z=ZPZYHDfsbdrUc^=4CLxi`}nCT<5Xb8k)7aVx%3l_sCs?sm__ zouW~F2Gb0Gzo*!$W9{x$GhEmAV+ytq?KLj$(T(S>W*Z;0-%xH<7K-yx)Hpc=!8f^z;30(&Vq1t7?j$XTM0;5V|)b%v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wbuLA0oTz#%zT`}bHo+(;S3OBW|mU7-w&ThT(spdpaJ{(|sYVSZS z*Ot;?s$JZDyghw$sj_;s?lg{j%^uskt z3Hb#`KYAg@Y>tySrf?h%#MGVH=Fn;0DNFWQbWlO~lex1nxs`G`|2TMh4a*~&{qVlL zn*Fzs;R!6d>%5>PV^0)rt1r0?YC!QciwHZIK$f@F(2#|iu|bKJ~vJ;!{G3pr+UoWwDO<8Y2L6~AYoDBck1Gkc@-?iNzWwpq?UTt$1w3WCmcrutJ! zsBpb{s9t!s2%9xjAff*+zFYIRo1QvyDC&bU?^STuLC6=ygt*`E@_(Uy6YaJ)=*FYN zx-&JF;x&8?C*#i7(@RH3nS8wqF#0+u?oDu4*s%mL@^9LI=L&1QhW%j7DEDJRnn5zP zy}QD`5u^1WeJvrs1ML8LKqJo*QUJOL>U9qP(gW=Q-3E=WBV-+@4rDw}$V|{)&>hg2 z3xt$`u7V;zA!GsQ2aaDIZw{ald+ZYZW z6+CC@(#7*;FQJ)@qqV+BDm4vgr{mY9^PBqf{?ewA;F0uXlQnRuZOL5vMC|}q+!bA* zEB|`6aO|&5qJ8mmgk+r}q!2s-8#M=V3b=?Qlr{+z_f3K!GWNN54SG`;gLtx mUt_<@=x+npWBa8*W;_~H1(|KhDWF@BiK}VnVPW@;UjGF$+oawA diff --git a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c index e767192..8de3c87 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -787,8 +787,8 @@ jobject pythonDictionaryAsMap(JNIEnv *env, PyObject *dict) { #define DEF_SETTER(typename, jtype, j2py) \ JNIEXPORT void JNICALL \ - Java_org_simantics_pythonlink_PythonContext_setPython \ - ##typename##VariableImpl( \ + Java_org_simantics_pythonlink_PythonContext_setPython##typename \ + ##VariableImpl( \ JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, \ jtype value) { \ PyObject *module = (PyObject*)contextID; \ @@ -910,269 +910,101 @@ Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl( } } -JNIEXPORT jstring JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonStringVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0; - } - - if (!PyUnicode_Check(value)) { - throwPythonException(env, "Python variable not a string"); - return 0; - } - - { - jstring result = pythonStringAsJavaString(env, value); - return result; - } -} - -JNIEXPORT jobjectArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonStringArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0; - } - - if (!PySequence_Check(value)) { - throwPythonException(env, "Python variable not a sequence"); - return 0; - } - - { - jobjectArray result = pythonSequenceAsStringArray(env, value); - return result; - } -} - -JNIEXPORT jboolean JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonBooleanVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0; - } - - if (!PyBool_Check(value)) { - throwPythonException(env, "Python variable not a boolean"); - return 0; - } - - return value == Py_True; -} - -JNIEXPORT jbooleanArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonBooleanArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0; - } - - if (!PySequence_Check(value)) { - throwPythonException(env, "Python variable not a sequence"); - return 0; - } - - { - jbooleanArray result = pythonSequenceAsBooleanArray(env, value); - return result; - } -} - -JNIEXPORT jlong JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonLongVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0; - } - - if (!PyLong_Check(value)) { - throwPythonException(env, "Python variable not an integer"); - return 0; - } - - { - jlong result = PyLong_AsLongLong(value); - return result; - } -} - -JNIEXPORT jintArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonIntegerArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return NULL; - } - - if (!PySequence_Check(value)) { - throwPythonException(env, "Python variable not a sequence"); - return NULL; - } - - { - jintArray result = pythonSequenceAsIntegerArray(env, value); - return result; - } -} - -JNIEXPORT jlongArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonLongArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return NULL; - } - - if (!PySequence_Check(value)) { - throwPythonException(env, "Python variable not a sequence"); - return NULL; - } - { - jlongArray result = pythonSequenceAsLongArray(env, value); - return result; - } -} - -JNIEXPORT jdouble JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonDoubleVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return 0.0; - } - - if (!PyFloat_Check(value)) { - throwPythonException(env, "Python variable not a float"); - return 0.0; - } +// Returns a borrowed reference. +static PyObject *getPythonValue( + JNIEnv *env, jlong contextID, jstring variableName) { + PyObject *module = (PyObject*)contextID; + PyObject *pythonName = getPythonString(env, variableName); + PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - { - jdouble result = PyFloat_AsDouble(value); - return result; - } + Py_DECREF(pythonName); + if (value == NULL) { + throwPythonException(env, "Python variable not found"); + } + return value; } -JNIEXPORT jdoubleArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonDoubleArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return NULL; - } - - if (!PySequence_Check(value)) { - throwPythonException(env, "Python variable not a sequence"); - return NULL; - } - - { - jdoubleArray result = pythonSequenceAsDoubleArray(env, value); - return result; - } -} +#define DEF_GETTER(typename, jtype, check, desc, py2j) \ + JNIEXPORT jtype JNICALL \ + Java_org_simantics_pythonlink_PythonContext_getPython##typename \ + ##VariableImpl( \ + JNIEnv *env, jobject thisObj, jlong contextID, \ + jstring variableName) { \ + jtype result = 0; \ + PyEval_RestoreThread(main_ts); \ + do { \ + PyObject *value = getPythonValue(env, contextID, variableName); \ + if (value == 0) break; \ + if (check(value)) { \ + result = py2j(env, value); \ + } else { \ + throwPythonException(env, "Python variable not " desc); \ + } \ + } while (0); \ + PyEval_SaveThread(); \ + return result; \ + } -JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonNDArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; +#define pythonBoolAsJboolean(env, value) ((value) == Py_True) +#define pythonLongAsJlong(env, value) PyLong_AsLongLong(value) +#define pythonFloatAsJdouble(env, value) PyFloat_AsDouble(value) + +DEF_GETTER(String, jstring, PyUnicode_Check, "a string", + pythonStringAsJavaString) +DEF_GETTER(StringArray, jobjectArray, PySequence_Check, "a sequence", + pythonSequenceAsStringArray) +DEF_GETTER(Boolean, jboolean, PyBool_Check, "a Boolean", pythonBoolAsJboolean) +DEF_GETTER(BooleanArray, jbooleanArray, PySequence_Check, "a sequence", + pythonSequenceAsBooleanArray) +DEF_GETTER(Long, jlong, PyLong_Check, "an integer", pythonLongAsJlong) +DEF_GETTER(IntegerArray, jintArray, PySequence_Check, "a sequence", + pythonSequenceAsIntegerArray) +DEF_GETTER(LongArray, jlongArray, PySequence_Check, "a sequence", + pythonSequenceAsLongArray) +DEF_GETTER(Double, jdouble, PyFloat_Check, "a float", pythonFloatAsJdouble) +DEF_GETTER(DoubleArray, jdoubleArray, PySequence_Check, "a sequence", + pythonSequenceAsDoubleArray) + +JNIEXPORT jobject JNICALL +Java_org_simantics_pythonlink_PythonContext_getPythonNDArrayVariableImpl( + JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { + jobject result = NULL; if (!hasNumpy) { throwPythonException(env, "Importing numpy failed"); return NULL; } - { - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return NULL; - } - + PyEval_RestoreThread(main_ts); + do { + PyObject *value = getPythonValue(env, contextID, variableName); + if (value == NULL) break; if (!PyArray_Check(value)) { throwPythonException(env, "Python variable not an ndarray"); - return NULL; - } - - if (PyArray_TYPE((PyArrayObject*)value) != NPY_DOUBLE) { - throwPythonException(env, "Only ndarrays of type double are supported"); - return NULL; + } else if (PyArray_TYPE((PyArrayObject*)value) != NPY_DOUBLE) { + throwPythonException( + env, "Only ndarrays of type double are supported"); + } else { + result = pythonArrayAsNDArray(env, (PyArrayObject *)value); } - - { - jobject result = pythonArrayAsNDArray(env, (PyArrayObject *)value); - return result; - } - } -} - -JNIEXPORT jobject JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariantVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - - PyObject *value = PyDict_GetItem(PyModule_GetDict(module), pythonName); - if (value == NULL) { - throwPythonException(env, "Python variable not found"); - return NULL; - } - - hasNumpy = _import_array() != -1; - - { - jobject result = pythonObjectAsObject(env, value); - return result; - } + } while (0); + PyEval_SaveThread(); + return result; } -JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariableTypeImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { - PyObject *module = (PyObject*)contextID; - PyObject *dict = PyModule_GetDict(module); +#define python_anything_goes(value) 1 - PyObject *pythonName = getPythonString(env, variableName); - - if (!PyDict_Contains(dict, pythonName)) { - return 0; - } +DEF_GETTER(Variant, jobject, python_anything_goes, "frabjous", + pythonObjectAsObject) +JNIEXPORT jint JNICALL +Java_org_simantics_pythonlink_PythonContext_getPythonVariableTypeImpl( + JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName) { + jint result = 0; + PyEval_RestoreThread(main_ts); { - PyObject *value = PyDict_GetItem(dict, pythonName); - - jint result; - + PyObject *value = getPythonValue(env, contextID, variableName); if (PyBool_Check(value)) result = 1; else if (PyLong_Check(value)) @@ -1191,28 +1023,36 @@ JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVari result = 8; else result = -1; - - return result; } + PyEval_SaveThread(); + return result; } -JNIEXPORT jobjectArray JNICALL Java_org_simantics_pythonlink_PythonContext_getPythonVariableNamesImpl(JNIEnv *env, jobject thisObj, jlong contextID) { - PyObject *module = (PyObject*)contextID; - PyObject *dict = PyModule_GetDict(module); +JNIEXPORT jobjectArray JNICALL +Java_org_simantics_pythonlink_PythonContext_getPythonVariableNamesImpl( + JNIEnv *env, jobject thisObj, jlong contextID) { + jobjectArray result = NULL; + PyEval_RestoreThread(main_ts); + { + PyObject *module = (PyObject*)contextID; + PyObject *dict = PyModule_GetDict(module); - PyObject *keys = PyDict_Keys(dict); - Py_ssize_t size = PyList_Size(keys); + PyObject *keys = PyDict_Keys(dict); + Py_ssize_t size = PyList_Size(keys); + Py_ssize_t i; - jobjectArray result = (*env)->NewObjectArray(env, (jsize)size, (*env)->FindClass(env, STRING_CLASS), NULL); + result = (*env)->NewObjectArray( + env, (jsize)size, (*env)->FindClass(env, STRING_CLASS), NULL); - Py_ssize_t i; - for (i = 0; i < size; i++) { - jstring javaName = pythonStringAsJavaString(env, PyList_GetItem(keys, i)); - (*env)->SetObjectArrayElement(env, result, (jint)i, javaName); - } - - Py_XDECREF(keys); + for (i = 0; i < size; i++) { + jstring javaName = pythonStringAsJavaString( + env, PyList_GetItem(keys, i)); + (*env)->SetObjectArrayElement(env, result, (jint)i, javaName); + } + Py_XDECREF(keys); + } + PyEval_SaveThread(); return result; } -- 2.47.1