1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
24 // File : SalomeApp_PyInterp.cxx
25 // Author : Nicolas REJNERI
27 #include "SalomeApp_PyInterp.h"
29 #include <utilities.h>
30 #include <Container_init_python.hxx>
32 #include "PyInterp.h" // this include must be first (see PyInterp_base.h)!
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.
39 SalomeApp_PyInterp::SalomeApp_PyInterp():
40 PyConsole_EnhInterp(), myFirstRun( true )
47 SalomeApp_PyInterp::~SalomeApp_PyInterp()
51 /*!\class SalomeApp_PyInterp
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 ...
70 bool SalomeApp_PyInterp::initContext()
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
77 if ( !PyConsole_Interp::initContext() )
80 // Import special module to change the import mechanism
81 PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
84 MESSAGE( "initContext: problem with import_hook import" );
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 ) );
95 MESSAGE( "initContext: problem with init_shared_modules call" );
106 The initialization has been done in main
108 void SalomeApp_PyInterp::initPython()
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;
118 Called before each Python command running.
120 int SalomeApp_PyInterp::beforeRun()
124 int ret = simpleRun( "from Help import *", false );
127 ret = simpleRun( "import salome", false );
130 ret = simpleRun( "salome.salome_init(0,1)", false );