Salome HOME
e0b741e22daa06faeecdede91cdd987d3bbd1164
[modules/gui.git] / tools / PyInterp / src / PyInterp_Interp.h
1 // Copyright (C) 2007-2022  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 //  File   : PyInterp_Interp.h
23 //  Author : Christian CAREMOLI, Paul RASCLE, Adrien BRUNETON
24
25 #ifndef PYINTERP_INTERP_H
26 #define PYINTERP_INTERP_H
27
28 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
29 #include "PyInterp_Utils.h"
30 #include "PyInterp_RefCounterObj.h"
31
32 #include <list>
33 #include <string>
34
35 typedef void PyOutChanged(void* data,char * c);
36
37 typedef struct {
38   PyObject_HEAD
39   int softspace;
40   PyOutChanged* _cb;
41   void* _data;
42   bool _iscerr;
43 } PyStdOut;
44
45 /**
46  * Main class representing a *virtual* Python interpreter. There is really only one true
47  * Python interpreter in the whole application (no call to Py_NewInterpreter),
48  * but the use of different execution contexts allow
49  * to split the execution lines, and hence to emulate (relatively) independent interpreters.
50  * This has some consequences: modules imported in one context are not re-imported in another context
51  * (only there namespace is made available when importing in another context).
52  * See also class PyConsole_Interp.
53  */
54 class PYINTERP_EXPORT PyInterp_Interp : public PyInterp_RefCounterObj
55 {
56 public:
57   static int _argc;
58   static char* _argv[];
59   
60   void initialize();
61   void destroy();
62
63   virtual int run(const char *command); 
64   virtual void initStudy() {}
65
66   std::string getBanner() const;
67   void setverrcb(PyOutChanged*, void*);
68   void setvoutcb(PyOutChanged*, void*);
69
70   const char* getPrevious();
71   const char* getNext();
72
73 protected:
74   
75   PyInterp_Interp();
76   virtual ~PyInterp_Interp();
77
78 protected:
79   /** Redirection of stdout and stderr */
80   PyObject* _vout;
81   PyObject* _verr;
82   /** Execution context (local and global variables) */
83   PyObject* _global_context;
84   PyObject* _local_context;
85
86   std::list<std::string> _history;
87   std::list<std::string>::iterator _ith;
88
89   virtual int beforeRun();
90   virtual int afterRun();
91   int simpleRun(const char* command, const bool addToHistory = true);
92
93   virtual void initPython();
94
95   /** Initialize execution context. */
96   virtual bool initContext();
97   virtual bool initRun();
98   virtual void closeContext();
99
100   bool initialized() const;
101
102 private:
103   bool _initialized;
104 };
105
106 #endif // PYINTERP_INTERP_H