[New-bugs-announce] [issue22298] Lib/warnings.py _show_warning does not protect against being called with a file like object which is closed
Julius Lehmann-Richter
report at bugs.python.org
Fri Aug 29 14:29:03 CEST 2014
New submission from Julius Lehmann-Richter:
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)
----------
components: Library (Lib)
messages: 226058
nosy: Julius.Lehmann-Richter
priority: normal
severity: normal
status: open
title: Lib/warnings.py _show_warning does not protect against being called with a file like object which is closed
type: behavior
versions: Python 2.7, Python 3.2
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22298>
_______________________________________
More information about the New-bugs-announce
mailing list