[New-bugs-announce] [issue41546] pprint() gives exception when ran from pythonw

Luiz report at bugs.python.org
Thu Aug 13 20:10:04 EDT 2020

New submission from Luiz <eldorado.luiz at 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 <report at bugs.python.org>

More information about the New-bugs-announce mailing list