]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEGUI/PyInterp_PyQt.cxx
Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[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"
13
14 #include "utilities.h"
15 using namespace std;
16
17 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
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 PyInterp_PyQt::PyInterp_PyQt()
25 {
26   initialize();
27 }
28
29 PyInterp_PyQt::~PyInterp_PyQt()
30 {
31 }
32
33 void PyInterp_PyQt::initState()
34 {
35   salomeAcquireLock();           //acquire python global lock (one for all interpreters)
36   SCRUTE(PyInterp_base::_gtstate);
37   _tstate=PyInterp_base::_gtstate;
38   PyThreadState_Swap(_tstate);
39   SCRUTE(_tstate);
40 }
41
42 void PyInterp_PyQt::initContext()
43 {
44   _g = PyDict_New();          // create interpreter dictionnary context
45   PyObject *bimod = PyImport_ImportModule("__builtin__");
46   PyDict_SetItemString(_g, "__builtins__", bimod);
47   Py_DECREF(bimod);
48 }
49
50 void PyInterp_PyQt::enter()
51 {
52   PyThreadState *oldstate;
53   SCRUTE(_tstate);
54   salomeAcquireLock();
55   oldstate=PyThreadState_Swap(_tstate);
56   SCRUTE(oldstate);
57 }
58
59 void PyInterp_PyQt::quit()
60 {
61   MESSAGE("quit");
62   salomeReleaseLock();
63   MESSAGE("fin quit");
64 }
65
66 void PyInterp_PyQt::run(const char *command)
67 {
68   PyObject *code,*r;
69   enter();
70   MESSAGE("compile");
71   code=Py_CompileString((char *)command,"PyGUI",Py_file_input);
72   if (code == NULL)
73     {
74       /*
75         Une erreur s est produite en general SyntaxError
76       */
77       PyErr_Print();
78       quit();
79       return;
80     }
81   r  = PyEval_EvalCode(code,_g,_g);
82   Py_DECREF(code);
83   if (r==NULL)
84     {
85       /*
86         Une erreur s est produite a l execution
87       */
88       PyErr_Print();
89       quit();
90       return;
91     }
92   Py_DECREF(r);
93   quit();
94 }
95