sys.stderr and PyErr_WriteUnraisable

J

Jan Langer

Hi all,
I am using Python 2.6 and with the following code I expect a different
result:

from test.test_support import captured_output
with captured_output("stderr") as stderr:
def g():
try:
g()
except RuntimeError,e:
pass
g()
print stderr.getvalue()

I expect the ignored exceptions to be printed into the StringIO object
stderr. With this code there are no warnings on the command line, but
getvalue() returns nothing.

I looked a little into Python and the warnings are printed from
PyErr_WriteUnraisable to sys.stderr.

Is this code supposed to work as I expect it or am I missing something?
thanks,
Jan
 
G

Gabriel Genellina

En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
from test.test_support import captured_output
with captured_output("stderr") as stderr:
def g():
try:
g()
except RuntimeError,e:
pass
g()
print stderr.getvalue()

I expect the ignored exceptions to be printed into the StringIO object
stderr. With this code there are no warnings on the command line, but
getvalue() returns nothing.

If sys.stderr is not a real file, no error gets printed.
PyErr_WriteUnraisable calls PyFile_WriteString, which checks for non-file
objects and only writes to them when no error is set.
Seems to be a safety measure.
 
J

Jan Langer

Gabriel said:
En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer


If sys.stderr is not a real file, no error gets printed.
PyErr_WriteUnraisable calls PyFile_WriteString, which checks for
non-file objects and only writes to them when no error is set.
Seems to be a safety measure.

thanks for the quick answer. PyErr_WriteUnraisable calls PyErr_Fetch
first, which should clear the error indicator, and the PyErr_Occurred in
PyFile_WriteString should return false.
 
J

Jan Langer

Jan said:
thanks for the quick answer. PyErr_WriteUnraisable calls PyErr_Fetch
first, which should clear the error indicator, and the PyErr_Occurred in
PyFile_WriteString should return false.

When thinking about it, it might be possible that the writing to the
StringIO object will hit the recursion limit, too. But then I would
expect a inifinte loops of

1 write to stderr
2 raise recursion exception
3 detect the error
4 call writeunraisable
5 write to stderr
6 loop to 2

But I have no overview happens exactly. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top