1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : PyInterp_Interp.h
24 // Author : Christian CAREMOLI, Paul RASCLE, EDF
27 #ifndef PYINTERP_INTERP_H
28 #define PYINTERP_INTERP_H
30 #include "PyInterp.h" // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
35 class PYINTERP_EXPORT PyLockWrapper
37 PyThreadState* myThreadState;
38 PyThreadState* mySaveThreadState;
39 PyGILState_STATE _savestate;
41 PyLockWrapper(PyThreadState* theThreadState);
45 typedef void PyOutChanged(void* data,char * c);
47 class PYINTERP_EXPORT PyInterp_Interp
52 static PyObject *builtinmodule;
53 static PyThreadState *_gtstate;
54 static PyInterpreterState *_interp;
57 virtual ~PyInterp_Interp();
61 virtual int run(const char *command);
63 PyLockWrapper GetLockWrapper();
65 std::string getbanner();
66 void setverrcb(PyOutChanged*,void*);
67 void setvoutcb(PyOutChanged*,void*);
69 const char * getPrevious();
70 const char * getNext();
73 PyThreadState * _tstate;
78 std::list<std::string> _history;
79 std::list<std::string>::iterator _ith;
81 virtual int beforeRun() { return 0; }
82 int simpleRun(const char* command, const bool addToHistory = true);
84 virtual bool initRun();
85 virtual void initPython();
86 virtual bool initState() = 0;
87 virtual bool initContext() = 0;
90 class PYINTERP_EXPORT PyObjWrapper
94 PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
95 PyObjWrapper() : myObject(0) {}
96 virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
98 operator PyObject*() { return myObject; }
99 PyObject* operator->() { return myObject; }
100 PyObject* get() { return myObject; }
101 bool operator!() { return !myObject; }
102 bool operator==(PyObject* theObject) { return myObject == theObject; }
103 PyObject** operator&() { return &myObject; }
104 PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
106 Py_XDECREF(myObject);
107 myObject = theObjWrapper->myObject;
120 #endif // PYINTERP_INTERP_H