Back to index

salome-gui  6.5.0
SALOME_PYQT_PyInterp.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "SALOME_PYQT_PyInterp.h" // this include must be first !!
00021 
00022 #include <SUITApp_init_python.hxx>
00023 #include <PyInterp_Interp.h>
00024 #include <utilities.h>
00025 
00026 #ifndef GUI_DISABLE_CORBA
00027 #include <Container_init_python.hxx>
00028 #endif
00029 
00030 
00031 
00032 
00038 SALOME_PYQT_PyInterp::SALOME_PYQT_PyInterp(): PyInterp_Interp()
00039 {
00040 }
00041 
00042 SALOME_PYQT_PyInterp::~SALOME_PYQT_PyInterp()
00043 {
00044 }
00045 
00046 void SALOME_PYQT_PyInterp::initPython()
00047 {
00048  /*
00049   * Do nothing
00050   * The initialization has been done in main
00051   */
00052   MESSAGE("SALOME_PYQT_PyInterp::initPython");
00053 #ifndef GUI_DISABLE_CORBA
00054   if(SUIT_PYTHON::initialized) {
00055     ASSERT(SUIT_PYTHON::_gtstate); // initialisation in main
00056     SCRUTE(SUIT_PYTHON::_gtstate);
00057     _tstate = SUIT_PYTHON::_gtstate;
00058   }
00059   else {
00060     ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
00061     SCRUTE(KERNEL_PYTHON::_gtstate);
00062     _tstate = KERNEL_PYTHON::_gtstate;
00063   }
00064 #else
00065   SCRUTE(SUIT_PYTHON::_gtstate);
00066   _tstate = SUIT_PYTHON::_gtstate;
00067 #endif
00068 }
00069 
00070 bool SALOME_PYQT_PyInterp::initState()
00071 {
00072  /*
00073   * The GIL is assumed to not be held on the call
00074   * The GIL is acquired in initState and will be held on initState exit
00075   * It is the caller responsability to release the lock on exit if needed
00076   */
00077   PyEval_AcquireThread(_tstate);
00078   SCRUTE(_tstate);
00079   PyEval_ReleaseThread(_tstate);
00080   return true;
00081 }
00082 
00083 
00084 bool SALOME_PYQT_PyInterp::initContext()
00085 {
00086   /*
00087    * The GIL is assumed to be held
00088    * It is the caller responsability to acquire the GIL before calling initContext
00089    * It will still be held on initContext exit
00090    */
00091   _g = PyDict_New();          // create interpreter dictionnary context
00092   PyObject *bimod = PyImport_ImportModule("__builtin__");
00093   PyDict_SetItemString(_g, "__builtins__", bimod);
00094   Py_DECREF(bimod); 
00095   return true;
00096 }
00097 
00098 int SALOME_PYQT_PyInterp::run(const char *command)
00099 {
00100   MESSAGE("compile");
00101   PyObject *code = Py_CompileString((char *)command,"PyGUI",Py_file_input);
00102   if(!code){
00103     // Une erreur s est produite en general SyntaxError
00104     PyErr_Print();
00105     return -1;
00106   }
00107   //#if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
00108   //  PyObject *r = PyEval_EvalCode(code,_g,_g);
00109   //#else
00110   PyObject *r = PyEval_EvalCode((PyCodeObject *)code,_g,_g);
00111   //#endif
00112   Py_DECREF(code);
00113   if(!r){
00114     // Une erreur s est produite a l execution
00115     PyErr_Print();
00116     return -1 ;
00117   }
00118   Py_DECREF(r);
00119   return 0;
00120 }