Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / src / SALOMEGUI / QAD_PyInterp.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_PyInterp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "QAD_PyInterp.h"
30 #include "utilities.h"
31
32 using namespace std;
33
34
35 #ifdef _DEBUG_
36 static int MYDEBUG = 0;
37 #else
38 static int MYDEBUG = 0;
39 #endif
40
41
42 /*!
43  * constructor : multi Python interpreter, one per SALOME study.
44  * calls initialize method defined in base class, which calls virtual methods
45  * initstate & initcontext redefined here.
46  */
47 QAD_PyInterp::QAD_PyInterp(): PyInterp_base()
48 {
49 }
50
51 QAD_PyInterp::~QAD_PyInterp()
52 {
53 }
54  
55 /*!
56  * EDF-CCAR
57  * When SALOME uses multi Python interpreter feature,
58  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
59  * This is fine because every study has its own modules (sys.modules) stdout and stderr
60  * BUT some Python modules must be imported only once. In multi interpreter context Python
61  * modules (*.py) are imported several times.
62  * The pyqt module must be imported only once because it registers classes in a C module.
63  * It's quite the same with omniorb modules (internals and generated with omniidl)
64  * This problem is handled with "shared modules" defined in salome_shared_modules.py
65  * These "shared modules" are imported only once and only copied in all the other interpreters
66  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
67  * but if we have copied some modules and imported others problems may arise with operations that
68  * are not allowed in restricted execution environment. So we must impose that all interpreters
69  * have identical __builtin__ module.
70  * That's all, for the moment ...
71  */
72
73 void QAD_PyInterp::initState()
74 {
75   /*
76    * The GIL is acquired and will be held on initState output
77    * It is the caller responsability to release the lock if needed
78    */
79   PyEval_AcquireLock();
80   _tstate = Py_NewInterpreter(); // create an interpreter and save current state
81   PySys_SetArgv(PyInterp_base::_argc,PyInterp_base::_argv); // initialize sys.argv
82   if(MYDEBUG) MESSAGE("QAD_PyInterp::initState - this = "<<this<<"; _tstate = "<<_tstate);
83
84   /*
85    * If builtinmodule has been initialized all the sub interpreters
86    * will have the same __builtin__ module
87    */
88   if(builtinmodule){ 
89     PyObject *m = PyImport_GetModuleDict();
90     PyDict_SetItemString(m, "__builtin__", builtinmodule);
91     SCRUTE(builtinmodule->ob_refcnt); // builtinmodule reference counter
92     _tstate->interp->builtins = PyModule_GetDict(builtinmodule);
93     Py_INCREF(_tstate->interp->builtins);
94   }
95 }
96
97
98 void QAD_PyInterp::initContext()
99 {
100   /*
101    * The GIL is assumed to be held
102    * It is the caller responsability caller to acquire the GIL
103    * It will still be held on initContext output
104    */
105   PyObject *m = PyImport_AddModule("__main__");  // interpreter main module (module context)
106   if(!m){
107     if(MYDEBUG) MESSAGE("problem...");
108     PyErr_Print();
109     ASSERT(0);
110     return;
111   }  
112   _g = PyModule_GetDict(m);          // get interpreter dictionnary context
113   if(MYDEBUG) MESSAGE("QAD_PyInterp::initContext - this = "<<this<<"; _g = "<<_g);
114
115   if(builtinmodule){
116     PyDict_SetItemString(_g, "__builtins__", builtinmodule); // assign singleton __builtin__ module
117   }
118
119   // Debut modif CCAR
120   // Import special module to change the import mechanism
121   PyObjWrapper m1(PyImport_ImportModule("import_hook"));
122   if(!m1){
123     MESSAGE("initContext: problem with import_hook import");
124     PyErr_Print();
125     PyErr_Clear();
126     ASSERT(0);
127   }else{
128     // Call init_shared_modules to initialize the shared import mechanism for modules 
129     //that must not be imported twice
130     PyObjWrapper m2(PyObject_CallMethod(m1,"init_shared_modules","O",salome_shared_modules_module));
131     if(!m2){
132       MESSAGE("initContext: problem with init_shared_modules call");
133       PyErr_Print();
134       PyErr_Clear();
135       ASSERT(0);
136     }
137   }
138   // Fin   modif CCAR
139 }