Salome HOME
NRI : Add message (GUI lib not found).
[modules/kernel.git] / src / SALOMEGUI / PyInterp_PyQt.cxx
1 using namespace std;
2 using namespace std;
3 //=============================================================================
4 // File      : PyInterp_PyQt.cxx
5 // Created   : ven fév  7 10:01:36 CET 2003
6 // Author    : Christian CAREMOLI, Paul RASCLE, EDF
7 // Project   : SALOME
8 // Copyright : EDF 2003
9 // $Header$
10 //=============================================================================
11
12 #include "PyInterp_PyQt.h"
13
14 #include "utilities.h"
15
16 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
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()
24 {
25   initialize();
26 }
27
28 PyInterp_PyQt::~PyInterp_PyQt()
29 {
30 }
31
32 void PyInterp_PyQt::initState()
33 {
34   salomeAcquireLock();           //acquire python global lock (one for all interpreters)
35   SCRUTE(PyInterp_base::_gtstate);
36   _tstate=PyInterp_base::_gtstate;
37   PyThreadState_Swap(_tstate);
38   SCRUTE(_tstate);
39 }
40
41 void PyInterp_PyQt::initContext()
42 {
43   _g = PyDict_New();          // create interpreter dictionnary context
44   PyObject *bimod = PyImport_ImportModule("__builtin__");
45   PyDict_SetItemString(_g, "__builtins__", bimod);
46   Py_DECREF(bimod);
47 }
48
49 void PyInterp_PyQt::enter()
50 {
51   PyThreadState *oldstate;
52   SCRUTE(_tstate);
53   salomeAcquireLock();
54   oldstate=PyThreadState_Swap(_tstate);
55   SCRUTE(oldstate);
56 }
57
58 void PyInterp_PyQt::quit()
59 {
60   MESSAGE("quit");
61   salomeReleaseLock();
62   MESSAGE("fin quit");
63 }
64
65 void PyInterp_PyQt::run(const char *command)
66 {
67   PyObject *code,*r;
68   enter();
69   MESSAGE("compile");
70   code=Py_CompileString((char *)command,"PyGUI",Py_file_input);
71   if (code == NULL)
72     {
73       /*
74         Une erreur s est produite en general SyntaxError
75       */
76       PyErr_Print();
77       quit();
78       return;
79     }
80   r  = PyEval_EvalCode(code,_g,_g);
81   Py_DECREF(code);
82   if (r==NULL)
83     {
84       /*
85         Une erreur s est produite a l execution
86       */
87       PyErr_Print();
88       quit();
89       return;
90     }
91   Py_DECREF(r);
92   quit();
93 }
94