Back to index

salome-gui  6.5.0
SalomeApp_PyInterp.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
00024 //  File   : SalomeApp_PyInterp.cxx
00025 //  Author : Nicolas REJNERI
00026 
00027 #include "SalomeApp_PyInterp.h"
00028 
00029 #include <utilities.h>
00030 #include <Container_init_python.hxx>
00031 
00032 #include "PyInterp.h" // this include must be first (see PyInterp_base.h)!
00033 
00039 SalomeApp_PyInterp::SalomeApp_PyInterp(): 
00040   PyConsole_Interp(), myFirstRun( true )
00041 {
00042 }
00043 
00047 SalomeApp_PyInterp::~SalomeApp_PyInterp()
00048 {
00049 }
00050  
00070 bool SalomeApp_PyInterp::initContext()
00071 {
00077   if ( !PyConsole_Interp::initContext() )
00078     return false;
00079 
00080   // Import special module to change the import mechanism
00081   PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
00082   if ( !m1 )
00083   {
00084     MESSAGE( "initContext: problem with import_hook import" );
00085     PyErr_Print();
00086     ASSERT( 0 );
00087     return false;
00088   }
00089 
00090   // Call init_shared_modules to initialize the shared import mechanism for modules 
00091   //that must not be imported twice
00092   PyObjWrapper m2( PyObject_CallMethod( m1, (char*)"init_shared_modules", (char*)"O", KERNEL_PYTHON::salome_shared_modules_module ) );
00093   if ( !m2 )
00094   {
00095     MESSAGE( "initContext: problem with init_shared_modules call" );
00096     PyErr_Print();
00097     ASSERT( 0 );
00098     return false;
00099   }
00100 
00101   return true;
00102 }
00103 
00108 void SalomeApp_PyInterp::initPython()
00109 {
00110   MESSAGE("SalomeApp_PyInterp::initPython");
00111   ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
00112   SCRUTE(KERNEL_PYTHON::_gtstate);
00113   _gtstate=KERNEL_PYTHON::_gtstate;
00114   _interp=KERNEL_PYTHON::_interp;
00115 }
00116 
00120 int SalomeApp_PyInterp::beforeRun()
00121 {
00122   if ( myFirstRun ) {
00123     myFirstRun = false;
00124     int ret = simpleRun( "from Help import *", false );
00125     if ( ret )
00126       return ret;
00127     ret = simpleRun( "import salome", false );
00128     if (ret)
00129       return ret;
00130     ret = simpleRun( "salome.salome_init(0,1)", false );
00131     if (ret)
00132       return ret;
00133   }
00134   return true;
00135 }