Salome HOME
766f02a5e72ded74c206ecd05e13ac568f71d19d
[modules/gui.git] / src / PyInterp / PyInterp_Interp.h
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : PyInterp_Interp.h
24 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
25 //  Module : SALOME
26 //
27 #ifndef PYINTERP_INTERP_H
28 #define PYINTERP_INTERP_H
29
30 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
31
32 #include <list>
33 #include <string>
34
35 /**
36  * Utility class wrappin the Python GIL acquisition. This makes use of the high level
37  * API (PyGILState_Ensure and PyGILState_Release), and is hence compatible with only
38  * one running Python interpreter (no call to Py_NewInterpreter()).
39  * When the class is instanciated the lock is acquired. It is released at destruction time.
40  * Copy construction (and hence assignation) is forbidden.
41  */
42 class PYINTERP_EXPORT PyLockWrapper
43 {
44   PyGILState_STATE _gil_state;
45 public:
46   PyLockWrapper();
47   ~PyLockWrapper();
48
49 private:
50   // "Rule of 3" - Forbid usage of copy operator and copy-constructor
51   PyLockWrapper(const PyLockWrapper & another);
52   const PyLockWrapper & operator=(const PyLockWrapper & another);
53 };
54
55 typedef void PyOutChanged(void* data,char * c);
56
57 /**
58  * Main class representing a *virtual* Python interpreter. There is really only one true
59  * Python interpreter in the whole application (no call to Py_NewInterpreter),
60  * but the use of different execution contexts allow
61  * to split the execution lines, and hence to emulate (relatively) independent interpreters.
62  * This has some consequences: modules imported in one context are not re-imported in another context
63  * (only there namespace is made available when importing in another context).
64  * See also class PyConsole_Interp.
65  */
66 class PYINTERP_EXPORT PyInterp_Interp
67 {
68 public:
69   static int _argc;
70   static char* _argv[];
71   
72   PyInterp_Interp();
73   virtual ~PyInterp_Interp();
74   
75   void initialize();
76   void destroy();
77
78   virtual int run(const char *command); 
79
80   // [ABN] - the PyLockWrapper is no more attached to the interpreter
81   // PyLockWrapper GetLockWrapper() const;
82
83   std::string getbanner() const;
84   void setverrcb(PyOutChanged*,void*);
85   void setvoutcb(PyOutChanged*,void*);
86
87   const char * getPrevious();
88   const char * getNext();
89
90 protected:
91   /** Redirection of stdout and stderr */
92   PyObject * _vout;
93   PyObject * _verr;
94   /** Execution context (local and global variables) */
95   PyObject * _context;
96   PyObject * _codeop;
97   std::list<std::string> _history;
98   std::list<std::string>::iterator _ith;
99
100   virtual int beforeRun();
101   int simpleRun(const char* command, const bool addToHistory = true);
102
103   virtual void initPython();
104   /** OBSOLETE - should'nt be called anymore */
105   //virtual bool initState() = 0;
106
107   /** Initialize execution context. Must set the member _context, and return True on success. */
108   virtual bool initContext() = 0;
109   virtual bool initRun();
110   virtual void closeContext();
111 };
112
113 /**
114  * Utility class to properly handle the reference counting required on Python objects.
115  */
116 class PYINTERP_EXPORT PyObjWrapper
117 {
118   PyObject* myObject;
119 public:
120   PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
121   PyObjWrapper() : myObject(0) {}
122   virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
123
124   operator PyObject*()    { return myObject;  }
125   PyObject* operator->()  { return myObject;  }
126   PyObject* get()         { return myObject;  }
127   bool operator!()        { return !myObject; }
128   bool operator==(PyObject* theObject) { return myObject == theObject; }
129   PyObject** operator&()  { return &myObject; }
130   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
131   {
132     Py_XDECREF(myObject);
133     myObject = theObjWrapper->myObject;
134     return *this;
135   }
136 };
137
138 typedef struct {
139   PyObject_HEAD
140   int softspace;
141   PyOutChanged* _cb;
142   void* _data;
143   bool _iscerr;
144 } PyStdOut;
145
146 #endif // PYINTERP_INTERP_H