mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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.
This commit is contained in:
parent
1584a00815
commit
2c07c493d2
3 changed files with 40 additions and 11 deletions
|
@ -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 = '\n'.join(stderr.splitlines())
|
||||||
|
|
||||||
stderr = res.err.decode('ascii', 'replace')
|
# normalize newlines
|
||||||
# normalize newlines
|
stderr = re.sub('<.*>', '<...>', stderr)
|
||||||
stderr = '\n'.join(stderr.splitlines())
|
return stderr
|
||||||
stderr = re.sub('<.*>', '<...>', 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):
|
||||||
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,11 +54,20 @@ def _formatwarnmsg_impl(msg):
|
||||||
if msg.source is not None:
|
if msg.source is not None:
|
||||||
try:
|
try:
|
||||||
import tracemalloc
|
import tracemalloc
|
||||||
tb = tracemalloc.get_object_traceback(msg.source)
|
# Logging a warning should not raise a new exception:
|
||||||
|
# catch Exception, not only ImportError and RecursionError.
|
||||||
except Exception:
|
except Exception:
|
||||||
# When a warning is logged during Python shutdown, tracemalloc
|
# don't suggest to enable tracemalloc if it's not available
|
||||||
# and the import machinery don't work anymore
|
tracing = True
|
||||||
tb = None
|
tb = None
|
||||||
|
else:
|
||||||
|
tracing = tracemalloc.is_tracing()
|
||||||
|
try:
|
||||||
|
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
|
||||||
|
tb = None
|
||||||
|
|
||||||
if tb is not None:
|
if tb is not None:
|
||||||
s += 'Object allocated at (most recent call last):\n'
|
s += 'Object allocated at (most recent call last):\n'
|
||||||
|
@ -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
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
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.
|
Loading…
Add table
Add a link
Reference in a new issue