Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUILight / SALOME_PYQT_PyInterp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SALOME_PYQT_PyInterp.h" // this include must be first !!
21
22 #include <SUITApp_init_python.hxx>
23 #include <PyInterp_Interp.h>
24 #include <utilities.h>
25
26 #ifndef GUI_DISABLE_CORBA
27 #include <Container_init_python.hxx>
28 #endif
29
30
31
32
33 /*!
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
37  */
38 SALOME_PYQT_PyInterp::SALOME_PYQT_PyInterp(): PyInterp_Interp()
39 {
40 }
41
42 SALOME_PYQT_PyInterp::~SALOME_PYQT_PyInterp()
43 {
44 }
45
46 void SALOME_PYQT_PyInterp::initPython()
47 {
48  /*
49   * Do nothing
50   * The initialization has been done in main
51   */
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;
58   }
59   else {
60     ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
61     SCRUTE(KERNEL_PYTHON::_gtstate);
62     _tstate = KERNEL_PYTHON::_gtstate;
63   }
64 #else
65   SCRUTE(SUIT_PYTHON::_gtstate);
66   _tstate = SUIT_PYTHON::_gtstate;
67 #endif
68 }
69
70 bool SALOME_PYQT_PyInterp::initState()
71 {
72  /*
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
76   */
77   PyEval_AcquireThread(_tstate);
78   SCRUTE(_tstate);
79   PyEval_ReleaseThread(_tstate);
80   return true;
81 }
82
83
84 bool SALOME_PYQT_PyInterp::initContext()
85 {
86   /*
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
90    */
91   _g = PyDict_New();          // create interpreter dictionnary context
92   PyObject *bimod = PyImport_ImportModule("__builtin__");
93   PyDict_SetItemString(_g, "__builtins__", bimod);
94   Py_DECREF(bimod); 
95   return true;
96 }
97
98 int SALOME_PYQT_PyInterp::run(const char *command)
99 {
100   MESSAGE("compile");
101   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
102   if(!code){
103     // Une erreur s est produite en general SyntaxError
104     PyErr_Print();
105     return -1;
106   }
107   //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
108   //  PyObject *r = PyEval_EvalCode(code,_g,_g);
109   //#else
110   PyObject *r = PyEval_EvalCode((PyCodeObject *)code,_g,_g);
111   //#endif
112   Py_DECREF(code);
113   if(!r){
114     // Une erreur s est produite a l execution
115     PyErr_Print();
116     return -1 ;
117   }
118   Py_DECREF(r);
119   return 0;
120 }