1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
21 // File : PyInterp_base.cxx
22 // Author : Christian CAREMOLI, Paul RASCLE, EDF
30 #include "PyInterp_base.h" // this include must be first (see PyInterp_base.h)!
31 #include <cStringIO.h>
35 PyLockWrapper::PyLockWrapper(PyThreadState* theThreadState):
36 myThreadState(theThreadState),
39 #if defined(USE_GILSTATE)
40 if (myThreadState->interp == PyInterp_base::_interp) {
41 _savestate = PyGILState_Ensure();
43 PyEval_AcquireThread(myThreadState);
46 PyEval_AcquireThread(myThreadState);
50 PyLockWrapper::~PyLockWrapper()
52 #if defined(USE_GILSTATE)
53 if (myThreadState->interp == PyInterp_base::_interp) {
54 PyGILState_Release(_savestate);
56 PyEval_ReleaseThread(myThreadState);
59 PyEval_ReleaseThread(myThreadState);
71 PyLockWrapper PyInterp_base::GetLockWrapper(){
76 // main python interpreter (static attributes)
78 int PyInterp_base::_argc = 1;
79 char* PyInterp_base::_argv[] = {""};
81 PyObject *PyInterp_base::builtinmodule = NULL;
83 PyThreadState *PyInterp_base::_gtstate = NULL;
84 PyInterpreterState *PyInterp_base::_interp = NULL;
88 * basic constructor here : herited classes constructors must call initalize() method
91 PyInterp_base::PyInterp_base(): _tstate(0), _vout(0), _verr(0), _g(0), _atFirst(true)
95 PyInterp_base::~PyInterp_base()
101 * Must be called by herited classes constructors. initialize() calls virtuals methods
102 * initstate & initcontext, not defined here in base class. initstate & initcontext methods
103 * must be implemented in herited classes, following the Python interpreter policy
104 * (mono or multi interpreter...).
106 void PyInterp_base::initialize()
108 _history.clear(); // start a new list of user's commands
109 _ith = _history.begin();
112 // Here the global lock is released
116 PyLockWrapper aLock= GetLockWrapper();
120 // used to interpret & compile commands
121 PyObjWrapper m(PyImport_ImportModule("codeop"));
127 // Create cStringIO to capture stdout and stderr
129 if (PycStringIO) { // CTH11627 : additional check
130 _vout = PycStringIO->NewOutput(128);
131 _verr = PycStringIO->NewOutput(128);
134 // All the initRun outputs are redirected to the standard output (console)
138 void PyInterp_base::init_python()
141 if (Py_IsInitialized())
144 // Python is not initialized
145 Py_SetProgramName(_argv[0]);
146 Py_Initialize(); // Initialize the interpreter
147 PySys_SetArgv(_argc, _argv);
148 PyEval_InitThreads(); // Create (and acquire) the interpreter lock
149 _interp = PyThreadState_Get()->interp;
150 _gtstate = PyEval_SaveThread(); // Release global thread state
153 string PyInterp_base::getbanner()
155 // Should we take the lock ?
156 // PyEval_RestoreThread(_tstate);
157 string aBanner("Python ");
158 aBanner = aBanner + Py_GetVersion() + " on " + Py_GetPlatform() ;
159 aBanner = aBanner + "\ntype help to get general information on environment\n";
160 //PyEval_SaveThread();
165 int PyInterp_base::initRun()
167 PySys_SetObject("stderr",_verr);
168 PySys_SetObject("stdout",_vout);
170 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
171 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
173 //PyObject *m = PyImport_GetModuleDict();
175 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
176 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
183 * This function compiles a string (command) and then evaluates it in the dictionnary
184 * context if possible.
187 * 1 : incomplete text
188 * 0 : complete text executed with success
190 int compile_command(const char *command,PyObject *context)
192 PyObject *m = PyImport_AddModule("codeop");
193 if(!m){ // Fatal error. No way to go on.
197 PyObjWrapper v(PyObject_CallMethod(m,"compile_command","s",command));
199 // Error encountered. It should be SyntaxError,
200 //so we don't write out traceback
201 PyObjWrapper exception, value, tb;
202 PyErr_Fetch(&exception, &value, &tb);
203 PyErr_NormalizeException(&exception, &value, &tb);
204 PyErr_Display(exception, value, NULL);
206 }else if (v == Py_None){
207 // Incomplete text we return 1 : we need a complete text to execute
210 // Complete and correct text. We evaluate it.
211 //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
212 // PyObjWrapper r(PyEval_EvalCode(v,context,context));
214 PyObjWrapper r(PyEval_EvalCode((PyCodeObject *)(void *)v,context,context));
217 // Execution error. We return -1
221 // The command has been successfully executed. Return 0
227 int PyInterp_base::run(const char *command)
231 ret = simpleRun("from Help import *");
236 ret = simpleRun("import salome");
241 ret = simpleRun("salome.salome_init(0,1)");
248 return simpleRun(command);
252 int PyInterp_base::simpleRun(const char *command)
254 if( !_atFirst && strcmp(command,"") != 0 ) {
255 _history.push_back(command);
256 _ith = _history.end();
259 // We come from C++ to enter Python world
260 // We need to acquire the Python global lock
261 //PyLockWrapper aLock(_tstate); // san - lock is centralized now
263 // Reset redirected outputs before treatment
264 PySys_SetObject("stderr",_verr);
265 PySys_SetObject("stdout",_vout);
267 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
268 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
270 int ier = compile_command(command,_g);
272 // Outputs are redirected on standards outputs (console)
273 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
274 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
280 const char * PyInterp_base::getPrevious()
282 if(_ith != _history.begin()){
284 return (*_ith).c_str();
287 return BEGIN_HISTORY_PY;
291 const char * PyInterp_base::getNext()
293 if(_ith != _history.end()){
296 if (_ith == _history.end())
297 return TOP_HISTORY_PY;
299 return (*_ith).c_str();
303 string PyInterp_base::getverr(){
304 //PyLockWrapper aLock(_tstate);
305 PyObjWrapper v(PycStringIO->cgetvalue(_verr));
306 string aRet(PyString_AsString(v));
311 string PyInterp_base::getvout(){
312 //PyLockWrapper aLock(_tstate);
313 PyObjWrapper v(PycStringIO->cgetvalue(_vout));
314 string aRet(PyString_AsString(v));