sys.stderr and PyErr_WriteUnraisable

Discussion in 'Python' started by Jan Langer, Dec 15, 2009.

  1. Jan Langer

    Jan Langer Guest

    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
     
    Jan Langer, Dec 15, 2009
    #1
    1. Advertising

  2. En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
    <-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
     
    Gabriel Genellina, Dec 15, 2009
    #2
    1. Advertising

  3. Jan Langer

    Jan Langer Guest

    Gabriel Genellina schrieb:
    > En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
    > <-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, Dec 15, 2009
    #3
  4. Jan Langer

    Jan Langer Guest

    Jan Langer schrieb:
    > Gabriel Genellina schrieb:
    >> En Tue, 15 Dec 2009 11:17:20 -0300, Jan Langer
    >> <-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. :)
     
    Jan Langer, Dec 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    663
    Roman Neuhauser
    Apr 4, 2005
  2. GinTon

    sys.stderr.write and sys.exit

    GinTon, Nov 23, 2006, in forum: Python
    Replies:
    5
    Views:
    582
    Dennis Lee Bieber
    Nov 24, 2006
  3. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    572
    Nobody
    Jan 11, 2010
  4. Michel Albert
    Replies:
    5
    Views:
    1,418
    Daniel Dehennin
    Oct 24, 2012
  5. Geoff Bache
    Replies:
    4
    Views:
    563
Loading...

Share This Page