1 // Copyright (C) 2007-2013 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
19 // Author : Anthony Geay (CEA/DEN)
21 #include "SPythonInterpreter.hxx"
30 static const int MAX_LINE=1024;
32 void runInFileAndQuit(PyObject *glob, PyObject *loc, const char *fileName)
34 std::ifstream ifs(fileName);
35 ifs.exceptions( std::ifstream::badbit );
36 char *line=new char[MAX_LINE+1];
37 ParaMEDMEM::SPythonInterpreter interp(glob,loc);
41 ifs.getline(line,MAX_LINE);
42 interp.run(line,isspython);
44 interp.finishSession();
48 void runInInteractiveMode(PyObject *glob, PyObject *loc)
51 char *line=new char[MAX_LINE+1];
52 ParaMEDMEM::SPythonInterpreter interp(glob,loc);
60 std::cin.getline(line,MAX_LINE);
62 isInterp=interp.run(line,isspython);
64 std::cout << "Spython line detected !" << std::endl;
69 int main(int argc, char *argv[])
71 if(!Py_IsInitialized())
73 Py_InitializeEx(0); // do not install signal handlers
74 PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
75 PyEval_SaveThread(); /* Release the thread state */
76 //here we do not have the Global Interpreter Lock
78 PyObject *mainmod,*res ;
80 PyGILState_STATE gstate;
81 gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
83 mainmod = PyImport_AddModule("__main__");
84 globals = PyModule_GetDict(mainmod);
85 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
87 PyObject *bimod = PyImport_ImportModule("__builtin__");
88 if (bimod == NULL || PyDict_SetItemString(globals, "__builtins__", bimod) != 0)
89 Py_FatalError("can't add __builtins__ to __main__");
92 res=PyRun_String("import sys",Py_single_input,globals,globals);
93 res=PyRun_String("from MEDCalculator import *",Py_single_input,globals,globals);
96 res=PyRun_String("sys.argv=['']",Py_single_input,globals,globals);
100 runInFileAndQuit(globals,globals,argv[1]);
103 runInInteractiveMode(globals,globals);
106 std::cerr << "Invalid usage of spython !" << std::endl;
109 /*PyObject *_bltins = PyEval_GetBuiltins();
110 res=PyRun_String("def tony(a):\n"
111 "\tprint \"coucou\"\n"
114 Py_single_input,globals,globals );
115 PyObject* pyfunc=PyDict_GetItemString(globals,"Augustin");
116 PyObject* args = PyTuple_New(1) ;
117 PyObject* arg0=PyFloat_FromDouble(6.7);
118 PyTuple_SetItem(args,0,arg0);
119 //PyObject_Print(args,stderr,Py_PRINT_RAW);
120 PyObject* result = PyObject_CallObject( pyfunc , args ) ;
121 int nres=PyTuple_Size(result);
122 PyObject* resRk0=PyTuple_GetItem(result,0);
123 double resCpp=PyFloat_AS_DOUBLE(resRk0);*/