Salome HOME
For future compatibility with python 3.9.
[modules/kernel.git] / src / Container / SALOME_CPythonHelper.cxx
1 // Copyright (C) 2019-2021  CEA/DEN, EDF R&D, 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
23 #if PY_VERSION_HEX < 0x03050000
24 static char*
25 Py_EncodeLocale(const wchar_t *arg, size_t *size)
26 {
27   return _Py_wchar2char(arg, size);
28 }
29 static wchar_t*
30 Py_DecodeLocale(const char *arg, size_t *size)
31 {
32   return _Py_char2wchar(arg, size);
33 }
34 #endif
35
36 void SALOME_CPythonHelper::initializePython(int argc, char *argv[])
37 {
38   Py_Initialize();
39 #if PY_VERSION_HEX < 0x03070000
40   PyEval_InitThreads();
41 #endif
42   wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
43   for (int i = 0; i < argc; i++)
44     changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
45   PySys_SetArgv(argc, changed_argv);
46   PyObject *mainmod(PyImport_AddModule("__main__"));
47   _globals=PyModule_GetDict(mainmod);
48   if(PyDict_GetItemString(_globals, "__builtins__") == NULL)
49     {
50       PyObject *bimod(PyImport_ImportModule("__builtin__"));
51       if (bimod == NULL || PyDict_SetItemString(_globals, "__builtins__", bimod) != 0)
52         Py_FatalError("can't add __builtins__ to __main__");
53       Py_XDECREF(bimod);
54     }
55   _locals=PyDict_New();
56   PyObject *tmp(PyList_New(0));
57   _pickler=PyImport_ImportModuleLevel(const_cast<char *>("pickle"),_globals,_locals,tmp,0);
58   _subprocess=PyImport_ImportModuleLevel(const_cast<char *>("subprocess"),_globals,_locals,tmp,0);
59   PyObject *socket(PyImport_ImportModuleLevel(const_cast<char *>("socket"),_globals,_locals,tmp,0));
60   PyDict_SetItemString(_globals,"sp",_subprocess);
61   PyDict_SetItemString(_globals,"socket",socket);
62 }
63
64 void SALOME_CPythonHelper::registerToSalomePiDict(const std::string& processName, long pid) const
65 {
66   PyObject *mod(PyImport_ImportModule("addToKillList"));//new value
67   if(!mod)
68     return;
69   PyObject *meth(PyObject_GetAttrString(mod,"addToKillList"));//new value
70   if(!meth)
71     { Py_XDECREF(mod); return ; }
72   PyObject *args(PyTuple_New(2));
73   PyTuple_SetItem(args,0,PyLong_FromLong(pid));
74   PyTuple_SetItem(args,1,PyUnicode_FromString(processName.c_str()));
75   PyObject *res(PyObject_CallObject(meth,args));
76   Py_XDECREF(args);
77   Py_XDECREF(res);
78   Py_XDECREF(meth);
79   Py_XDECREF(mod);
80 }
81
82 std::vector<long> SALOME_CPythonHelper::evalVL(const std::string& pyCode) const
83 {
84   PyObject* code(Py_CompileString(pyCode.c_str(),"evalVL.py", Py_eval_input));
85   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
86   Py_DECREF(code);
87   Py_ssize_t n(PyList_Size(res));
88   std::vector<long> ret(n);
89   for(auto i = 0; i<n; ++i)
90     {
91       PyObject *elt(PyList_GetItem(res,i));//borrowed
92       ret[i]=PyLong_AsLong(elt);
93     }
94   Py_DECREF(res);
95   return ret;
96 }
97
98 std::string SALOME_CPythonHelper::evalS(const std::string& pyCode) const
99 {
100   PyObject* code(Py_CompileString(pyCode.c_str(),"evalS.py", Py_eval_input));
101   PyObject *res(PyEval_EvalCode( code, _globals, _locals));
102   Py_DECREF(code);
103   std::string ret(PyUnicode_AsUTF8(res));
104   Py_DECREF(res);
105   return ret;
106 }
107
108 SALOME_CPythonHelper::~SALOME_CPythonHelper()
109 {
110   // _globals is borrowed ref -> do nothing
111   Py_XDECREF(_locals);
112   Py_XDECREF(_pickler);
113 }