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
16 #include "PyInterp_base.h" // this include must be first (see PyInterp_base.h)!
17 #include <Container_init_python.hxx>
18 #include <cStringIO.h>
20 #include <utilities.h>
27 //static int MYDEBUG = 1;
28 //static int MYPYDEBUG = 1;
30 //static int MYDEBUG = 0;
31 //static int MYPYDEBUG = 0;
35 PyLockWrapper::PyLockWrapper(PyThreadState* theThreadState):
36 myThreadState(theThreadState),
37 mySaveThreadState(KERNEL_PYTHON::_gtstate)
40 mySaveThreadState = PyThreadState_Swap(myThreadState); // store previous current in save,
41 // set local in current
42 // if(MYDEBUG) MESSAGE(" PyLockWrapper "<<this<<" aqcuired: new thread state "<<myThreadState<<" ; old thread state "<<mySaveThreadState);
46 PyLockWrapper::~PyLockWrapper(){
47 PyThreadState_Swap(mySaveThreadState); // restore previous current (no need to get local,
48 PyEval_ReleaseLock(); // local thread state* already in _tstate
49 // if(MYDEBUG) MESSAGE(" PyLockWrapper "<<this<<" released: new thread state "<<mySaveThreadState);
56 // if(MYPYDEBUG) MESSAGE("~PyReleaseLock()");
62 PyLockWrapper PyInterp_base::GetLockWrapper(){
67 // main python interpreter
69 //PyThreadState *PyInterp_base::_gtstate = 0; // force 0 before execution
70 int PyInterp_base::_argc = 1;
71 char* PyInterp_base::_argv[] = {""};
73 PyObject *PyInterp_base::builtinmodule = NULL;
77 * basic constructor here : herited classes constructors must call initalize() method
80 PyInterp_base::PyInterp_base(): _tstate(0), _vout(0), _verr(0), _g(0), _atFirst(true)
82 // if(MYPYDEBUG) MESSAGE("PyInterp_base::PyInterp_base() - this = "<<this);
85 PyInterp_base::~PyInterp_base()
87 // if(MYPYDEBUG) MESSAGE("PyInterp_base::~PyInterp_base() - this = "<<this);
88 PyLockWrapper aLock(_tstate);
89 //Py_EndInterpreter(_tstate);
94 * Must be called by herited classes constructors. initialize() calls virtuals methods
95 * initstate & initcontext, not defined here in base class. initstate & initcontext methods
96 * must be implemented in herited classes, following the Python interpreter policy
97 * (mono or multi interpreter...).
99 void PyInterp_base::initialize()
101 _history.clear(); // start a new list of user's commands
102 _ith = _history.begin();
105 // Here the global lock is released
106 // if(MYPYDEBUG) MESSAGE("PyInterp_base::initialize() - this = "<<this<<"; _gtstate = "<<_gtstate);
108 // The lock will be acquired in initState. Make provision to release it on exit
109 PyReleaseLock aReleaseLock;
114 // used to interpret & compile commands
115 PyObjWrapper m(PyImport_ImportModule("codeop"));
117 // INFOS("PyInterp_base::initialize() - PyImport_ImportModule('codeop') failed");
123 // Create cStringIO to capture stdout and stderr
125 //PycStringIO = (PycStringIO_CAPI *)xxxPyCObject_Import("cStringIO", "cStringIO_CAPI");
126 _vout = PycStringIO->NewOutput(128);
127 _verr = PycStringIO->NewOutput(128);
129 // All the initRun outputs are redirected to the standard output (console)
133 void PyInterp_base::init_python()
136 * Initialize the main state (_gtstate) if not already done
137 * The lock is released on init_python output
138 * It is the caller responsability to acquire it if needed
140 MESSAGE("PyInterp_base::init_python");
141 ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
142 SCRUTE(KERNEL_PYTHON::_gtstate);
144 // PyReleaseLock aReleaseLock;
145 // Py_Initialize(); // Initialize the interpreter
146 // PyEval_InitThreads(); // Initialize and acquire the global interpreter lock
147 // PySys_SetArgv(_argc,_argv); // initialize sys.argv
148 // _gtstate = PyThreadState_Get();
152 string PyInterp_base::getbanner()
154 string aBanner("Python ");
155 aBanner = aBanner + Py_GetVersion() + " on " + Py_GetPlatform() ;
156 aBanner = aBanner + "\ntype help to get general information on environment\n";
161 int PyInterp_base::initRun()
163 PySys_SetObject("stderr",_verr);
164 PySys_SetObject("stdout",_vout);
166 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
167 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
169 //PyObject *m = PyImport_GetModuleDict();
171 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
172 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
174 // if(MYPYDEBUG) MESSAGE("PyInterp_base::initRun() - this = "<<this<<"; _verr = "<<_verr<<"; _vout = "<<_vout);
180 * This function compiles a string (command) and then evaluates it in the dictionnary
181 * context if possible.
184 * 1 : incomplete text
185 * 0 : complete text executed with success
187 int compile_command(const char *command,PyObject *context)
189 PyObject *m = PyImport_AddModule("codeop");
190 if(!m){ // Fatal error. No way to go on.
194 PyObjWrapper v(PyObject_CallMethod(m,"compile_command","s",command));
196 // Error encountered. It should be SyntaxError,
197 //so we don't write out traceback
198 PyObjWrapper exception, value, tb;
199 PyErr_Fetch(&exception, &value, &tb);
200 PyErr_NormalizeException(&exception, &value, &tb);
201 PyErr_Display(exception, value, NULL);
203 }else if (v == Py_None){
204 // Incomplete text we return 1 : we need a complete text to execute
207 // Complete and correct text. We evaluate it.
208 PyObjWrapper r(PyEval_EvalCode(v,context,context));
210 // Execution error. We return -1
214 // The command has been successfully executed. Return 0
220 int PyInterp_base::run(const char *command)
224 ret = simpleRun("from Help import *");
229 ret = simpleRun("import salome");
234 ret = simpleRun("salome.salome_init()");
241 return simpleRun(command);
245 int PyInterp_base::simpleRun(const char *command)
247 if( !_atFirst && strcmp(command,"") != 0 ) {
248 _history.push_back(command);
249 _ith = _history.end();
252 // We come from C++ to enter Python world
253 // We need to acquire the Python global lock
254 //PyLockWrapper aLock(_tstate); // san - lock is centralized now
256 // Reset redirected outputs before treatment
257 PySys_SetObject("stderr",_verr);
258 PySys_SetObject("stdout",_vout);
260 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
261 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
263 int ier = compile_command(command,_g);
265 // Outputs are redirected on standards outputs (console)
266 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
267 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
272 const char * PyInterp_base::getPrevious()
274 if(_ith != _history.begin()){
276 return (*_ith).c_str();
279 return BEGIN_HISTORY_PY;
283 const char * PyInterp_base::getNext()
285 if(_ith != _history.end()){
288 if (_ith == _history.end())
289 return TOP_HISTORY_PY;
291 return (*_ith).c_str();
295 string PyInterp_base::getverr(){
296 //PyLockWrapper aLock(_tstate);
297 PyObjWrapper v(PycStringIO->cgetvalue(_verr));
298 string aRet(PyString_AsString(v));
303 string PyInterp_base::getvout(){
304 //PyLockWrapper aLock(_tstate);
305 PyObjWrapper v(PycStringIO->cgetvalue(_vout));
306 string aRet(PyString_AsString(v));