diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-01-09 19:44:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-09 19:44:05 (GMT) |
commit | 0297418cacf998e778bc0517aa11eaac827b8c0f (patch) | |
tree | 898fa875df9f7db23a246ebf858ca755e0f37197 | |
parent | a5db6a3351b440a875a5af84a8b2447981356e34 (diff) | |
download | cpython-0297418cacf998e778bc0517aa11eaac827b8c0f.zip cpython-0297418cacf998e778bc0517aa11eaac827b8c0f.tar.gz cpython-0297418cacf998e778bc0517aa11eaac827b8c0f.tar.bz2 |
gh-113781: Silence AttributeError in warning module during Python finalization (GH-113813)
The tracemalloc module can already be cleared.
-rw-r--r-- | Lib/warnings.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-01-08-14-57-09.gh-issue-113781.IoTnwi.rst | 2 |
2 files changed, 6 insertions, 3 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index b8ff078..4ad6ad0 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -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: @@ -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 diff --git a/Misc/NEWS.d/next/Library/2024-01-08-14-57-09.gh-issue-113781.IoTnwi.rst b/Misc/NEWS.d/next/Library/2024-01-08-14-57-09.gh-issue-113781.IoTnwi.rst new file mode 100644 index 0000000..141230b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-01-08-14-57-09.gh-issue-113781.IoTnwi.rst @@ -0,0 +1,2 @@ +Silence unraisable AttributeError when warnings are emitted during Python +finalization. |