1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SALOME_PYQT_PyInterp.h" // this include must be first !!
22 #include <SUITApp_init_python.hxx>
23 #include <PyInterp_Interp.h>
24 #include <utilities.h>
26 #ifndef GUI_DISABLE_CORBA
27 #include <Container_init_python.hxx>
34 * constructor : the main SALOME Python interpreter is used for PyQt GUI.
35 * calls initialize method defined in base class, which calls virtual methods
36 * initstate & initcontext redefined here
38 SALOME_PYQT_PyInterp::SALOME_PYQT_PyInterp(): PyInterp_Interp()
42 SALOME_PYQT_PyInterp::~SALOME_PYQT_PyInterp()
46 void SALOME_PYQT_PyInterp::initPython()
50 * The initialization has been done in main
52 MESSAGE("SALOME_PYQT_PyInterp::initPython");
53 #ifndef GUI_DISABLE_CORBA
54 if(SUIT_PYTHON::initialized) {
55 ASSERT(SUIT_PYTHON::_gtstate); // initialisation in main
56 SCRUTE(SUIT_PYTHON::_gtstate);
57 _tstate = SUIT_PYTHON::_gtstate;
60 ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
61 SCRUTE(KERNEL_PYTHON::_gtstate);
62 _tstate = KERNEL_PYTHON::_gtstate;
65 SCRUTE(SUIT_PYTHON::_gtstate);
66 _tstate = SUIT_PYTHON::_gtstate;
70 bool SALOME_PYQT_PyInterp::initState()
73 * The GIL is assumed to not be held on the call
74 * The GIL is acquired in initState and will be held on initState exit
75 * It is the caller responsability to release the lock on exit if needed
77 PyEval_AcquireThread(_tstate);
79 PyEval_ReleaseThread(_tstate);
84 bool SALOME_PYQT_PyInterp::initContext()
87 * The GIL is assumed to be held
88 * It is the caller responsability to acquire the GIL before calling initContext
89 * It will still be held on initContext exit
91 _g = PyDict_New(); // create interpreter dictionnary context
92 PyObject *bimod = PyImport_ImportModule("__builtin__");
93 PyDict_SetItemString(_g, "__builtins__", bimod);
98 int SALOME_PYQT_PyInterp::run(const char *command)
101 PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
103 // Une erreur s est produite en general SyntaxError
107 //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
108 // PyObject *r = PyEval_EvalCode(code,_g,_g);
110 PyObject *r = PyEval_EvalCode((PyCodeObject *)code,_g,_g);
114 // Une erreur s est produite a l execution