Skip to content

Commit 0091f34

Browse files
bpo-29564: warnings suggests to enable tracemalloc (GH-10486) (GH-10509)
The warnings module now suggests to enable tracemalloc if the source is specified, tracemalloc module is available, but tracemalloc is not tracing memory allocations. (cherry picked from commit 2c07c49) Co-authored-by: Victor Stinner <[email protected]>
1 parent 1751423 commit 0091f34

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
@@ -960,12 +960,27 @@ def func():
960960
func()
961961
"""))
962962

963-
res = assert_python_ok('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
963+
def run(*args):
964+
res = assert_python_ok(*args)
965+
stderr = res.err.decode('ascii', 'replace')
966+
stderr = '\n'.join(stderr.splitlines())
964967

965-
stderr = res.err.decode('ascii', 'replace')
966-
# normalize newlines
967-
stderr = '\n'.join(stderr.splitlines())
968-
stderr = re.sub('<.*>', '<...>', stderr)
968+
# normalize newlines
969+
stderr = re.sub('<.*>', '<...>', stderr)
970+
return stderr
971+
972+
# tracemalloc disabled
973+
stderr = run('-Wd', support.TESTFN)
974+
expected = textwrap.dedent('''
975+
{fname}:5: ResourceWarning: unclosed file <...>
976+
f = None
977+
ResourceWarning: Enable tracemalloc to get the object allocation traceback
978+
''')
979+
expected = expected.format(fname=support.TESTFN).strip()
980+
self.assertEqual(stderr, expected)
981+
982+
# tracemalloc enabled
983+
stderr = run('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
969984
expected = textwrap.dedent('''
970985
{fname}:5: ResourceWarning: unclosed file <...>
971986
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)