Skip to content

Commit 85cf360

Browse files
[3.12] gh-113781: Silence AttributeError in warning module during Python finalization (GH-113813) (GH-113873)
The tracemalloc module can already be cleared. (cherry picked from commit 0297418) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9a6b99e commit 85cf360

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Lib/warnings.py

+4-3
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
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)