Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   sys.stderr and PyErr_WriteUnraisable (http://www.velocityreviews.com/forums/t708800-sys-stderr-and-pyerr_writeunraisable.html)

Jan Langer 12-15-2009 02:17 PM

sys.stderr and PyErr_WriteUnraisable
 
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

Gabriel Genellina 12-15-2009 03:44 PM

Re: sys.stderr and PyErr_WriteUnraisable
 
En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
<jan.langer@etit.tu-chemnitz.de> escribió:

> 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.

--
Gabriel Genellina


Jan Langer 12-15-2009 04:20 PM

Re: sys.stderr and PyErr_WriteUnraisable
 
Gabriel Genellina schrieb:
> En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
> <jan.langer@etit.tu-chemnitz.de> escribió:
>
>> 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.
>


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.

Jan Langer 12-15-2009 04:44 PM

Re: sys.stderr and PyErr_WriteUnraisable
 
Jan Langer schrieb:
> Gabriel Genellina schrieb:
>> En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
>> <jan.langer@etit.tu-chemnitz.de> escribió:
>>
>>> 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.
>>

>
> 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. :-)


All times are GMT. The time now is 02:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57