mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[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 0297418cac
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
9a6b99ee8b
commit
85cf360d29
2 changed files with 6 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Silence unraisable AttributeError when warnings are emitted during Python
|
||||
finalization.
|
Loading…
Add table
Add a link
Reference in a new issue