New submission from Luiz <eldorado.l...@gmail.com>:

If you use the pprint function when running on windowed mode (either by using 
the .pyw extension or running the file with pythonw) a "AttributeError" 
exception occurs, saying that "'NoneType' object has no attribute 'write'".

Looking at the source code (Lib/pprint.py), it happens because in this mode 
sys.stdout is set to None, therefore it has no write attribute.

I think intended behavior is to simply ignore any console output when in this 
mode, since many programs have loads of print statements for debugging with a 
console window; it shouldn't crash because of a extension change.

There's two solutions I can think of. One is to check if sys.stdout is None, 
not printing anything in case it is. Another is, when in windowed mode, setting 
sys.stdout to some dummy null file. This has the advantage of automatically 
fixing this in other parts of Python where this error might also occur.

This error is particularly hard to find, since in windowed mode there's no 
console to show the exception - it simply closes. In the attached file, where I 
showcase the error, I have to log to a file to see the problem.

I'm not sure how can this be unnoticed for so much time, it can't possibly be 
intended, or maybe it is and it's print() that actually has a problem, lol.

----------
components: Library (Lib)
files: pprint_error_showcase.py
messages: 375358
nosy: luizeldorado
priority: normal
severity: normal
status: open
title: pprint() gives exception when ran from pythonw
type: behavior
versions: Python 3.8
Added file: https://bugs.python.org/file49388/pprint_error_showcase.py

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

Reply via email to