1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : PyInterp_base.h
8 // Author : Christian CAREMOLI, Paul RASCLE, EDF
12 #ifndef _PYINTERP_BASE_H_
13 #define _PYINTERP_BASE_H_
21 // include order important!
22 // pthread then python then qt
23 //#include <pthread.h> // must be before Python.h !
25 #include <Python.h> // must be before qt includes ...
26 #include <compile.h> // Python include needed for versions before 2.4. Included in Python.h now.
27 #include <eval.h> // Python include needed for versions before 2.4. Included in Python.h now.
29 //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
30 //extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
33 /* For 2.3, use the PyGILState_ calls */
34 #if (PY_VERSION_HEX >= 0x02030000)
38 #define TOP_HISTORY_PY "--- top of history ---"
39 #define BEGIN_HISTORY_PY "--- begin of history ---"
41 class PYINTERP_EXPORT PyLockWrapper
43 PyThreadState* myThreadState;
44 PyThreadState* mySaveThreadState;
45 #if defined(USE_GILSTATE)
46 PyGILState_STATE _savestate ;
49 PyLockWrapper(PyThreadState* theThreadState);
53 class PYINTERP_EXPORT PyInterp_base{
57 static PyObject *builtinmodule;
58 static PyThreadState *_gtstate;
59 static PyInterpreterState *_interp;
64 virtual void initialize();
65 virtual void init_python();
66 // init_python() made virtual to:
67 // 1. Remove dependency on KERNEL in light SALOME configuration
68 // 2. Allow redefinition of this method in SalomeApp_PyInterp class (it should be empty there and rely on KERNEL_PYTHON)
70 virtual int run(const char *command);
72 PyLockWrapper GetLockWrapper();
74 std::string getbanner();
75 std::string getverr();
76 std::string getvout();
78 const char * getPrevious();
79 const char * getNext();
82 PyThreadState * _tstate;
87 std::list<std::string> _history;
88 std::list<std::string>::iterator _ith;
91 int simpleRun(const char* command);
94 virtual bool initState() = 0;
95 virtual bool initContext() = 0;
99 class PYINTERP_EXPORT PyObjWrapper{
102 PyObjWrapper(PyObject* theObject): myObject(theObject) {}
103 PyObjWrapper(): myObject(0) {}
104 operator PyObject*(){
107 PyObject* operator->(){
116 bool operator==(PyObject* theObject){
117 return myObject == theObject;
119 PyObject** operator&(){
122 PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
123 Py_XDECREF(myObject);
124 myObject = theObjWrapper->myObject;
127 virtual ~PyObjWrapper(){
128 Py_XDECREF(myObject);