1 // Copyright (C) 2006-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PyStdout.hxx"
21 #include "Exception.hxx"
22 #include "AutoGIL.hxx"
24 #include <structmember.h>
31 #define getpid _getpid
46 PyStdOut_dealloc(PyStdOut *self)
52 PyStdOut_write(PyStdOut *self, PyObject *args)
56 if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
60 *(self->out)=*(self->out)+std::string(c);
66 static PyMethodDef PyStdOut_methods[] = {
67 {"write", (PyCFunction)PyStdOut_write, METH_VARARGS,
68 PyDoc_STR("write(string) -> None")},
69 {NULL, NULL} /* sentinel */
72 static PyMemberDef PyStdOut_memberlist[] = {
73 {(char*)"softspace", T_INT, offsetof(PyStdOut, softspace), 0,
74 (char*)"flag indicating that a space needs to be printed; used by print"},
78 static PyTypeObject PyStdOut_Type = {
79 /* The ob_type field must be initialized in the module init function
80 * to be portable to Windows without using C++. */
81 PyObject_HEAD_INIT(NULL)
84 sizeof(PyStdOut), /*tp_basicsize*/
87 (destructor)PyStdOut_dealloc, /*tp_dealloc*/
99 PyObject_GenericGetAttr, /*tp_getattro*/
100 /* softspace is writable: we must supply tp_setattro */
101 PyObject_GenericSetAttr, /* tp_setattro */
103 Py_TPFLAGS_DEFAULT, /*tp_flags*/
107 0, /*tp_richcompare*/
108 0, /*tp_weaklistoffset*/
111 PyStdOut_methods, /*tp_methods*/
112 PyStdOut_memberlist, /*tp_members*/
127 #define PyStdOut_Check(v) ((v)->ob_type == &PyStdOut_Type)
129 PyObject * newPyStdOut( std::string& out )
132 self = PyObject_New(PyStdOut, &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 std::ostringstream stream;
155 stream << "/tmp/PythonNode_";
157 AutoPyRef context(PyDict_New());
158 PyDict_SetItemString( context, "__builtins__", PyEval_GetBuiltins() );
159 AutoPyRef code(Py_CompileString(zeCodeStr.c_str(), "kkkk", Py_file_input));
162 std::string errorDetails;
163 PyObject *new_stderr(newPyStdOut(errorDetails));
164 PySys_SetObject((char*)"stderr", new_stderr);
166 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
167 Py_DECREF(new_stderr);
168 std::ostringstream oss; oss << "evalPy failed : " << errorDetails;
169 throw Exception(oss.str());
171 AutoPyRef res(PyEval_EvalCode(reinterpret_cast<PyCodeObject *>((PyObject *)code),context,context));
172 PyObject *ret(PyDict_GetItemString(context,funcName.c_str())); //borrowed ref
174 throw YACS::Exception("evalPy : Error on returned func !");
179 PyObject *evalFuncPyWithNoParams(PyObject *func)
182 throw YACS::Exception("evalFuncPyWithNoParams : input func is NULL !");
183 AutoPyRef args(PyTuple_New(0));
184 AutoPyRef ret(PyObject_CallObject(func,args));
186 throw YACS::Exception("evalFuncPyWithNoParams : ret is null !");