This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Julius.Lehmann-Richter
Recipients Julius.Lehmann-Richter
Date 2014-08-29.12:29:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1409315343.62.0.0672695786427.issue22298@psf.upfronthosting.co.za>
In-reply-to
Content
In Lib/warnings.py the _show_warning function catches IOError with the commented intention to ward against an "invalid file":

def _show_warning(message, category, filename, lineno, file=None, line=None):
    """Hook to write a warning to a file; replace if you like."""
    if file is None:
        file = sys.stderr
    try:
        file.write(formatwarning(message, category, filename, lineno, line))
    except IOError:
        pass # the file (probably stderr) is invalid - this warning gets lost.

If for some reason the file like object, and in the default case stderr, is closed, a calling program is faced with a ValueError, which is not being caught.

It seems to me, and correct me if I am wrong, that a file object which has been closed is a case of an "invalid file" and that the warning subsystem should in that case behave in the same manner as in the case of the IOError.

This behavior is the same for python 3.2 with the function renamed to showwarning and can be reproduced with for example

from sys import stderr                      
from warnings import warn
                     
stderr.close()
try:
    warn("foo")
except ValueError as e:
    print(e)
History
Date User Action Args
2014-08-29 12:29:03Julius.Lehmann-Richtersetrecipients: + Julius.Lehmann-Richter
2014-08-29 12:29:03Julius.Lehmann-Richtersetmessageid: <1409315343.62.0.0672695786427.issue22298@psf.upfronthosting.co.za>
2014-08-29 12:29:03Julius.Lehmann-Richterlinkissue22298 messages
2014-08-29 12:29:03Julius.Lehmann-Richtercreate