]> SALOME platform Git repositories - modules/gui.git/blob - src/PyInterp/PyInterp_base.h
Salome HOME
66847770d1eb66234747a29bb542c5d34aa15ab3
[modules/gui.git] / src / PyInterp / PyInterp_base.h
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : PyInterp_base.h
8 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
9 //  Module : SALOME
10 //  $Header$
11
12 #ifndef _PYINTERP_BASE_H_
13 #define _PYINTERP_BASE_H_
14
15 #include "PyInterp.h"
16
17 #include <list>
18 #include <string>
19 #include <iostream>
20
21 // include order important!
22 // pthread then python then qt
23 //#include <pthread.h>  // must be before Python.h !
24
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.
28
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);
31 //#endif
32
33 /* For 2.3, use the PyGILState_ calls */
34 #if (PY_VERSION_HEX >= 0x02030000)
35 #define USE_GILSTATE
36 #endif
37
38 #define TOP_HISTORY_PY "--- top of history ---"
39 #define BEGIN_HISTORY_PY "--- begin of history ---"
40
41 class PYINTERP_EXPORT PyLockWrapper
42 {
43   PyThreadState* myThreadState;
44   PyThreadState* mySaveThreadState;
45 #if defined(USE_GILSTATE)
46   PyGILState_STATE _savestate ;
47 #endif
48  public:
49   PyLockWrapper(PyThreadState* theThreadState);
50   ~PyLockWrapper();
51 };
52
53 class PYINTERP_EXPORT PyInterp_base{
54  public:
55   static int _argc;
56   static char* _argv[];
57   static PyObject *builtinmodule;
58   static PyThreadState *_gtstate;
59   static PyInterpreterState *_interp;
60   
61   PyInterp_base();
62   ~PyInterp_base();
63   
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)
69
70   virtual int run(const char *command); 
71
72   PyLockWrapper GetLockWrapper();
73
74   std::string getbanner(); 
75   std::string getverr();
76   std::string getvout();  
77
78   const char * getPrevious();
79   const char * getNext();    
80
81  protected:
82   PyThreadState * _tstate;
83   PyObject * _vout;
84   PyObject * _verr;
85   PyObject * _g;
86   PyObject * _codeop;
87   std::list<std::string> _history;
88   std::list<std::string>::iterator _ith;
89   bool _atFirst;
90
91   int simpleRun(const char* command);
92   int initRun();
93
94   virtual bool initState() = 0;
95   virtual bool initContext() = 0;  
96 };
97
98
99 class PYINTERP_EXPORT PyObjWrapper{
100   PyObject* myObject;
101 public:
102   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
103   PyObjWrapper(): myObject(0) {}
104   operator PyObject*(){
105     return myObject;
106   }
107   PyObject* operator->(){
108     return myObject;
109   }
110   PyObject* get(){
111     return myObject;
112   }
113   bool operator!(){
114     return !myObject;
115   }
116   bool operator==(PyObject* theObject){
117     return myObject == theObject;
118   }
119   PyObject** operator&(){
120     return &myObject;
121   }
122   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
123     Py_XDECREF(myObject);
124     myObject = theObjWrapper->myObject;
125     return *this;
126   }
127   virtual ~PyObjWrapper(){ 
128     Py_XDECREF(myObject);
129   }
130 };
131
132 #endif