Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b1'.
[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 using namespace std;
30 using namespace std;
31 #include "QAD_PyInterp.h"
32 #include "utilities.h"
33
34 /*!
35  * constructor : multi Python interpreter, one per SALOME study.
36  * calls initialize method defined in base class, which calls virtual methods
37  * initstate & initcontext redefined here.
38  */
39 QAD_PyInterp::QAD_PyInterp(): PyInterp_base()
40 {
41   initialize();
42 }
43
44 QAD_PyInterp::~QAD_PyInterp()
45 {
46 }
47  
48 /*!
49  * EDF-CCAR
50  * When SALOME uses multi Python interpreter feature,
51  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
52  * This is fine because every study has its own modules (sys.modules) stdout and stderr
53  * BUT some Python modules must be imported only once. In multi interpreter context Python
54  * modules (*.py) are imported several times.
55  * The pyqt module must be imported only once because it registers classes in a C module.
56  * It's quite the same with omniorb modules (internals and generated with omniidl)
57  * This problem is handled with "shared modules" defined in salome_shared_modules.py
58  * These "shared modules" are imported only once and only copied in all the other interpreters
59  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
60  * but if we have copied some modules and imported others problems may arise with operations that
61  * are not allowed in restricted execution environment. So we must impose that all interpreters
62  * have identical __builtin__ module.
63  * That's all, for the moment ...
64  */
65
66 void QAD_PyInterp::initState()
67 {
68   MESSAGE("QAD_PyInterp::initState");
69   salomeAcquireLock();           //acquire python global lock (one for all interpreters)
70   _tstate = Py_NewInterpreter(); //create an interpreter and save current state
71   SCRUTE(_tstate);
72   SCRUTE(PyInterp_base::_argc);
73   SCRUTE(PyInterp_base::_argv[0]);
74   PySys_SetArgv(PyInterp_base::_argc,PyInterp_base::_argv);    // initialize sys.argv
75
76   if(builtinmodule == NULL)return;
77   /*
78    * If builtinmodule has been initialized all the sub interpreters
79    * will have the same __builtin__ module
80    */
81   PyObject *m=PyImport_GetModuleDict();
82   PyDict_SetItemString(m, "__builtin__", builtinmodule);
83   SCRUTE(builtinmodule->ob_refcnt);                            // builtinmodule reference counter
84   _tstate->interp->builtins = PyModule_GetDict(builtinmodule);
85   Py_INCREF(_tstate->interp->builtins);
86 }
87
88 void QAD_PyInterp::initContext()
89 {
90   MESSAGE("QAD_PyInterp::initContext");
91   PyObject *m;
92   m=PyImport_AddModule("__main__");  // interpreter main module (module context)
93   if(m == NULL)
94     {
95       MESSAGE("problem...");
96       PyErr_Print();
97       ASSERT(0);
98       salomeReleaseLock(); 
99       return;
100     }  
101   _g = PyModule_GetDict(m);          // get interpreter dictionnary context
102   SCRUTE(_g);
103
104   if(builtinmodule)
105     {
106       PyDict_SetItemString(_g, "__builtins__", builtinmodule); // assign singleton __builtin__ module
107     }
108 // Debut modif CCAR
109   /*
110    * Import special module to change the import mechanism
111    */
112   m =PyImport_ImportModule("import_hook");
113   if(m == NULL){
114       MESSAGE("initContext: problem with import_hook import");
115       PyErr_Print();
116       PyErr_Clear();
117       ASSERT(0);
118   }
119   /*
120    * Call init_shared_modules to initialize the shared import mechanism for modules 
121    * that must not be imported twice
122    */
123   if(m != NULL){
124       m= PyObject_CallMethod(m,
125                          "init_shared_modules","O",salome_shared_modules_module);
126       if (m == NULL){
127           MESSAGE("initContext: problem with init_shared_modules call");
128           PyErr_Print();
129           PyErr_Clear();
130           ASSERT(0);
131       }
132   }
133 // Fin   modif CCAR
134
135 }