Salome HOME
PR: Merge V1_2c etape 1
[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 using namespace std;
13 using namespace std;
14 #include "PyInterp_PyQt.h"
15
16 #include "utilities.h"
17
18 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
19
20 /*!
21  * constructor : the main SALOME Python interpreter is used for PyQt GUI.
22  * calls initialize method defined in base class, which calls virtual methods
23  * initstate & initcontext redefined here
24  */
25 PyInterp_PyQt::PyInterp_PyQt()
26 {
27   initialize();
28 }
29
30 PyInterp_PyQt::~PyInterp_PyQt()
31 {
32 }
33
34 void PyInterp_PyQt::initState()
35 {
36   salomeAcquireLock();           //acquire python global lock (one for all interpreters)
37   SCRUTE(PyInterp_base::_gtstate);
38   _tstate=PyInterp_base::_gtstate;
39   PyThreadState_Swap(_tstate);
40   SCRUTE(_tstate);
41 }
42
43 void PyInterp_PyQt::initContext()
44 {
45   _g = PyDict_New();          // create interpreter dictionnary context
46   PyObject *bimod = PyImport_ImportModule("__builtin__");
47   PyDict_SetItemString(_g, "__builtins__", bimod);
48   Py_DECREF(bimod);
49 }
50
51 void PyInterp_PyQt::enter()
52 {
53   PyThreadState *oldstate;
54   SCRUTE(_tstate);
55   salomeAcquireLock();
56   oldstate=PyThreadState_Swap(_tstate);
57   SCRUTE(oldstate);
58 }
59
60 void PyInterp_PyQt::quit()
61 {
62   MESSAGE("quit");
63   salomeReleaseLock();
64   MESSAGE("fin quit");
65 }
66
67 void PyInterp_PyQt::run(const char *command)
68 {
69   PyObject *code,*r;
70   enter();
71   MESSAGE("compile");
72   code=Py_CompileString((char *)command,"PyGUI",Py_file_input);
73   if (code == NULL)
74     {
75       /*
76         Une erreur s est produite en general SyntaxError
77       */
78       PyErr_Print();
79       quit();
80       return;
81     }
82   r  = PyEval_EvalCode(code,_g,_g);
83   Py_DECREF(code);
84   if (r==NULL)
85     {
86       /*
87         Une erreur s est produite a l execution
88       */
89       PyErr_Print();
90       quit();
91       return;
92     }
93   Py_DECREF(r);
94   quit();
95 }
96