1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 * ParametersPlugin_PyInterp.cpp
5 * Created on: Apr 2, 2015
9 #include <ParametersPlugin_PyInterp.h>
14 ParametersPlugin_PyInterp::ParametersPlugin_PyInterp()
19 ParametersPlugin_PyInterp::~ParametersPlugin_PyInterp()
23 const char* aSearchCode =
25 "class FindName(ast.NodeVisitor):\n"
26 " def __init__(self, name):\n"
28 " def visit_Name(self, node):\n"
29 " if node.id == self.name:\n"
30 " positions.append((node.lineno, node.col_offset))\n"
31 "FindName(name).visit(ast.parse(expression))";
33 std::list<std::pair<int, int> >
34 ParametersPlugin_PyInterp::positions(const std::string& theExpression,
35 const std::string& theName)
37 PyLockWrapper lck; // Acquire GIL until the end of the method
39 std::list<std::pair<int, int> > aResult;
42 PyObject* aContext = PyDict_New();
43 PyObject* aBuiltinModule = PyImport_AddModule("__builtin__");
44 PyDict_SetItemString(aContext, "__builtins__", aBuiltinModule);
46 // extend aContext with variables
47 PyDict_SetItemString(aContext, "expression", PyString_FromString(theExpression.c_str()));
48 PyDict_SetItemString(aContext, "name", PyString_FromString(theName.c_str()));
49 PyDict_SetItemString(aContext, "positions", Py_BuildValue("[]"));
51 // run the search code
52 PyObject* aExecResult = PyRun_String(aSearchCode, Py_file_input, aContext, aContext);
53 Py_XDECREF(aExecResult);
55 // receive results from context
56 PyObject* aPositions = PyDict_GetItemString(aContext, "positions");
57 for (int anIndex = 0; anIndex < PyList_Size(aPositions); ++anIndex) {
58 PyObject* aPosition = PyList_GetItem(aPositions, anIndex);
59 PyObject* aLineNo = PyTuple_GetItem(aPosition, 0);
60 PyObject* aColOffset = PyTuple_GetItem(aPosition, 1);
63 std::pair<int, int>((int)PyInt_AsLong(aLineNo),
64 (int)PyInt_AsLong(aColOffset)));
67 // TODO(spo): after this refCount of the variable is not 0. Is there memory leak?
74 std::list<std::string> ParametersPlugin_PyInterp::compile(const std::string& theExpression)
76 PyLockWrapper lck; // Acquire GIL until the end of the method
77 std::list<std::string> aResult;
78 PyObject *aCodeopModule = PyImport_AddModule("codeop");
79 if(!aCodeopModule) { // Fatal error. No way to go on.
84 PyObject *aCodePyObj =
85 PyObject_CallMethod(aCodeopModule, (char*)"compile_command", (char*)"(s)",
86 theExpression.c_str());
88 if(!aCodePyObj || aCodePyObj == Py_None || !PyCode_Check(aCodePyObj)) {
89 Py_XDECREF(aCodePyObj);
93 PyCodeObject* aCodeObj = (PyCodeObject*) aCodePyObj;
94 std::string aCodeName(PyString_AsString(aCodeObj->co_code));
95 // co_names should be tuple, but can be changed in modern versions of python (>2.7.3)
96 if(!PyTuple_Check(aCodeObj->co_names))
99 size_t params_size = PyTuple_Size(aCodeObj->co_names);
100 if (params_size > 0) {
101 for (size_t i = 0; i < params_size; i++) {
102 PyObject* aParamObj = PyTuple_GetItem(aCodeObj->co_names, i);
103 PyObject* aParamObjStr = PyObject_Str(aParamObj);
104 std::string aParamName(PyString_AsString(aParamObjStr));
105 aResult.push_back(aParamName);
106 Py_XDECREF(aParamObjStr);
109 Py_XDECREF(aCodeObj);
113 void ParametersPlugin_PyInterp::extendLocalContext(const std::list<std::string>& theParameters)
115 PyLockWrapper lck; // Acquire GIL until the end of the method
116 if (theParameters.empty())
118 std::list<std::string>::const_iterator it = theParameters.begin();
119 for ( ; it != theParameters.cend(); it++) {
120 std::string aParamValue = *it;
121 simpleRun(aParamValue.c_str(), false);
125 void ParametersPlugin_PyInterp::clearLocalContext()
128 PyDict_Clear(_local_context);
131 double ParametersPlugin_PyInterp::evaluate(const std::string& theExpression, std::string& theError)
133 PyLockWrapper lck; // Acquire GIL until the end of the method
134 PyCompilerFlags aFlags = {CO_FUTURE_DIVISION};
135 aFlags.cf_flags = CO_FUTURE_DIVISION;
136 PyCodeObject* anExprCode = (PyCodeObject *) Py_CompileStringFlags(theExpression.c_str(),
137 "<string>", Py_eval_input, &aFlags);
139 theError = errorMessage();
140 Py_XDECREF(anExprCode);
144 PyObject* anEvalResult = PyEval_EvalCode(anExprCode, _global_context, _local_context);
146 theError = errorMessage();
147 Py_XDECREF(anExprCode);
148 Py_XDECREF(anEvalResult);
152 PyObject* anEvalStrObj = PyObject_Str(anEvalResult);
153 std::string anEvalStr(PyString_AsString(anEvalStrObj));
154 Py_XDECREF(anExprCode);
155 Py_XDECREF(anEvalResult);
156 Py_XDECREF(anEvalStrObj);
159 result = std::stod(anEvalStr);
160 } catch (const std::invalid_argument&) {
161 theError = "Unable to eval " + anEvalStr;
167 std::string ParametersPlugin_PyInterp::errorMessage()
169 std::string aPyError;
170 if (PyErr_Occurred()) {
171 PyObject *pstr, *ptype, *pvalue, *ptraceback;
172 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
173 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
174 pstr = PyObject_Str(pvalue);
175 aPyError = std::string(PyString_AsString(pstr));
179 Py_XDECREF(ptraceback);
184 bool ParametersPlugin_PyInterp::initContext()
186 PyObject *m = PyImport_AddModule("__main__"); // interpreter main module (module context)
191 _global_context = PyModule_GetDict(m); // get interpreter global variable context
192 Py_INCREF(_global_context);
193 _local_context = PyDict_New();
194 Py_INCREF(_local_context);
196 return PyRun_SimpleString("from math import *") == 0;
199 void ParametersPlugin_PyInterp::closeContext()
201 Py_XDECREF(_local_context);
202 PyInterp_Interp::closeContext();