1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : PyInterp_base.cxx
8 // Author : Christian CAREMOLI, Paul RASCLE, EDF
17 #include <cStringIO.h>
21 #include "PyInterp_base.h"
22 #include "utilities.h"
29 static int MYDEBUG = 0;
30 static int MYPYDEBUG = 0;
32 static int MYDEBUG = 0;
33 static int MYPYDEBUG = 0;
37 static QMutex myMutex(true);
40 PyLockWrapper::PyLockWrapper(PyThreadState* theThreadState):
41 myThreadState(theThreadState),
42 mySaveThreadState(PyInterp_base::_gtstate)
45 mySaveThreadState = PyThreadState_Swap(myThreadState);
49 PyLockWrapper::~PyLockWrapper(){
50 PyThreadState_Swap(mySaveThreadState);
55 ThreadLock::ThreadLock(QMutex* theMutex, const char* theComment):
59 if(MYDEBUG && myComment != "") MESSAGE(" ThreadLock "<<this<<"::"<<myMutex<<" - "<<myComment<<" - "<<myMutex->locked());
64 ThreadLock::~ThreadLock(){
65 if(MYDEBUG && myComment != "") MESSAGE("~ThreadLock "<<this<<"::"<<myMutex<<" - "<<myComment);
71 return myMutex.locked();
75 ThreadLock GetPyThreadLock(const char* theComment){
76 return ThreadLock(&myMutex,theComment);
83 if(MYPYDEBUG) MESSAGE("~PyReleaseLock()");
89 PyLockWrapper PyInterp_base::GetLockWrapper(){
94 // main python interpreter
96 PyThreadState *PyInterp_base::_gtstate = 0; // force 0 before execution
97 int PyInterp_base::_argc = 1;
98 char* PyInterp_base::_argv[] = {""};
100 PyObject *PyInterp_base::builtinmodule = NULL;
101 PyObject *PyInterp_base::salome_shared_modules_module = NULL;
105 * basic constructor here : herited classes constructors must call initalize() method
108 PyInterp_base::PyInterp_base(): _tstate(0), _vout(0), _verr(0), _g(0), _atFirst(true)
110 if(MYPYDEBUG) MESSAGE("PyInterp_base::PyInterp_base() - this = "<<this);
113 PyInterp_base::~PyInterp_base()
115 if(MYPYDEBUG) MESSAGE("PyInterp_base::~PyInterp_base() - this = "<<this);
116 PyLockWrapper aLock(_tstate);
117 //Py_EndInterpreter(_tstate);
122 * Must be called by herited classes constructors. initialize() calls virtuals methods
123 * initstate & initcontext, not defined here in base class. initstate & initcontext methods
124 * must be implemented in herited classes, following the Python interpreter policy
125 * (mono or multi interpreter...).
127 void PyInterp_base::initialize()
129 _history.clear(); // start a new list of user's commands
130 _ith = _history.begin();
133 PyReleaseLock aReleaseLock;
134 Py_Initialize(); // Initialize the interpreter
135 PyEval_InitThreads(); // Initialize and acquire the global interpreter lock
136 PySys_SetArgv(_argc,_argv); // initialize sys.argv
137 _gtstate = PyThreadState_Get();
140 * salome_shared_modules should be imported only once
142 salome_shared_modules_module = PyImport_ImportModule("salome_shared_modules");
143 if(!salome_shared_modules_module)
145 INFOS("PyInterp_base::initialize() - salome_shared_modules_module == NULL");
150 // Here the global lock is released
151 if(MYPYDEBUG) MESSAGE("PyInterp_base::initialize() - this = "<<this<<"; _gtstate = "<<_gtstate);
153 // The lock will be acquired in initState. Make provision to release it on exit
154 PyReleaseLock aReleaseLock;
159 // used to interpret & compile commands
160 PyObjWrapper m(PyImport_ImportModule("codeop"));
162 INFOS("PyInterp_base::initialize() - PyImport_ImportModule('codeop') failed");
168 // Create cStringIO to capture stdout and stderr
170 PycStringIO = (PycStringIO_CAPI *)xxxPyCObject_Import("cStringIO", "cStringIO_CAPI");
171 _vout = PycStringIO->NewOutput(128);
172 _verr = PycStringIO->NewOutput(128);
174 // All the initRun outputs are redirected to the standard output (console)
179 string PyInterp_base::getbanner()
181 string aBanner("Python ");
182 aBanner = aBanner + Py_GetVersion() + " on " + Py_GetPlatform() ;
183 aBanner = aBanner + "\ntype help to get general information on environment\n";
188 int PyInterp_base::initRun()
190 PySys_SetObject("stderr",_verr);
191 PySys_SetObject("stdout",_vout);
193 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
194 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
196 PyObject *m = PyImport_GetModuleDict();
198 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
199 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
201 if(MYPYDEBUG) MESSAGE("PyInterp_base::initRun() - this = "<<this<<"; _verr = "<<_verr<<"; _vout = "<<_vout);
207 * This function compiles a string (command) and then evaluates it in the dictionnary
208 * context if possible.
211 * 1 : incomplete text
212 * 0 : complete text executed with success
214 int compile_command(const char *command,PyObject *context)
216 PyObject *m = PyImport_AddModule("codeop");
217 if(!m){ // Fatal error. No way to go on.
221 PyObjWrapper v(PyObject_CallMethod(m,"compile_command","s",command));
223 // Error encountered. It should be SyntaxError,
224 //so we don't write out traceback
225 PyObjWrapper exception, value, tb;
226 PyErr_Fetch(&exception, &value, &tb);
227 PyErr_NormalizeException(&exception, &value, &tb);
228 PyErr_Display(exception, value, NULL);
230 }else if (v == Py_None){
231 // Incomplete text we return 1 : we need a complete text to execute
234 // Complete and correct text. We evaluate it.
235 PyObjWrapper r(PyEval_EvalCode(v,context,context));
237 // Execution error. We return -1
241 // The command has been successfully executed. Return 0
247 int PyInterp_base::run(const char *command)
251 ret = simpleRun("from Help import *");
256 ret = simpleRun("import salome");
263 return simpleRun(command);
267 int PyInterp_base::simpleRun(const char *command)
269 if( !_atFirst && strcmp(command,"") != 0 ) {
270 _history.push_back(command);
271 _ith = _history.end();
274 // We come from C++ to enter Python world
275 // We need to acquire the Python global lock
276 PyLockWrapper aLock(_tstate);
278 // Reset redirected outputs before treatment
279 PySys_SetObject("stderr",_verr);
280 PySys_SetObject("stdout",_vout);
282 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
283 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
285 int ier = compile_command(command,_g);
287 // Outputs are redirected on standards outputs (console)
288 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
289 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
294 const char * PyInterp_base::getPrevious()
296 if(_ith != _history.begin()){
298 return (*_ith).c_str();
301 return BEGIN_HISTORY_PY;
305 const char * PyInterp_base::getNext()
307 if(_ith != _history.end()){
310 if (_ith == _history.end())
311 return TOP_HISTORY_PY;
313 return (*_ith).c_str();
317 string PyInterp_base::getverr(){
318 PyLockWrapper aLock(_tstate);
319 PyObjWrapper v(PycStringIO->cgetvalue(_verr));
320 string aRet(PyString_AsString(v));
325 string PyInterp_base::getvout(){
326 PyLockWrapper aLock(_tstate);
327 PyObjWrapper v(PycStringIO->cgetvalue(_vout));
328 string aRet(PyString_AsString(v));