Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUI / SALOME_PYQT_PyInterp.cxx
1 //  Copyright (C) 2007-2008  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.
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   : SALOME_PYQT_PyInterp.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include "SALOME_PYQT_PyInterp.h" // this include must be first (see PyInterp_base.h)!
26 #include <utilities.h>
27 #include <Container_init_python.hxx>
28
29 /*!
30  * constructor : the main SALOME Python interpreter is used for PyQt GUI.
31  * calls initialize method defined in base class, which calls virtual methods
32  * initstate & initcontext redefined here
33  */
34 SALOME_PYQT_PyInterp::SALOME_PYQT_PyInterp(): PyInterp_Interp()
35 {
36 }
37
38 SALOME_PYQT_PyInterp::~SALOME_PYQT_PyInterp()
39 {
40 }
41
42 bool SALOME_PYQT_PyInterp::initState()
43 {
44  /*
45   * The GIL is assumed to not be held on the call
46   * The GIL is acquired in initState and will be held on initState exit
47   * It is the caller responsability to release the lock on exit if needed
48   */
49   SCRUTE(KERNEL_PYTHON::_gtstate);
50   _tstate = KERNEL_PYTHON::_gtstate;
51   PyEval_AcquireThread(_tstate);
52   SCRUTE(_tstate);
53   PyEval_ReleaseThread(_tstate);
54   return true;
55 }
56
57 bool SALOME_PYQT_PyInterp::initContext()
58 {
59   /*
60    * The GIL is assumed to be held
61    * It is the caller responsability to acquire the GIL before calling initContext
62    * It will still be held on initContext exit
63    */
64   _g = PyDict_New();          // create interpreter dictionnary context
65   PyObject *bimod = PyImport_ImportModule("__builtin__");
66   PyDict_SetItemString(_g, "__builtins__", bimod);
67   Py_DECREF(bimod);
68   return true;
69 }
70
71 int SALOME_PYQT_PyInterp::run(const char *command)
72 {
73   MESSAGE("compile");
74   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
75   if(!code){
76     // Une erreur s est produite en general SyntaxError
77     PyErr_Print();
78     return -1;
79   }
80   //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
81   //  PyObject *r = PyEval_EvalCode(code,_g,_g);
82   //#else
83   PyObject *r = PyEval_EvalCode((PyCodeObject *)code,_g,_g);
84   //#endif
85   Py_DECREF(code);
86   if(!r){
87     // Une erreur s est produite a l execution
88     PyErr_Print();
89     return -1 ;
90   }
91   Py_DECREF(r);
92   return 0;
93 }