22 #include "PythonCppUtils.hxx"
24 #include <structmember.h>
31 #define getpid _getpid
56 if (!PyArg_ParseTuple(
args,
"s#:write",&
c, &
l))
60 *(
self->out)=*(self->out)+std::string(
c);
68 PyDoc_STR(
"write(string) -> None")},
73 {(
char*)
"softspace", T_INT, offsetof(
PyStdOut, softspace), 0,
74 (
char*)
"flag indicating that a space needs to be printed; used by print"},
81 PyVarObject_HEAD_INIT(NULL, 0)
99 PyObject_GenericGetAttr,
101 PyObject_GenericSetAttr,
127 #define PyStdOut_Check(v) ((v)->ob_type == &PyStdOut_Type)
137 return (PyObject*)
self;
140 PyObject *
evalPy(
const std::string& funcName,
const std::string& strToEval)
142 std::ostringstream oss0; oss0 <<
"def " << funcName <<
"():\n";
143 std::string::size_type i0(0);
144 while(i0<strToEval.length() && i0!=std::string::npos)
146 std::string::size_type i2(strToEval.find(
'\n',i0));
147 std::string::size_type lgth(i2!=std::string::npos?i2-i0:std::string::npos);
148 std::string part(strToEval.substr(i0,lgth));
150 oss0 <<
" " << part <<
"\n";
151 i0=i2!=std::string::npos?i2+1:std::string::npos;
153 std::string zeCodeStr(oss0.str());
154 AutoPyRef context(PyDict_New());
155 PyDict_SetItemString( context,
"__builtins__", PyEval_GetBuiltins() );
156 AutoPyRef code(Py_CompileString(zeCodeStr.c_str(),
"kkkk", Py_file_input));
159 std::string errorDetails;
161 PySys_SetObject((
char*)
"stderr", new_stderr);
163 PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
164 Py_DECREF(new_stderr);
165 std::ostringstream oss; oss <<
"evalPy failed : " << errorDetails;
168 AutoPyRef res(PyEval_EvalCode((PyObject *)code,context,context));
169 PyObject *ret(PyDict_GetItemString(context,funcName.c_str()));
179 throw YACS::Exception(
"evalFuncPyWithNoParams : input func is NULL !");
180 AutoPyRef
args(PyTuple_New(0));
181 AutoPyRef ret(PyObject_CallObject(func,
args));
static void PyStdOut_dealloc(PyStdOut *self)
PyObject * evalFuncPyWithNoParams(PyObject *func)
static PyObject * PyStdOut_write(PyStdOut *self, PyObject *args)
PyObject * evalPy(const std::string &funcName, const std::string &strToEval)
PyObject * newPyStdOut(std::string &out)
static PyTypeObject PyStdOut_Type
static PyMethodDef PyStdOut_methods[]
static PyMemberDef PyStdOut_memberlist[]
PyObject_HEAD int softspace