#include <Python.h>
typedef struct {
PyObject_HEAD
} MyStdout;
static PyObject *MyStdout_write(MyStdout* self, PyObject *args)
{
char *data = PyString_AsString(args);
if (data){
printf("%s", data);
}
Py_RETURN_NONE;
}
static PyMethodDef MyStdout_methods[] = {
{"write", (PyCFunction)MyStdout_write, METH_O, ""},
{NULL}
};
static PyTypeObject MyStdoutType = {
PyObject_HEAD_INIT(NULL)
0,
"MyStdout",
sizeof(MyStdout),
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
Py_TPFLAGS_DEFAULT,
"",
0,
0,
0,
0,
0,
0,
MyStdout_methods,
};
PyObject *mystdout = NULL;
Py_Initialize();
if (PyType_Ready(&MyStdoutType) < 0){
PyErr_Clear();
goto fail;
}
mystdout = _PyObject_New(&MyStdoutType);
if (PyErr_Occurred() || mystdout == NULL){
PyErr_Clear();
goto fail;
}
PySys_SetObject("stdout", mystdout);
if (PyErr_Occurred()){
PyErr_Clear();
goto fail;
}
PySys_SetObject("stderr", mystdout);
if (PyErr_Occurred()){
PyErr_Clear();
goto fail;
}
Py_XDECREF(mystdout);
fail:
Py_XDECREF(mystdout);