[issue22298] Lib/warnings.py _show_warning does not protect against being called with a file like object which is closed

Terry J. Reedy report at bugs.python.org
Fri Nov 25 06:33:14 EST 2016


Terry J. Reedy added the comment:

In 3.6, the code in question is

def _showwarnmsg_impl(msg):
    file = msg.file
    if file is None:
        file = sys.stderr
        if file is None:
            # sys.stderr is None when run with pythonw.exe:
            # warnings get lost
            return
    text = _formatwarnmsg(msg)
    try:
        file.write(text)
    except OSError:
        # the file (probably stderr) is invalid - this warning gets lost.
        pass

When the write is attempted, *file* is not None.  It can either be sys.stderr or something else, successfully opened.  If writing to sys.stderr fails, we properly should give up.

If writing to something else fails, I now think we should try to write an exception to stderr.  So I thing the bug here, if any, is unconditionally passing.  Perhaps the exception clause should be replaced (in a new issue) with something like

     except (OSError, ValueError):
        if msg.file is not sys.stderr:
            raise

----------
resolution:  -> not a bug
stage: test needed -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22298>
_______________________________________


More information about the Python-bugs-list mailing list