Repository navigation
Logger error under Pythonw results in a crash and recursion error #107792
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Running under pythonw, log messages to stderr cause a crash with recursion.
The error messages:
Traceback (most recent call last):
File "logging\__init__.py", line 1113, in emit
AttributeError: 'NoneType' object has no attribute 'write'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "logging\__init__.py", line 1113, in emit
AttributeError: 'NoneType' object has no attribute 'write'
During handling of the above exception, another exception occurred:
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "kivy\logger.py", line 310, in emit
File "kivy\logger.py", line 284, in _configure
File "logging\__init__.py", line 1489, in info
File "logging\__init__.py", line 1634, in _log
File "logging\__init__.py", line 1644, in handle
File "logging\__init__.py", line 1706, in callHandlers
File "logging\__init__.py", line 978, in handle
File "logging\__init__.py", line 1118, in emit
File "logging\__init__.py", line 1031, in handleError
File "kivy\logger.py", line 390, in write
File "logging\__init__.py", line 1501, in warning
File "logging\__init__.py", line 1634, in _log
File "logging\__init__.py", line 1644, in handle
File "logging\__init__.py", line 1706, in callHandlers
File "logging\__init__.py", line 978, in handle
File "logging\__init__.py", line 1118, in emit
File "logging\__init__.py", line 1031, in handleError
File "kivy\logger.py", line 390, in write
File "logging\__init__.py", line 1501, in warning
File "logging\__init__.py", line 1634, in _log
File "logging\__init__.py", line 1644, in handle
File "logging\__init__.py", line 1706, in callHandlers
File "logging\__init__.py", line 978, in handle
File "logging\__init__.py", line 1118, in emit
File "logging\__init__.py", line 1031, in handleError
...
RecursionError: maximum recursion depth exceeded
...
Checklist
- I am confident this is a bug in CPython, not a bug in a third-party project
- I have searched the CPython issue tracker, and am confident this bug has not been reported before
A clear and concise description of the bug
In pythonw, stderr is None. This triggers a problem in the exception handling of the emit code in the logger.
cpython/Lib/logging/__init__.py
Line 1098 in 0aa3b9d
| def emit(self, record): |
# The emit code in the standard library:
def emit(self, record):
"""
Emit a record.
If a formatter is specified, it is used to format the record.
The record is then written to the stream with a trailing newline. If
exception information is present, it is formatted using
traceback.print_exception and appended to the stream. If the stream
has an 'encoding' attribute, it is used to determine how to do the
output to the stream.
"""
try:
msg = self.format(record)
stream = self.stream
# issue 35046: merged two stream.writes into one.
stream.write(msg + self.terminator)
self.flush()
except RecursionError: # See issue 36272
raise
except Exception:
self.handleError(record)I believe the more correct solution is to handle the Attribute exception caused when sys.stderr is None.
def emit(self, record):
"""
Emit a record.
If a formatter is specified, it is used to format the record.
The record is then written to the stream with a trailing newline. If
exception information is present, it is formatted using
traceback.print_exception and appended to the stream. If the stream
has an 'encoding' attribute, it is used to determine how to do the
output to the stream.
"""
try:
msg = self.format(record)
stream = self.stream
# issue 35046: merged two stream.writes into one.
stream.write(msg + self.terminator)
self.flush()
except AttributeError: # handle the Attribute error, return, ignoring the request.
return # this would cover the case for pythonw or whenever sys.stderr is None
except RecursionError: # See issue 36272
raise
except Exception:
self.handleError(record)I would submit a PR - but I'm not confident I understand the logger with enough depth to not open up other issues. There appears to be a number of issues that are related that this fix would address.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
sunmy2019 commentedon Aug 10, 2023
This is too wildcarded. How about something like
ElliotGarbus commentedon Aug 10, 2023
I managed to fix my issue by not adding the console handler to the logger when sys.stderr is None.
@sunmy2019 Your solution looks good to me.
I can close this issue or leave it open it is worth taking a closer look.
sunmy2019 commentedon Aug 10, 2023
I see. I think it can be left open. We can add checks here.
cpython/Lib/logging/__init__.py
Lines 1076 to 1085 in 0aa3b9d
At least we can prevent
emititself from causing the recursion.ElliotGarbus commentedon Aug 10, 2023
Good idea.
vsajip commentedon Jan 30, 2024
I think the solution used by @ElliotGarbus - to not add the console handler when running in
pythonw- makes the most sense for the use case. I'm not against putting a check for aNonevalue just beforeself.stream = stream- but there would also need to be a check insetStream(), and perhaps the check should be forhasattr(stream, 'write')rather than a simple check forNone. Since this is a change in behavior (e.g. an exception will be raised by the check where one wasn't raised before) it seems like such a change would have to be not backported.serhiy-storchaka commentedon Jul 22, 2026
Nothing to do on our side. The error is produced by third-party handler. Closed as not planned.