15 std::ofstream
dg_debugfile(
"/tmp/dynamic-graph-traces.txt",
16 std::ios::trunc& std::ios::out);
22 "from __future__ import print_function\n",
24 "class StdoutCatcher:\n" 25 " def __init__(self):\n" 27 " def write(self, stuff):\n" 28 " self.data = self.data + stuff\n" 33 "stdout_catcher = StdoutCatcher()\n",
34 "stderr_catcher = StdoutCatcher()\n",
36 "sys.stdout = stdout_catcher",
37 "sys.stderr = stderr_catcher"};
39 bool HandleErr(std::string& err, PyObject* globals_,
int PythonInputType) {
44 if (PyErr_Occurred() != NULL) {
45 bool is_syntax_error = PyErr_ExceptionMatches(PyExc_SyntaxError);
47 PyObject* stderr_obj = PyRun_String(
"stderr_catcher.fetch()", Py_eval_input,
50 Py_DECREF(stderr_obj);
56 if (is_syntax_error && PythonInputType == Py_eval_input) {
57 dgDEBUG(15) <<
"Detected a syntax error " << std::endl;
64 dgDEBUG(15) <<
"no object generated but no error occured." << std::endl;
73 dlopen(PYTHON_LIBRARY, RTLD_LAZY | RTLD_GLOBAL);
76 #if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7 79 mainmod_ = PyImport_AddModule(
"__main__");
84 PyRun_SimpleString(pythonPrefix[0].c_str());
85 PyRun_SimpleString(pythonPrefix[1].c_str());
86 PyRun_SimpleString(pythonPrefix[2].c_str());
87 PyRun_SimpleString(pythonPrefix[3].c_str());
88 PyRun_SimpleString(pythonPrefix[4].c_str());
89 PyRun_SimpleString(pythonPrefix[5].c_str());
90 PyRun_SimpleString(pythonPrefix[6].c_str());
91 PyRun_SimpleString(pythonPrefix[7].c_str());
92 PyRun_SimpleString(
"import linecache");
107 PyObject* poAttrList = PyObject_Dir(
mainmod_);
108 PyObject* poAttrIter = PyObject_GetIter(poAttrList);
109 PyObject* poAttrName;
111 while ((poAttrName = PyIter_Next(poAttrIter)) != NULL) {
112 std::string oAttrName(
obj_to_str(poAttrName));
115 if (oAttrName.compare(0, 2,
"__") != 0 ||
116 oAttrName.compare(oAttrName.size() - 2, 2,
"__") != 0) {
117 PyObject* poAttr = PyObject_GetAttr(
mainmod_, poAttrName);
120 if (poAttr && poAttr->ob_type !=
mainmod_->ob_type)
121 PyObject_SetAttr(
mainmod_, poAttrName, NULL);
126 Py_DECREF(poAttrName);
129 Py_DECREF(poAttrIter);
130 Py_DECREF(poAttrList);
139 std::string lerr(
""), lout(
""), lres(
"");
140 python(command, lres, lout, lerr);
145 std::string& out, std::string& err) {
151 std::string::size_type iFirstNonWhite = command.find_first_not_of(
" \t");
153 if (iFirstNonWhite == std::string::npos)
return;
155 if (command[iFirstNonWhite] ==
'#')
return;
159 std::cout << command.c_str() << std::endl;
163 if (result == NULL) {
166 if (!
HandleErr(err, globals_, Py_eval_input)) {
172 if (result == NULL)
HandleErr(err, globals_, Py_single_input);
178 dgDEBUG(15) <<
"Do not try a second time." << std::endl;
181 PyObject* stdout_obj = 0;
183 PyRun_String(
"stdout_catcher.fetch()", Py_eval_input, globals_, globals_);
185 Py_DECREF(stdout_obj);
187 if (out.size() != 0) std::cout <<
"Output:" << out << std::endl;
188 if (err.size() != 0) std::cout <<
"Error:" << err << std::endl;
191 dgDEBUG(15) <<
"For command: " << command << std::endl;
192 if (result != NULL) {
194 dgDEBUG(15) <<
"Result is: " << res << std::endl;
197 dgDEBUG(15) <<
"Result is: empty" << std::endl;
199 dgDEBUG(15) <<
"Out is: " << out << std::endl;
200 dgDEBUG(15) <<
"Err is :" << err << std::endl;
210 std::string err =
"";
215 FILE* pFile = fopen(filename.c_str(),
"r");
217 err = filename +
" cannot be open";
228 std::cerr << err << std::endl;
238 #if PY_MAJOR_VERSION >= 3 239 const Py_UNICODE* argv[] = {
L"dg-embedded-pysh"};
240 Py_Main(1, const_cast<Py_UNICODE**>(argv));
242 const char* argv[] = {
"dg-embedded-pysh"};
243 Py_Main(1, const_cast<char**>(argv));
250 sprintf(line,
"%s",
"\n");
252 std::streamsize maxSize = 10000;
254 while (line != std::string(
"")) {
255 stream.getline(line, maxSize,
'\n');
256 command += std::string(line) + std::string(
"\n");
260 stream.getline(line, maxSize,
';');
261 command += std::string(line);
std::ofstream dg_debugfile("/tmp/dynamic-graph-traces.txt", std::ios::trunc &std::ios::out)
PyObject * globals_
Pointer to the dictionary of global variables.
std::string python(const std::string &command)
Method to start python interperter.
static const std::string pythonPrefix[8]
bool HandleErr(std::string &err, PyObject *globals_, int PythonInputType)
PyObject * globals()
Return a pointer to the dictionary of global variables.
std::string obj_to_str(PyObject *o)
void runPythonFile(std::string filename)
Method to exectue a python script.
PyThreadState * _pyState
The Python thread state.
std::string processStream(std::istream &stream, std::ostream &os)
Process input stream to send relevant blocks to python.