1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SalomeApp_PyInterp.cxx
25 // Author : Nicolas REJNERI
29 #include "SalomeApp_PyInterp.h"
31 #include <utilities.h>
32 #include <Container_init_python.hxx>
34 static int MYDEBUG = 0;
36 static int MYDEBUG = 0;
40 * constructor : multi Python interpreter, one per SALOME study.
41 * calls initialize method defined in base class, which calls virtual methods
42 * initstate & initcontext redefined here.
44 SalomeApp_PyInterp::SalomeApp_PyInterp(): PythonConsole_PyInterp()
51 SalomeApp_PyInterp::~SalomeApp_PyInterp()
55 /*!\class SalomeApp_PyInterp
57 * Wasashen 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 ...
74 bool SalomeApp_PyInterp::initContext()
77 * The GIL is assumed to be held
78 * It is the caller responsability caller to acquire the GIL
79 * It will still be held on initContext output
81 if ( !PythonConsole_PyInterp::initContext() )
85 // Import special module to change the import mechanism
86 PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
89 MESSAGE( "initContext: problem with import_hook import" );
96 // Call init_shared_modules to initialize the shared import mechanism for modules
97 //that must not be imported twice
98 PyObjWrapper m2( PyObject_CallMethod( m1, "init_shared_modules", "O", KERNEL_PYTHON::salome_shared_modules_module ) );
101 MESSAGE( "initContext: problem with init_shared_modules call" );