Problem flushing stderr in embedded python

caroundw5h caroundw5h at yahoo.com
Mon Nov 8 23:48:01 EST 2004


"Farshid Lashkari" <lashkariNO at SPAMworldviz.com> wrote in message news:<VoRjd.33$iY3.7 at trnddc01>...
> Hi,
> 
> My application has python embedded into it. I noticed that when I run any
> python code the output is buffered and doesn't get flushed until my
> application exits. To fix this I simply flush sys.stdout and sys.stderr
> every once in while by using the following code:
> 
> //Get handle to python stdout file and flush it
> PyObject *pyStdout = PySys_GetObject("stdout");
> if(pyStdout && PyFile_Check(pyStdout)) {
>     PyObject *result = PyObject_CallMethod(pyStdout,"flush",NULL);
>     Py_XDECREF(result);
> }
> 
> //Get handle to python stderr file and flush it
> PyObject *pyStderr = PySys_GetObject("stderr");
> if(pyStderr && PyFile_Check(pyStderr)) {
>     PyObject *result = PyObject_CallMethod(pyStderr,"flush",NULL);
>     Py_XDECREF(result);
> }
> 
> This works for stdout but not for stderr. In order to flush stderr I have to
> do:
> 
> PyRun_SimpleString("sys.stderr.flush()");
> 
> Does anybody know why my first method doesn't work with stderr? I checked
> and PyObject_CallMethod is being called for both objects and no errors are
> occuring. The solution I have is acceptable, but I would still like to know
> why my first method didn't work.
> 
> BTW, I'm running python 2.3 on Windows XP
> 
> Thanks,
> 
> Farshid
I'm sorry to notice that you haven't received any responses back to
your questions even though you have found a workaround. I've noticed
this trend quite a lot in the python community and having learned
python first and now C(in order to understand python better) I'm
frankly disspapointed. I believe python and C are a wonderful marriage
but I suppose because many ppl come to python as their intro language
they info on extending and embedding is limited. I myself haven't
started using the python api but will soon, I hope I for one will be
someone who  will be able to provide an answer next time.

Good luck.

BTW: from a C perspective. I don't understand your problem though.
stdout and stderr are the normal output device for your system -
usally your screen. I don't see why you should have to call one via
python sys module.
Perhaps because one doesn't normally flush(stderr) explicity but
flush(stdout).

interested to know as well.



More information about the Python-list mailing list