Salome HOME
Merge remote branch 'origin/abn/newpy_pv4-1' into V7_3_1_BR
[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 - does nothing");
53 }
54
55 bool SALOME_PYQT_PyInterp::initContext()
56 {
57   /*
58    * The GIL is assumed to be held
59    * It is the caller responsability to acquire the GIL before calling initContext
60    * It will still be held on initContext exit
61    */
62   _context = PyDict_New();          // create interpreter dictionnary context
63   return true;
64 }
65
66 int SALOME_PYQT_PyInterp::run(const char *command)
67 {
68   MESSAGE("compile");
69   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
70   if(!code){
71     // An error occured - normally here a SyntaxError
72     PyErr_Print();
73     return -1;
74   }
75   PyObject *r = PyEval_EvalCode((PyCodeObject *)code,_context,_context);
76
77   Py_DECREF(code);
78   if(!r){
79       // An error occured at execution
80     PyErr_Print();
81     return -1 ;
82   }
83   Py_DECREF(r);
84   return 0;
85 }