Salome HOME
PAL10715: Portability python 2.4. A fix by Erwan ADAM.
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUI / SALOME_PYQT_PyInterp.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOME_PYQT_PyInterp.cxx
8 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
9 //  Module : SALOME
10 //  $Header$
11
12 #include "SALOME_PYQT_PyInterp.h" // this include must be first (see PyInterp_base.h)!
13 #include "utilities.h"
14 #include "Container_init_python.hxx"
15
16 using namespace std;
17
18
19 /*!
20  * constructor : the main SALOME Python interpreter is used for PyQt GUI.
21  * calls initialize method defined in base class, which calls virtual methods
22  * initstate & initcontext redefined here
23  */
24 SALOME_PYQT_PyInterp::SALOME_PYQT_PyInterp(): PyInterp_base()
25 {
26 }
27
28 SALOME_PYQT_PyInterp::~SALOME_PYQT_PyInterp()
29 {
30 }
31
32 bool SALOME_PYQT_PyInterp::initState()
33 {
34  /*
35   * The GIL is assumed to not be held on the call
36   * The GIL is acquired in initState and will be held on initState exit
37   * It is the caller responsability to release the lock on exit if needed
38   */
39   SCRUTE(KERNEL_PYTHON::_gtstate);
40   _tstate = KERNEL_PYTHON::_gtstate;
41   PyEval_AcquireLock();
42   PyThreadState_Swap(_tstate);
43   SCRUTE(_tstate);
44   return true;
45 }
46
47 bool SALOME_PYQT_PyInterp::initContext()
48 {
49   /*
50    * The GIL is assumed to be held
51    * It is the caller responsability to acquire the GIL before calling initContext
52    * It will still be held on initContext exit
53    */
54   _g = PyDict_New();          // create interpreter dictionnary context
55   PyObject *bimod = PyImport_ImportModule("__builtin__");
56   PyDict_SetItemString(_g, "__builtins__", bimod);
57   Py_DECREF(bimod);
58   return true;
59 }
60
61 int SALOME_PYQT_PyInterp::run(const char *command)
62 {
63   MESSAGE("compile");
64   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
65   if(!code){
66     // Une erreur s est produite en general SyntaxError
67     PyErr_Print();
68     return -1;
69   }
70 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
71   PyObject *r = PyEval_EvalCode(code,_g,_g);
72 #else
73   PyObject *r = PyEval_EvalCode((PyCodeObject*)code,_g,_g);
74 #endif
75   Py_DECREF(code);
76   if(!r){
77     // Une erreur s est produite a l execution
78     PyErr_Print();
79     return -1 ;
80   }
81   Py_DECREF(r);
82   return 0;
83 }
84