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