Back to index

salome-med  6.5.0
spython.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
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 "SPythonInterpreter.hxx"
00021 
00022 #include <Python.h>
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 #include <float.h>
00027 #include <limits>
00028 
00029 static const int MAX_LINE=1024;
00030 
00031 void runInFileAndQuit(PyObject *glob, PyObject *loc, const char *fileName)
00032 {
00033   std::ifstream ifs(fileName);
00034   ifs.exceptions( std::ifstream::badbit );
00035   char *line=new char[MAX_LINE+1];
00036   ParaMEDMEM::SPythonInterpreter interp(glob,loc);
00037   bool isspython;
00038   while(!ifs.eof())
00039     {
00040       ifs.getline(line,MAX_LINE);
00041       interp.run(line,isspython);
00042     }
00043   interp.finishSession();
00044   delete [] line;
00045 }
00046 
00047 void runInInteractiveMode(PyObject *glob, PyObject *loc)
00048 {
00049   std::string s;
00050   char *line=new char[MAX_LINE+1];
00051   ParaMEDMEM::SPythonInterpreter interp(glob,loc);
00052   bool isInterp=true;
00053   while(true)
00054     {
00055       if(isInterp)
00056         std::cout << ">>> ";
00057       else
00058         std::cout << "... ";
00059       std::cin.getline(line,MAX_LINE);
00060       bool isspython;
00061       isInterp=interp.run(line,isspython);
00062       if(isspython)
00063         std::cout << "Spython line detected !" << std::endl;
00064     }
00065     delete [] line;
00066 }
00067 
00068 int main(int argc, char *argv[])
00069 {
00070   if(!Py_IsInitialized())
00071     {
00072       Py_InitializeEx(0); // do not install signal handlers
00073       PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
00074       PyEval_SaveThread(); /* Release the thread state */
00075       //here we do not have the Global Interpreter Lock
00076     }
00077   PyObject *mainmod,*res ;
00078   PyObject *globals;
00079   PyGILState_STATE gstate;
00080   gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
00081   
00082   mainmod = PyImport_AddModule("__main__");
00083   globals = PyModule_GetDict(mainmod);
00084   if (PyDict_GetItemString(globals, "__builtins__") == NULL) 
00085     {
00086       PyObject *bimod = PyImport_ImportModule("__builtin__");
00087       if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
00088         Py_FatalError("can't add __builtins__ to __main__");
00089       Py_DECREF(bimod);
00090     }
00091   res=PyRun_String("import sys",Py_single_input,globals,globals);
00092   res=PyRun_String("from MEDCalculator import *",Py_single_input,globals,globals);
00093   if(!res)
00094     PyErr_Print();
00095   res=PyRun_String("sys.argv=['']",Py_single_input,globals,globals);
00096   switch(argc)
00097     {
00098     case 2:
00099       runInFileAndQuit(globals,globals,argv[1]);
00100       break;
00101     case 1:
00102       runInInteractiveMode(globals,globals);
00103       break;
00104     default:
00105       std::cerr << "Invalid usage of spython !" << std::endl;
00106       return 1;
00107     }
00108   /*PyObject *_bltins = PyEval_GetBuiltins();
00109     res=PyRun_String("def tony(a):\n"
00110     "\tprint \"coucou\"\n"
00111     "\treturn (7*a,)"
00112     ,
00113     Py_single_input,globals,globals );
00114     PyObject* pyfunc=PyDict_GetItemString(globals,"Augustin");
00115     PyObject* args = PyTuple_New(1) ;
00116     PyObject* arg0=PyFloat_FromDouble(6.7);
00117     PyTuple_SetItem(args,0,arg0);
00118     //PyObject_Print(args,stderr,Py_PRINT_RAW);
00119     PyObject* result = PyObject_CallObject( pyfunc , args ) ;
00120     int nres=PyTuple_Size(result);
00121     PyObject* resRk0=PyTuple_GetItem(result,0);
00122     double resCpp=PyFloat_AS_DOUBLE(resRk0);*/
00123   Py_DECREF(globals);
00124   return 0;
00125 }