Salome HOME
Merge branch 'master' into V9_dev
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUILight / SALOME_PYQT_PyInterp.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 /*!
56  * Override. Create a distinct context from the SALOME Python console.
57  * Especially the global context is not connected to __main__ as in PyInterp_Interp
58  */
59 bool SALOME_PYQT_PyInterp::initContext()
60 {
61   /*
62    * The GIL is assumed to be held
63    * It is the caller responsability to acquire the GIL before calling initContext
64    * It will still be held on initContext exit
65    */
66   _local_context = PyDict_New();
67   _global_context = PyDict_New();
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     // An error occured - normally here a SyntaxError
77     PyErr_Print();
78     return -1;
79   }
80   PyObject *r = PyEval_EvalCode((PyObject *)code,_global_context,_local_context);
81
82   Py_DECREF(code);
83   if(!r){
84       // An error occured at execution
85     PyErr_Print();
86     return -1 ;
87   }
88   Py_DECREF(r);
89   return 0;
90 }