This commit is contained in:
ton
2024-10-07 10:13:40 +07:00
parent aa1631742f
commit 3a7d696db6
9729 changed files with 1832837 additions and 161742 deletions

View File

@@ -36,7 +36,9 @@ def _formatwarnmsg_impl(msg):
category = msg.category.__name__
s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
if msg.line is None:
# "sys" is a made up file name when we are not able to get the frame
# so do not try to get the source line
if msg.line is None and msg.filename != "sys":
try:
import linecache
line = linecache.getline(msg.filename, msg.lineno)
@@ -58,15 +60,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 +88,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