Terry J. Reedy added the comment:

If sys.stderr is set to None, then "if file is None: file = sys.stderr" will 
have no effect and 'file.write' raises "AttributeError: 'NoneType' object has 
no attribute 'write'" (actual quote).  Do you propose to catch that?  If not, 
it is not much of an improvement. Multiple people have reported on python-list 
that they are mystified by messages like this when they never used a None 
object as an argument.

I am sympathetic to not overlooking bugs.  However, a traceback is normally 
lost in this situation anyway.  If sys.stderr is closed, trying to write a 
ValueError traceback (or any other traceback) to stderr generates another 
ValueError.  Python apparently tries to write a double traceback to 
sys.__stderr__ as a backup (see below).  If that is also unusable (perhaps 
because it *is* sys.stderr), then the traceback disappears anyway.  This is 
what happens (on Windows, at least) with python started from either an icon or 
in Command Prompt.

>>> import sys; sys.stderr.close()
>>> from warnings import warn; warn('foo')
>>> sys.stderr is sys.__stderr__
True

When Idle starts an icon, the result is the same except that the user process 
is restarted.  Also, two streams are not the same; sys.__stderr__ is None.

If Idle is run with python from a console, there is still no message in the 
Idle shell but since sys.__stderr__ points back to the console, the double 
traceback appears there: first the warn ValueError, then 'while 
proccessing...', the traceback ValueError.  Idle replaces showwarning.  I 
believe it now only changes the formatting, but I could have it use 
sys.__stderr__ when not None to print the warning instead of or in addition to 
the tracebacks.

If the code above is placed in a file and run in batch mode, python quits with 
no output.

C:\Programs\Python34>python temop.py

C:\Programs\Python34>

When run from the idle editor running under pythonw, the user process does the 
same (exit without output).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to