Commit 2c07c493 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-29564: warnings suggests to enable tracemalloc (GH-10486)

The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
parent 1584a008
...@@ -897,12 +897,27 @@ class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): ...@@ -897,12 +897,27 @@ class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
func() func()
""")) """))
res = assert_python_ok('-Wd', '-X', 'tracemalloc=2', support.TESTFN) def run(*args):
res = assert_python_ok(*args)
stderr = res.err.decode('ascii', 'replace') stderr = res.err.decode('ascii', 'replace')
# normalize newlines
stderr = '\n'.join(stderr.splitlines()) stderr = '\n'.join(stderr.splitlines())
# normalize newlines
stderr = re.sub('<.*>', '<...>', stderr) stderr = re.sub('<.*>', '<...>', stderr)
return stderr
# tracemalloc disabled
stderr = run('-Wd', support.TESTFN)
expected = textwrap.dedent('''
{fname}:5: ResourceWarning: unclosed file <...>
f = None
ResourceWarning: Enable tracemalloc to get the object allocation traceback
''')
expected = expected.format(fname=support.TESTFN).strip()
self.assertEqual(stderr, expected)
# tracemalloc enabled
stderr = run('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
expected = textwrap.dedent(''' expected = textwrap.dedent('''
{fname}:5: ResourceWarning: unclosed file <...> {fname}:5: ResourceWarning: unclosed file <...>
f = None f = None
......
...@@ -33,9 +33,8 @@ def _showwarnmsg_impl(msg): ...@@ -33,9 +33,8 @@ def _showwarnmsg_impl(msg):
pass pass
def _formatwarnmsg_impl(msg): def _formatwarnmsg_impl(msg):
s = ("%s:%s: %s: %s\n" category = msg.category.__name__
% (msg.filename, msg.lineno, msg.category.__name__, s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
msg.message))
if msg.line is None: if msg.line is None:
try: try:
...@@ -55,6 +54,15 @@ def _formatwarnmsg_impl(msg): ...@@ -55,6 +54,15 @@ def _formatwarnmsg_impl(msg):
if msg.source is not None: if msg.source is not None:
try: try:
import tracemalloc import tracemalloc
# Logging a warning should not raise a new exception:
# catch Exception, not only ImportError and RecursionError.
except Exception:
# don't suggest to enable tracemalloc if it's not available
tracing = True
tb = None
else:
tracing = tracemalloc.is_tracing()
try:
tb = tracemalloc.get_object_traceback(msg.source) tb = tracemalloc.get_object_traceback(msg.source)
except Exception: except Exception:
# When a warning is logged during Python shutdown, tracemalloc # When a warning is logged during Python shutdown, tracemalloc
...@@ -77,6 +85,9 @@ def _formatwarnmsg_impl(msg): ...@@ -77,6 +85,9 @@ def _formatwarnmsg_impl(msg):
if line: if line:
line = line.strip() line = line.strip()
s += ' %s\n' % line s += ' %s\n' % line
elif not tracing:
s += (f'{category}: Enable tracemalloc to get the object '
f'allocation traceback\n')
return s return s
# Keep a reference to check if the function was replaced # Keep a reference to check if the function was replaced
......
The warnings module now suggests to enable tracemalloc if the source is
specified, the tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment