Back to index

salome-gui  6.5.0
PyConsole_Interp.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   : PyConsole_Interp.cxx
00025 //  Author : Nicolas REJNERI
00026 //  Module : SALOME
00027 //
00028 #include "PyConsole_Interp.h"
00029 
00073 PyConsole_Interp::PyConsole_Interp(): PyInterp_Interp()
00074 {
00075 }
00076 
00082 PyConsole_Interp::~PyConsole_Interp()
00083 {
00084 }
00085  
00094 bool PyConsole_Interp::initState()
00095 {
00096   PyEval_AcquireLock();
00097   _tstate = Py_NewInterpreter(); // create an interpreter and save current state
00098   PySys_SetArgv(PyInterp_Interp::_argc,PyInterp_Interp::_argv); // initialize sys.argv
00099   
00100   if(!builtinmodule) // PAL18041: deepcopy function don't work in Salome
00101   {
00102     //builtinmodule is static member of PyInterp class
00103     //If it is not NULL (initialized to the builtin module of the main interpreter
00104     //all the sub interpreters will have the same builtin
00105     //_interp is a static member and is the main interpreter
00106     //The first time we initialized it to the builtin of main interpreter
00107     builtinmodule=PyDict_GetItemString(_interp->modules, "__builtin__");
00108   }
00109 
00110   //If builtinmodule has been initialized all the sub interpreters
00111   // will have the same __builtin__ module
00112   if(builtinmodule){ 
00113     PyObject *m = PyImport_GetModuleDict();
00114     PyDict_SetItemString(m, "__builtin__", builtinmodule);
00115     _tstate->interp->builtins = PyModule_GetDict(builtinmodule);
00116     Py_INCREF(_tstate->interp->builtins);
00117   }
00118   PyEval_ReleaseThread(_tstate);
00119   return true;
00120 }
00121 
00131 bool PyConsole_Interp::initContext()
00132 {
00133   PyObject *m = PyImport_AddModule("__main__");  // interpreter main module (module context)
00134   if(!m){
00135     PyErr_Print();
00136     return false;
00137   }  
00138   _g = PyModule_GetDict(m);          // get interpreter dictionnary context
00139 
00140   if(builtinmodule){
00141     PyDict_SetItemString(_g, "__builtins__", builtinmodule); // assign singleton __builtin__ module
00142   }
00143   return true;
00144 }