Skip to content

[3.11] gh-113781: Silence AttributeError in warning module during Python finalization (GH-113813) #113874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ def _formatwarnmsg_impl(msg):
# catch Exception, not only ImportError and RecursionError.
except Exception:
# don't suggest to enable tracemalloc if it's not available
tracing = True
suggest_tracemalloc = False
tb = None
else:
tracing = tracemalloc.is_tracing()
try:
suggest_tracemalloc = not tracemalloc.is_tracing()
tb = tracemalloc.get_object_traceback(msg.source)
except Exception:
# When a warning is logged during Python shutdown, tracemalloc
# and the import machinery don't work anymore
suggest_tracemalloc = False
tb = None

if tb is not None:
Expand All @@ -85,7 +86,7 @@ def _formatwarnmsg_impl(msg):
if line:
line = line.strip()
s += ' %s\n' % line
elif not tracing:
elif suggest_tracemalloc:
s += (f'{category}: Enable tracemalloc to get the object '
f'allocation traceback\n')
return s
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Silence unraisable AttributeError when warnings are emitted during Python
finalization.