Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / SalomeApp / SalomeApp_PyInterp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
24 //  File   : SalomeApp_PyInterp.cxx
25 //  Author : Nicolas REJNERI
26
27 #include "SalomeApp_PyInterp.h"
28
29 #include <utilities.h>
30 #include <Container_init_python.hxx>
31
32 #include "PyInterp.h" // this include must be first (see PyInterp_base.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 SalomeApp_PyInterp::SalomeApp_PyInterp(): 
40   PyConsole_Interp(), myFirstRun( true )
41 {
42 }
43
44 /*!
45  * Destructor.
46  */
47 SalomeApp_PyInterp::~SalomeApp_PyInterp()
48 {
49 }
50  
51 /*!\class SalomeApp_PyInterp
52  * EDF-CCAR
53  * When SALOME uses multi Python interpreter feature,
54  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
55  * This is fine because every study has its own modules (sys.modules) stdout and stderr
56  * BUT some Python modules must be imported only once. In multi interpreter context Python
57  * modules (*.py) are imported several times.
58  * The pyqt module must be imported only once because it registers classes in a C module.
59  * It's quite the same with omniorb modules (internals and generated with omniidl)
60  * This problem is handled with "shared modules" defined in salome_shared_modules.py
61  * These "shared modules" are imported only once and only copied in all the other interpreters
62  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
63  * but if we have copied some modules and imported others problems may arise with operations that
64  * are not allowed in restricted execution environment. So we must impose that all interpreters
65  * have identical __builtin__ module.
66  * That's all, for the moment ...
67  */
68
69
70 bool SalomeApp_PyInterp::initContext()
71 {
72   /*!
73    * The GIL is assumed to be held
74    * It is the caller responsability caller to acquire the GIL
75    * It will still be held on initContext output
76    */
77   if ( !PyConsole_Interp::initContext() )
78     return false;
79
80   // Import special module to change the import mechanism
81   PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
82   if ( !m1 )
83   {
84     MESSAGE( "initContext: problem with import_hook import" );
85     PyErr_Print();
86     ASSERT( 0 );
87     return false;
88   }
89
90   // Call init_shared_modules to initialize the shared import mechanism for modules 
91   //that must not be imported twice
92   PyObjWrapper m2( PyObject_CallMethod( m1, (char*)"init_shared_modules", (char*)"O", KERNEL_PYTHON::salome_shared_modules_module ) );
93   if ( !m2 )
94   {
95     MESSAGE( "initContext: problem with init_shared_modules call" );
96     PyErr_Print();
97     ASSERT( 0 );
98     return false;
99   }
100
101   return true;
102 }
103
104 /*!
105   Do nothing
106   The initialization has been done in main
107  */
108 void SalomeApp_PyInterp::initPython()
109 {
110   MESSAGE("SalomeApp_PyInterp::initPython");
111   ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
112   SCRUTE(KERNEL_PYTHON::_gtstate);
113   _gtstate=KERNEL_PYTHON::_gtstate;
114   _interp=KERNEL_PYTHON::_interp;
115 }
116
117 /*!
118   Called before each Python command running.
119 */
120 int SalomeApp_PyInterp::beforeRun()
121 {
122   if ( myFirstRun ) {
123     myFirstRun = false;
124     int ret = simpleRun( "from Help import *", false );
125     if ( ret )
126       return ret;
127     ret = simpleRun( "import salome", false );
128     if (ret)
129       return ret;
130     ret = simpleRun( "salome.salome_init(0,1)", false );
131     if (ret)
132       return ret;
133   }
134   return true;
135 }