This is a combination of my two recent questions:
[1] http://stackoverflow.com/questions/1954494/python-instance-method-in-c
[2] http://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python
I would like to log the output of both stdout and stderr from a python script.
The thing I want to ask is, to create a new type according to [1] seems fairly complicated. Does it simplifies the things if there was no need to expose the new type to Python, i.e. it would only exist in C?
I mean, when Python prints something it goes to "Objects/fileobject.c" and there in "PyFile_WriteObject" it check whether it is possible to write to its argument:
writer = PyObject_GetAttrString(f, "write");
if (writer == NULL)
...
Also, it is possible to get stdout and stderr like this:
PyObject* out = PySys_GetObject("stdout");
PyObject* err = PySys_GetObject("stderr");
My question is then, is it somehow possible to construct necessary PyObject which satisfies the above 'PyObject_GetAttrString(f, "write")' and is callable so I can write:
PySys_SetObject("stdout", <my writer object / class / type / ?>);
http://docs.python.org/c-api/sys.html?highlight=pysys%5Fsetobject#PySys%5FSetObject
This way, there would be no need to expose the new "writer type" to the rest of Python script so I thought it might be a bit simpler to write the code...?