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