-// 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
// Author : Anthony GEAY (EDF R&D)
#include "SALOME_CPythonHelper.hxx"
+#include "PythonCppUtils.hxx"
+
+SALOME_CPythonHelper *SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE = nullptr;
#if PY_VERSION_HEX < 0x03050000
static char*
}
#endif
+SALOME_CPythonHelper *SALOME_CPythonHelper::Singleton()
+{
+ if(!_CPYTHONHELPER_INSTANCE)
+ _CPYTHONHELPER_INSTANCE = new SALOME_CPythonHelper;
+ return _CPYTHONHELPER_INSTANCE;
+}
+
+void SALOME_CPythonHelper::KillSingleton()
+{
+ delete _CPYTHONHELPER_INSTANCE;
+ _CPYTHONHELPER_INSTANCE = nullptr;
+}
+
void SALOME_CPythonHelper::initializePython(int argc, char *argv[])
{
Py_Initialize();
+#if PY_VERSION_HEX < 0x03070000
PyEval_InitThreads();
+#endif
wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
for (int i = 0; i < argc; i++)
changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
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;
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);
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);
return ret;
}
+
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);
}