Salome HOME
[EDF30399] : Steer directory hosting replay files
[modules/kernel.git] / src / Container / SALOME_CPythonHelper.cxx
1 // Copyright (C) 2019-2024  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOME_CPythonHelper.hxx"
22 #include "PythonCppUtils.hxx"
23
24 SALOME_CPythonHelper *SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE = nullptr;
25
26 #if PY_VERSION_HEX < 0x03050000
27 static char*
28 Py_EncodeLocale(const wchar_t *arg, size_t *size)
29 {
30   return _Py_wchar2char(arg, size);
31 }
32 static wchar_t*
33 Py_DecodeLocale(const char *arg, size_t *size)
34 {
35   return _Py_char2wchar(arg, size);
36 }
37 #endif
38
39 SALOME_CPythonHelper *SALOME_CPythonHelper::Singleton()
40 {
41   if(!_CPYTHONHELPER_INSTANCE)
42     _CPYTHONHELPER_INSTANCE = new SALOME_CPythonHelper;
43   return _CPYTHONHELPER_INSTANCE;
44 }
45
46 void SALOME_CPythonHelper::KillSingleton()
47 {
48   delete _CPYTHONHELPER_INSTANCE;
49   _CPYTHONHELPER_INSTANCE = nullptr;
50 }
51
52 void SALOME_CPythonHelper::initializePython(int argc, char *argv[])
53 {
54   Py_Initialize();
55 #if PY_VERSION_HEX < 0x03070000
56   PyEval_InitThreads();
57 #endif
58   wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
59   for (int i = 0; i < argc; i++)
60     changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
61   PySys_SetArgv(argc, changed_argv);
62   PyObject *mainmod(PyImport_AddModule("__main__"));
63   _globals=PyModule_GetDict(mainmod);
64   if(PyDict_GetItemString(_globals, "__builtins__") == NULL)
65     {
66       PyObject *bimod(PyImport_ImportModule("__builtin__"));
67       if (bimod == NULL || PyDict_SetItemString(_globals, "__builtins__", bimod) != 0)
68         Py_FatalError("can't add __builtins__ to __main__");
69       Py_XDECREF(bimod);
70     }
71   _locals=PyDict_New();
72   PyObject *tmp(PyList_New(0));
73   _pickler=PyImport_ImportModuleLevel(const_cast<char *>("pickle"),_globals,_locals,tmp,0);
74   _subprocess=PyImport_ImportModuleLevel(const_cast<char *>("subprocess"),_globals,_locals,tmp,0);
75   PyObject *socket(PyImport_ImportModuleLevel(const_cast<char *>("socket"),_globals,_locals,tmp,0));
76   PyDict_SetItemString(_globals,"sp",_subprocess);
77   PyDict_SetItemString(_globals,"socket",socket);
78 }
79
80 void SALOME_CPythonHelper::allowPythonCallsFromDifferentThread() const
81 {
82 #if PY_VERSION_HEX < 0x03070000
83   PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
84 #endif
85   PyEval_SaveThread(); /* Release the thread state */
86 }
87
88 void SALOME_CPythonHelper::registerToSalomePiDict(const std::string& processName, long pid) const
89 {
90   AutoGIL agil;
91   PyObject *mod(PyImport_ImportModule("addToKillList"));//new value
92   if(!mod)
93     return;
94   PyObject *meth(PyObject_GetAttrString(mod,"addToKillList"));//new value
95   if(!meth)
96     { Py_XDECREF(mod); return ; }
97   PyObject *args(PyTuple_New(2));
98   PyTuple_SetItem(args,0,PyLong_FromLong(pid));
99   PyTuple_SetItem(args,1,PyUnicode_FromString(processName.c_str()));
100   PyObject *res(PyObject_CallObject(meth,args));
101   Py_XDECREF(args);
102   Py_XDECREF(res);
103   Py_XDECREF(meth);
104   Py_XDECREF(mod);
105 }
106
107 std::vector<long> SALOME_CPythonHelper::evalVL(const std::string& pyCode) const
108 {
109   AutoGIL agil;
110   PyObject* code(Py_CompileString(pyCode.c_str(),"evalVL.py", Py_eval_input));
111   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
112   Py_DECREF(code);
113   Py_ssize_t n(PyList_Size(res));
114   std::vector<long> ret(n);
115   for(auto i = 0; i<n; ++i)
116     {
117       PyObject *elt(PyList_GetItem(res,i));//borrowed
118       ret[i]=PyLong_AsLong(elt);
119     }
120   Py_DECREF(res);
121   return ret;
122 }
123
124 std::string SALOME_CPythonHelper::evalS(const std::string& pyCode) const
125 {
126   AutoGIL agil;
127   PyObject* code(Py_CompileString(pyCode.c_str(),"evalS.py", Py_eval_input));
128   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
129   Py_DECREF(code);
130   std::string ret(PyUnicode_AsUTF8(res));
131   Py_DECREF(res);
132   return ret;
133 }
134
135
136 SALOME_CPythonHelper::~SALOME_CPythonHelper()
137 {
138   // _globals is borrowed ref -> do nothing
139
140   /*if(_locals){ auto refcount_locals = Py_REFCNT(_locals); }*/
141   AutoGIL agil;
142   Py_XDECREF(_locals);
143   Py_XDECREF(_pickler);
144 }