1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SPythonInterpreter.hxx"
29 static const int MAX_LINE=1024;
31 void runInFileAndQuit(PyObject *glob, PyObject *loc, const char *fileName)
33 std::ifstream ifs(fileName);
34 ifs.exceptions( std::ifstream::badbit );
35 char *line=new char[MAX_LINE+1];
36 ParaMEDMEM::SPythonInterpreter interp(glob,loc);
40 ifs.getline(line,MAX_LINE);
41 interp.run(line,isspython);
43 interp.finishSession();
47 void runInInteractiveMode(PyObject *glob, PyObject *loc)
50 char *line=new char[MAX_LINE+1];
51 ParaMEDMEM::SPythonInterpreter interp(glob,loc);
59 std::cin.getline(line,MAX_LINE);
61 isInterp=interp.run(line,isspython);
63 std::cout << "Spython line detected !" << std::endl;
68 int main(int argc, char *argv[])
70 if(!Py_IsInitialized())
72 Py_InitializeEx(0); // do not install signal handlers
73 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
74 PyEval_SaveThread(); /* Release the thread state */
75 //here we do not have the Global Interpreter Lock
77 PyObject *mainmod,*res ;
79 PyGILState_STATE gstate;
80 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
82 mainmod = PyImport_AddModule("__main__");
83 globals = PyModule_GetDict(mainmod);
84 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
86 PyObject *bimod = PyImport_ImportModule("__builtin__");
87 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
88 Py_FatalError("can't add __builtins__ to __main__");
91 res=PyRun_String("import sys",Py_single_input,globals,globals);
92 res=PyRun_String("from MEDCalculator import *",Py_single_input,globals,globals);
95 res=PyRun_String("sys.argv=['']",Py_single_input,globals,globals);
99 runInFileAndQuit(globals,globals,argv[1]);
102 runInInteractiveMode(globals,globals);
105 std::cerr << "Invalid usage of spython !" << std::endl;
108 /*PyObject *_bltins = PyEval_GetBuiltins();
109 res=PyRun_String("def tony(a):\n"
110 "\tprint \"coucou\"\n"
113 Py_single_input,globals,globals );
114 PyObject* pyfunc=PyDict_GetItemString(globals,"Augustin");
115 PyObject* args = PyTuple_New(1) ;
116 PyObject* arg0=PyFloat_FromDouble(6.7);
117 PyTuple_SetItem(args,0,arg0);
118 //PyObject_Print(args,stderr,Py_PRINT_RAW);
119 PyObject* result = PyObject_CallObject( pyfunc , args ) ;
120 int nres=PyTuple_Size(result);
121 PyObject* resRk0=PyTuple_GetItem(result,0);
122 double resCpp=PyFloat_AS_DOUBLE(resRk0);*/