Salome HOME
[EDF30399] : Steer directory hosting replay files
[modules/kernel.git] / src / Container / SALOME_CPythonHelper.cxx
index e536fe20525599641ec92bf5d6933164d5670502..8fae563f3a251b60c975a7c64ed90f0ec566db8c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2021  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2019-2024  CEA, EDF, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@
 // Author : Anthony GEAY (EDF R&D)
 
 #include "SALOME_CPythonHelper.hxx"
+#include "PythonCppUtils.hxx"
 
 SALOME_CPythonHelper *SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE = nullptr;
 
@@ -76,8 +77,17 @@ void SALOME_CPythonHelper::initializePython(int argc, char *argv[])
   PyDict_SetItemString(_globals,"socket",socket);
 }
 
+void SALOME_CPythonHelper::allowPythonCallsFromDifferentThread() const
+{
+#if PY_VERSION_HEX < 0x03070000
+  PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
+#endif
+  PyEval_SaveThread(); /* Release the thread state */
+}
+
 void SALOME_CPythonHelper::registerToSalomePiDict(const std::string& processName, long pid) const
 {
+  AutoGIL agil;
   PyObject *mod(PyImport_ImportModule("addToKillList"));//new value
   if(!mod)
     return;
@@ -96,6 +106,7 @@ void SALOME_CPythonHelper::registerToSalomePiDict(const std::string& processName
 
 std::vector<long> SALOME_CPythonHelper::evalVL(const std::string& pyCode) const
 {
+  AutoGIL agil;
   PyObject* code(Py_CompileString(pyCode.c_str(),"evalVL.py", Py_eval_input));
   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
   Py_DECREF(code);
@@ -112,6 +123,7 @@ std::vector<long> SALOME_CPythonHelper::evalVL(const std::string& pyCode) const
 
 std::string SALOME_CPythonHelper::evalS(const std::string& pyCode) const
 {
+  AutoGIL agil;
   PyObject* code(Py_CompileString(pyCode.c_str(),"evalS.py", Py_eval_input));
   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
   Py_DECREF(code);
@@ -126,7 +138,7 @@ SALOME_CPythonHelper::~SALOME_CPythonHelper()
   // _globals is borrowed ref -> do nothing
 
   /*if(_locals){ auto refcount_locals = Py_REFCNT(_locals); }*/
-  
+  AutoGIL agil;
   Py_XDECREF(_locals);
   Py_XDECREF(_pickler);
 }