Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDCalculator / Swig / spython.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SPythonInterpreter.hxx"
21
22 #include <Python.h>
23
24 #include <iostream>
25 #include <fstream>
26 #include <float.h>
27 #include <limits>
28
29 static const int MAX_LINE=1024;
30
31 void runInFileAndQuit(PyObject *glob, PyObject *loc, const char *fileName)
32 {
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);
37   bool isspython;
38   while(!ifs.eof())
39     {
40       ifs.getline(line,MAX_LINE);
41       interp.run(line,isspython);
42     }
43   interp.finishSession();
44   delete [] line;
45 }
46
47 void runInInteractiveMode(PyObject *glob, PyObject *loc)
48 {
49   std::string s;
50   char *line=new char[MAX_LINE+1];
51   ParaMEDMEM::SPythonInterpreter interp(glob,loc);
52   bool isInterp=true;
53   while(true)
54     {
55       if(isInterp)
56         std::cout << ">>> ";
57       else
58         std::cout << "... ";
59       std::cin.getline(line,MAX_LINE);
60       bool isspython;
61       isInterp=interp.run(line,isspython);
62       if(isspython)
63         std::cout << "Spython line detected !" << std::endl;
64     }
65     delete [] line;
66 }
67
68 int main(int argc, char *argv[])
69 {
70   if(!Py_IsInitialized())
71     {
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
76     }
77   PyObject *mainmod,*res ;
78   PyObject *globals;
79   PyGILState_STATE gstate;
80   gstate = PyGILState_Ensure(); // acquire the Global Interpreter Lock
81   
82   mainmod = PyImport_AddModule("__main__");
83   globals = PyModule_GetDict(mainmod);
84   if (PyDict_GetItemString(globals, "__builtins__") == NULL) 
85     {
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__");
89       Py_DECREF(bimod);
90     }
91   res=PyRun_String("import sys",Py_single_input,globals,globals);
92   res=PyRun_String("from MEDCalculator import *",Py_single_input,globals,globals);
93   if(!res)
94     PyErr_Print();
95   res=PyRun_String("sys.argv=['']",Py_single_input,globals,globals);
96   switch(argc)
97     {
98     case 2:
99       runInFileAndQuit(globals,globals,argv[1]);
100       break;
101     case 1:
102       runInInteractiveMode(globals,globals);
103       break;
104     default:
105       std::cerr << "Invalid usage of spython !" << std::endl;
106       return 1;
107     }
108   /*PyObject *_bltins = PyEval_GetBuiltins();
109     res=PyRun_String("def tony(a):\n"
110     "\tprint \"coucou\"\n"
111     "\treturn (7*a,)"
112     ,
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);*/
123   Py_DECREF(globals);
124   return 0;
125 }