]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-113781: Silence AttributeError in warning module during Python finalization...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 9 Jan 2024 20:41:02 +0000 (21:41 +0100)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 20:41:02 +0000 (22:41 +0200)
The tracemalloc module can already be cleared.
(cherry picked from commit 0297418cacf998e778bc0517aa11eaac827b8c0f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/warnings.py
Misc/NEWS.d/next/Library/2024-01-08-14-57-09.gh-issue-113781.IoTnwi.rst [new file with mode: 0644]

index 7d8c4400127f7fee300a6d14641fdb808d26677a..7c8a0943b8179e0f69e0beb8d8fab6656b9d2e35 100644 (file)
@@ -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 (file)
index 0000000..141230b
--- /dev/null
@@ -0,0 +1,2 @@
+Silence unraisable AttributeError when warnings are emitted during Python
+finalization.