Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / src / SALOMEGUI / PyInterp_PyQt.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   : PyInterp_PyQt.cxx
8 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
9 //  Module : SALOME
10 //  $Header$
11
12 #include "PyInterp_PyQt.h" // this include must be first (see PyInterp_base.h)!
13 #include "utilities.h"
14
15 using namespace std;
16
17
18 /*!
19  * constructor : the main SALOME Python interpreter is used for PyQt GUI.
20  * calls initialize method defined in base class, which calls virtual methods
21  * initstate & initcontext redefined here
22  */
23 PyInterp_PyQt::PyInterp_PyQt(): PyInterp_base()
24 {
25 }
26
27 PyInterp_PyQt::~PyInterp_PyQt()
28 {
29 }
30
31 void PyInterp_PyQt::initState()
32 {
33  /*
34   * The GIL is assumed to not be held on the call
35   * The GIL is acquired in initState and will be held on initState exit
36   * It is the caller responsability to release the lock on exit if needed
37   */
38   SCRUTE(PyInterp_base::_gtstate);
39   _tstate=PyInterp_base::_gtstate;
40   PyEval_AcquireLock();
41   PyThreadState_Swap(_tstate);
42   SCRUTE(_tstate);
43 }
44
45 void PyInterp_PyQt::initContext()
46 {
47   /*
48    * The GIL is assumed to be held
49    * It is the caller responsability to acquire the GIL before calling initContext
50    * It will still be held on initContext exit
51    */
52   _g = PyDict_New();          // create interpreter dictionnary context
53   PyObject *bimod = PyImport_ImportModule("__builtin__");
54   PyDict_SetItemString(_g, "__builtins__", bimod);
55   Py_DECREF(bimod);
56 }
57
58 void PyInterp_PyQt::run(const char *command)
59 {
60   MESSAGE("compile");
61   PyLockWrapper aLock(_tstate);
62   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
63   if(!code){
64     // Une erreur s est produite en general SyntaxError
65     PyErr_Print();
66     return;
67   }
68   PyObject *r = PyEval_EvalCode(code,_g,_g);
69   Py_DECREF(code);
70   if(!r){
71     // Une erreur s est produite a l execution
72     PyErr_Print();
73     return;
74   }
75   Py_DECREF(r);
76 }
77