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 <cStringIO.h>
21 PyLockWrapper::PyLockWrapper(PyThreadState* theThreadState):
22 myThreadState(theThreadState),
25 #if defined(USE_GILSTATE)
26 if (myThreadState->interp == PyInterp_base::_interp) {
27 _savestate = PyGILState_Ensure();
29 PyEval_AcquireThread(myThreadState);
32 PyEval_AcquireThread(myThreadState);
36 PyLockWrapper::~PyLockWrapper()
38 #if defined(USE_GILSTATE)
39 if (myThreadState->interp == PyInterp_base::_interp) {
40 PyGILState_Release(_savestate);
42 PyEval_ReleaseThread(myThreadState);
45 PyEval_ReleaseThread(myThreadState);
57 PyLockWrapper PyInterp_base::GetLockWrapper(){
62 // main python interpreter (static attributes)
64 int PyInterp_base::_argc = 1;
65 char* PyInterp_base::_argv[] = {""};
67 PyObject *PyInterp_base::builtinmodule = NULL;
69 PyThreadState *PyInterp_base::_gtstate = NULL;
70 PyInterpreterState *PyInterp_base::_interp = NULL;
74 * basic constructor here : herited classes constructors must call initalize() method
77 PyInterp_base::PyInterp_base(): _tstate(0), _vout(0), _verr(0), _g(0), _atFirst(true)
81 PyInterp_base::~PyInterp_base()
87 * Must be called by herited classes constructors. initialize() calls virtuals methods
88 * initstate & initcontext, not defined here in base class. initstate & initcontext methods
89 * must be implemented in herited classes, following the Python interpreter policy
90 * (mono or multi interpreter...).
92 void PyInterp_base::initialize()
94 _history.clear(); // start a new list of user's commands
95 _ith = _history.begin();
98 // Here the global lock is released
102 PyLockWrapper aLock= GetLockWrapper();
106 // used to interpret & compile commands
107 PyObjWrapper m(PyImport_ImportModule("codeop"));
113 // Create cStringIO to capture stdout and stderr
115 _vout = PycStringIO->NewOutput(128);
116 _verr = PycStringIO->NewOutput(128);
118 // All the initRun outputs are redirected to the standard output (console)
122 void PyInterp_base::init_python()
125 if (Py_IsInitialized())
128 // Python is not initialized
129 Py_SetProgramName(_argv[0]);
130 Py_Initialize(); // Initialize the interpreter
131 PySys_SetArgv(_argc, _argv);
132 PyEval_InitThreads(); // Create (and acquire) the interpreter lock
133 _interp = PyThreadState_Get()->interp;
134 _gtstate = PyEval_SaveThread(); // Release global thread state
137 string PyInterp_base::getbanner()
139 // Should we take the lock ?
140 // PyEval_RestoreThread(_tstate);
141 string aBanner("Python ");
142 aBanner = aBanner + Py_GetVersion() + " on " + Py_GetPlatform() ;
143 aBanner = aBanner + "\ntype help to get general information on environment\n";
144 //PyEval_SaveThread();
149 int PyInterp_base::initRun()
151 PySys_SetObject("stderr",_verr);
152 PySys_SetObject("stdout",_vout);
154 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
155 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
157 //PyObject *m = PyImport_GetModuleDict();
159 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
160 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
167 * This function compiles a string (command) and then evaluates it in the dictionnary
168 * context if possible.
171 * 1 : incomplete text
172 * 0 : complete text executed with success
174 int compile_command(const char *command,PyObject *context)
176 PyObject *m = PyImport_AddModule("codeop");
177 if(!m){ // Fatal error. No way to go on.
181 PyObjWrapper v(PyObject_CallMethod(m,"compile_command","s",command));
183 // Error encountered. It should be SyntaxError,
184 //so we don't write out traceback
185 PyObjWrapper exception, value, tb;
186 PyErr_Fetch(&exception, &value, &tb);
187 PyErr_NormalizeException(&exception, &value, &tb);
188 PyErr_Display(exception, value, NULL);
190 }else if (v == Py_None){
191 // Incomplete text we return 1 : we need a complete text to execute
194 // Complete and correct text. We evaluate it.
195 //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
196 // PyObjWrapper r(PyEval_EvalCode(v,context,context));
198 PyObjWrapper r(PyEval_EvalCode((PyCodeObject *)(void *)v,context,context));
201 // Execution error. We return -1
205 // The command has been successfully executed. Return 0
211 int PyInterp_base::run(const char *command)
215 ret = simpleRun("from Help import *");
220 ret = simpleRun("import salome");
225 ret = simpleRun("salome.salome_init()");
232 return simpleRun(command);
236 int PyInterp_base::simpleRun(const char *command)
238 if( !_atFirst && strcmp(command,"") != 0 ) {
239 _history.push_back(command);
240 _ith = _history.end();
243 // We come from C++ to enter Python world
244 // We need to acquire the Python global lock
245 //PyLockWrapper aLock(_tstate); // san - lock is centralized now
247 // Reset redirected outputs before treatment
248 PySys_SetObject("stderr",_verr);
249 PySys_SetObject("stdout",_vout);
251 PyObjWrapper verr(PyObject_CallMethod(_verr,"reset",""));
252 PyObjWrapper vout(PyObject_CallMethod(_vout,"reset",""));
254 int ier = compile_command(command,_g);
256 // Outputs are redirected on standards outputs (console)
257 PySys_SetObject("stdout",PySys_GetObject("__stdout__"));
258 PySys_SetObject("stderr",PySys_GetObject("__stderr__"));
264 const char * PyInterp_base::getPrevious()
266 if(_ith != _history.begin()){
268 return (*_ith).c_str();
271 return BEGIN_HISTORY_PY;
275 const char * PyInterp_base::getNext()
277 if(_ith != _history.end()){
280 if (_ith == _history.end())
281 return TOP_HISTORY_PY;
283 return (*_ith).c_str();
287 string PyInterp_base::getverr(){
288 //PyLockWrapper aLock(_tstate);
289 PyObjWrapper v(PycStringIO->cgetvalue(_verr));
290 string aRet(PyString_AsString(v));
295 string PyInterp_base::getvout(){
296 //PyLockWrapper aLock(_tstate);
297 PyObjWrapper v(PycStringIO->cgetvalue(_vout));
298 string aRet(PyString_AsString(v));