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
- stderr = '\n'.join(stderr.splitlines())
- stderr = re.sub('<.*>', '<...>', stderr)
+ # normalize newlines
+ 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('''
{fname}:5: ResourceWarning: unclosed file <...>
f = None
pass
def _formatwarnmsg_impl(msg):
- s = ("%s:%s: %s: %s\n"
- % (msg.filename, msg.lineno, msg.category.__name__,
- msg.message))
+ category = msg.category.__name__
+ s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
if msg.line is None:
try:
if msg.source is not None:
try:
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:
- # When a warning is logged during Python shutdown, tracemalloc
- # and the import machinery don't work anymore
+ # 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)
+ 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:
s += 'Object allocated at (most recent call last):\n'
if line:
line = line.strip()
s += ' %s\n' % line
+ elif not tracing:
+ s += (f'{category}: Enable tracemalloc to get the object '
+ f'allocation traceback\n')
return s
# Keep a reference to check if the function was replaced
--- /dev/null
+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.