[BUG] Rich loghandler raising exceptions when using pythonw
See original GitHub issueAfter switching to using the Rich log handler in an application it started raising 'NoneType' object has no attribute 'write'
exceptions, however, this would only happen when the debugger was not connected and running under pythonw (the special windows version that does not create a console window, used for GUI applications).
On a hunch I checked the implementation of Pythons builtin log handler (that we had been using previously) and found the following line in the Handler.handleError
:
if raiseExceptions and sys.stderr: # see issue 13807
This confirmed my hunch, when using pythonw both sys.stdout
and sys.stderr
are None
. The Rich logger doesn’t appear to have an exception handler catching all exceptions being generated when rending log output and deferring to the handleError
method.
To Reproduce
Running this with Python will produce the expected log message, running with pythonw will generate the error.txt file with the missing attribute exception.
import logging
from rich.logging import RichHandler
logging.basicConfig(handlers=[RichHandler()], level=logging.INFO)
try:
logging.info("My amazing info message")
except Exception as ex:
with open("error.txt", "w") as f_out:
print(str(ex), file=f_out)
Platform This is a Windows-specific error
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Fix is in 10.13.0
The
emit
method oflogging.StreamHandler
(ref https://github.com/python/cpython/blob/main/Lib/logging/__init__.py#L1103 ) has a try/except block that catchesException
and passes the record toHandler.handleError
. The docstring forhandleError
statesThis was the pattern I followed in the PR associated with this issue.