1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 !!!
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.
42 class PYINTERP_EXPORT PyLockWrapper
44 PyGILState_STATE _gil_state;
50 // "Rule of 3" - Forbid usage of copy operator and copy-constructor
51 PyLockWrapper(const PyLockWrapper & another);
52 const PyLockWrapper & operator=(const PyLockWrapper & another);
55 typedef void PyOutChanged(void* data,char * c);
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.
66 class PYINTERP_EXPORT PyInterp_Interp
73 virtual ~PyInterp_Interp();
78 virtual int run(const char *command);
80 // [ABN] - the PyLockWrapper is no more attached to the interpreter
81 // PyLockWrapper GetLockWrapper() const;
83 std::string getbanner() const;
84 void setverrcb(PyOutChanged*,void*);
85 void setvoutcb(PyOutChanged*,void*);
87 const char * getPrevious();
88 const char * getNext();
91 /** Redirection of stdout and stderr */
94 /** Execution context (local and global variables) */
97 std::list<std::string> _history;
98 std::list<std::string>::iterator _ith;
100 virtual int beforeRun();
101 int simpleRun(const char* command, const bool addToHistory = true);
103 virtual void initPython();
104 /** OBSOLETE - should'nt be called anymore */
105 //virtual bool initState() = 0;
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();
114 * Utility class to properly handle the reference counting required on Python objects.
116 class PYINTERP_EXPORT PyObjWrapper
120 PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
121 PyObjWrapper() : myObject(0) {}
122 virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
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)
132 Py_XDECREF(myObject);
133 myObject = theObjWrapper->myObject;
146 #endif // PYINTERP_INTERP_H