Skip to content

Commit 0297418

Browse files
gh-113781: Silence AttributeError in warning module during Python finalization (GH-113813)
The tracemalloc module can already be cleared.
1 parent a5db6a3 commit 0297418

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Lib/warnings.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ def _formatwarnmsg_impl(msg):
5858
# catch Exception, not only ImportError and RecursionError.
5959
except Exception:
6060
# don't suggest to enable tracemalloc if it's not available
61-
tracing = True
61+
suggest_tracemalloc = False
6262
tb = None
6363
else:
64-
tracing = tracemalloc.is_tracing()
6564
try:
65+
suggest_tracemalloc = not tracemalloc.is_tracing()
6666
tb = tracemalloc.get_object_traceback(msg.source)
6767
except Exception:
6868
# When a warning is logged during Python shutdown, tracemalloc
6969
# and the import machinery don't work anymore
70+
suggest_tracemalloc = False
7071
tb = None
7172

7273
if tb is not None:
@@ -85,7 +86,7 @@ def _formatwarnmsg_impl(msg):
8586
if line:
8687
line = line.strip()
8788
s += ' %s\n' % line
88-
elif not tracing:
89+
elif suggest_tracemalloc:
8990
s += (f'{category}: Enable tracemalloc to get the object '
9091
f'allocation traceback\n')
9192
return s
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Silence unraisable AttributeError when warnings are emitted during Python
2+
finalization.

0 commit comments

Comments
 (0)