diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-11-13 01:41:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 01:41:00 (GMT) |
commit | 2c07c493d2eb45101312e3eb3a77f94d0c9cad1f (patch) | |
tree | 5cd5016fbf1a2b3833dbeb83ca7c1055c8671c5b /Lib/warnings.py | |
parent | 1584a0081500d35dc93ff88e5836df35faf3e3e2 (diff) | |
download | cpython-2c07c493d2eb45101312e3eb3a77f94d0c9cad1f.zip cpython-2c07c493d2eb45101312e3eb3a77f94d0c9cad1f.tar.gz cpython-2c07c493d2eb45101312e3eb3a77f94d0c9cad1f.tar.bz2 |
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.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r-- | Lib/warnings.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index 6830b60..cf88131 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -33,9 +33,8 @@ def _showwarnmsg_impl(msg): 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: @@ -55,11 +54,20 @@ def _formatwarnmsg_impl(msg): 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' @@ -77,6 +85,9 @@ def _formatwarnmsg_impl(msg): 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 |