Salome HOME
Use LightApp library
[modules/gui.git] / src / SalomeApp / SalomeApp_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   : SalomeApp_PyInterp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SalomeApp_PyInterp.h"
30
31 #include <utilities.h>
32 #include <Container_init_python.hxx>
33
34 #include <string>
35 #include <vector>
36
37 #include "PyInterp_base.h" // this include must be first (see PyInterp_base.h)!
38
39 #include <cStringIO.h>
40
41 /*!
42  * constructor : multi Python interpreter, one per SALOME study.
43  * calls initialize method defined in base class, which calls virtual methods
44  * initstate & initcontext redefined here.
45  */
46 SalomeApp_PyInterp::SalomeApp_PyInterp(): PythonConsole_PyInterp()
47 {
48 }
49
50 /*!
51  * Destructor.
52  */
53 SalomeApp_PyInterp::~SalomeApp_PyInterp()
54 {
55 }
56  
57 /*!\class SalomeApp_PyInterp
58  * EDF-CCAR
59  * Wasashen SALOME uses multi Python interpreter feature,
60  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
61  * This is fine because every study has its own modules (sys.modules) stdout and stderr
62  * BUT some Python modules must be imported only once. In multi interpreter context Python
63  * modules (*.py) are imported several times.
64  * The pyqt module must be imported only once because it registers classes in a C module.
65  * It's quite the same with omniorb modules (internals and generated with omniidl)
66  * This problem is handled with "shared modules" defined in salome_shared_modules.py
67  * These "shared modules" are imported only once and only copied in all the other interpreters
68  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
69  * but if we have copied some modules and imported others problems may arise with operations that
70  * are not allowed in restricted execution environment. So we must impose that all interpreters
71  * have identical __builtin__ module.
72  * That's all, for the moment ...
73  */
74
75
76 bool SalomeApp_PyInterp::initContext()
77 {
78   /*!
79    * The GIL is assumed to be held
80    * It is the caller responsability caller to acquire the GIL
81    * It will still be held on initContext output
82    */
83   if ( !PythonConsole_PyInterp::initContext() )
84     return false;
85
86   // Debut modif CCAR
87   // Import special module to change the import mechanism
88   PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
89   if ( !m1 )
90   {
91     MESSAGE( "initContext: problem with import_hook import" );
92     PyErr_Print();
93     PyErr_Clear();
94     ASSERT( 0 );
95     return false;
96   }
97
98   // Call init_shared_modules to initialize the shared import mechanism for modules 
99   //that must not be imported twice
100   PyObjWrapper m2( PyObject_CallMethod( m1, "init_shared_modules", "O", KERNEL_PYTHON::salome_shared_modules_module ) );
101   if ( !m2 )
102   {
103           MESSAGE( "initContext: problem with init_shared_modules call" );
104           PyErr_Print();
105           PyErr_Clear();
106           ASSERT( 0 );
107     return false;
108   }
109
110   return true;
111 }
112
113 void SalomeApp_PyInterp::init_python()
114 {
115   /*
116    * Initialize the main state (_gtstate) if not already done
117    * The lock is released on init_python output
118    * It is the caller responsability to acquire it if needed
119    */
120   MESSAGE("PyInterp_base::init_python");
121   ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
122   SCRUTE(KERNEL_PYTHON::_gtstate);
123 //  if(!_gtstate){
124 //  PyReleaseLock aReleaseLock;
125 //  Py_Initialize(); // Initialize the interpreter
126 //  PyEval_InitThreads(); // Initialize and acquire the global interpreter lock
127 //  PySys_SetArgv(_argc,_argv); // initialize sys.argv
128 //    _gtstate = PyThreadState_Get();
129 //  }
130 }
131