1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
23 #include "LightApp_PyInterp.h"
25 #include <SUITApp_init_python.hxx>
26 #include <PyConsole_Interp.h>
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.
35 LightApp_PyInterp::LightApp_PyInterp(): PyConsole_Interp()
42 LightApp_PyInterp::~LightApp_PyInterp()
46 /*!\class LightApp_PyInterp
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 ...
65 bool LightApp_PyInterp::initContext()
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
72 if ( !PyConsole_Interp::initContext() )
75 //Import special module to change the import mechanism
76 PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
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 ) );
96 The initialization has been done in main
98 void LightApp_PyInterp::initPython()
100 _gtstate=SUIT_PYTHON::_gtstate; // initialisation in main
101 _interp=SUIT_PYTHON::_interp;