Skip to content

Commit 2c07c49

Browse files
authored
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.
1 parent 1584a00 commit 2c07c49

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,27 @@ def func():
897897
func()
898898
"""))
899899

900-
res = assert_python_ok('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
900+
def run(*args):
901+
res = assert_python_ok(*args)
902+
stderr = res.err.decode('ascii', 'replace')
903+
stderr = '\n'.join(stderr.splitlines())
901904

902-
stderr = res.err.decode('ascii', 'replace')
903-
# normalize newlines
904-
stderr = '\n'.join(stderr.splitlines())
905-
stderr = re.sub('<.*>', '<...>', stderr)
905+
# normalize newlines
906+
stderr = re.sub('<.*>', '<...>', stderr)
907+
return stderr
908+
909+
# tracemalloc disabled
910+
stderr = run('-Wd', support.TESTFN)
911+
expected = textwrap.dedent('''
912+
{fname}:5: ResourceWarning: unclosed file <...>
913+
f = None
914+
ResourceWarning: Enable tracemalloc to get the object allocation traceback
915+
''')
916+
expected = expected.format(fname=support.TESTFN).strip()
917+
self.assertEqual(stderr, expected)
918+
919+
# tracemalloc enabled
920+
stderr = run('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
906921
expected = textwrap.dedent('''
907922
{fname}:5: ResourceWarning: unclosed file <...>
908923
f = None

Lib/warnings.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def _showwarnmsg_impl(msg):
3333
pass
3434

3535
def _formatwarnmsg_impl(msg):
36-
s = ("%s:%s: %s: %s\n"
37-
% (msg.filename, msg.lineno, msg.category.__name__,
38-
msg.message))
36+
category = msg.category.__name__
37+
s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
3938

4039
if msg.line is None:
4140
try:
@@ -55,11 +54,20 @@ def _formatwarnmsg_impl(msg):
5554
if msg.source is not None:
5655
try:
5756
import tracemalloc
58-
tb = tracemalloc.get_object_traceback(msg.source)
57+
# Logging a warning should not raise a new exception:
58+
# catch Exception, not only ImportError and RecursionError.
5959
except Exception:
60-
# When a warning is logged during Python shutdown, tracemalloc
61-
# and the import machinery don't work anymore
60+
# don't suggest to enable tracemalloc if it's not available
61+
tracing = True
6262
tb = None
63+
else:
64+
tracing = tracemalloc.is_tracing()
65+
try:
66+
tb = tracemalloc.get_object_traceback(msg.source)
67+
except Exception:
68+
# When a warning is logged during Python shutdown, tracemalloc
69+
# and the import machinery don't work anymore
70+
tb = None
6371

6472
if tb is not None:
6573
s += 'Object allocated at (most recent call last):\n'
@@ -77,6 +85,9 @@ def _formatwarnmsg_impl(msg):
7785
if line:
7886
line = line.strip()
7987
s += ' %s\n' % line
88+
elif not tracing:
89+
s += (f'{category}: Enable tracemalloc to get the object '
90+
f'allocation traceback\n')
8091
return s
8192

8293
# Keep a reference to check if the function was replaced
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The warnings module now suggests to enable tracemalloc if the source is
2+
specified, the tracemalloc module is available, but tracemalloc is not
3+
tracing memory allocations.

0 commit comments

Comments
 (0)