Salome HOME
7b1ca1cb203269c77d38461b1eb0761dc5dead30
[modules/gui.git] / src / LightApp / LightApp_PyInterp.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
21 //  Date   : 22/06/2007
22 //
23 #include "LightApp_PyInterp.h"
24
25 #include <SUITApp_init_python.hxx>
26 #include <PyConsole_Interp.h>
27
28 #include <pthread.h>
29
30 /*!
31  * constructor : multi Python interpreter, one per SALOME study.
32  * calls initialize method defined in base class, which calls virtual methods
33  * initstate & initcontext redefined here.
34  */
35 LightApp_PyInterp::LightApp_PyInterp(): PyConsole_Interp()
36 {
37 }
38
39 /*!
40  * Destructor.
41  */
42 LightApp_PyInterp::~LightApp_PyInterp()
43 {
44 }
45  
46 /*!\class LightApp_PyInterp
47  * EDF-CCAR
48  * When SALOME uses multi Python interpreter feature,
49  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
50  * This is fine because every study has its own modules (sys.modules) stdout and stderr
51  * BUT some Python modules must be imported only once. In multi interpreter context Python
52  * modules (*.py) are imported several times.
53  * The pyqt module must be imported only once because it registers classes in a C module.
54  * It's quite the same with omniorb modules (internals and generated with omniidl)
55  * This problem is handled with "shared modules" defined in salome_shared_modules.py
56  * These "shared modules" are imported only once and only copied in all the other interpreters
57  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
58  * but if we have copied some modules and imported others problems may arise with operations that
59  * are not allowed in restricted execution environment. So we must impose that all interpreters
60  * have identical __builtin__ module.
61  * That's all, for the moment ...
62  */
63
64
65 bool LightApp_PyInterp::initContext()
66 {
67   /*!
68    * The GIL is assumed to be held
69    * It is the caller responsability caller to acquire the GIL
70    * It will still be held on initContext output
71    */
72   if ( !PyConsole_Interp::initContext() )
73     return false;
74   
75   //Import special module to change the import mechanism
76   PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
77   if ( !m1 )
78   {
79     PyErr_Print();
80     return false;
81     }
82   
83   // Call init_shared_modules to initialize the shared import mechanism for modules 
84   //that must not be imported twice
85   PyObjWrapper m2( PyObject_CallMethod( m1, (char*)"init_shared_modules", (char*)"O", SUIT_PYTHON::salome_shared_modules_module ) );
86   if ( !m2 )
87   {
88     PyErr_Print();
89     return false;
90   }
91   return true;
92 }
93
94 /*!
95   Do nothing
96   The initialization has been done in main
97  */
98 void LightApp_PyInterp::initPython()
99 {
100   _gtstate=SUIT_PYTHON::_gtstate; // initialisation in main
101   _interp=SUIT_PYTHON::_interp;
102 }