From 37a4130e58c8b136e66820c9364fc208a9cae373 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Nov 2018 17:35:30 +0100 Subject: [PATCH 1/2] bpo-29564: warnings suggests to enable tracemalloc The warnings module now suggests to enable tracemalloc if the source is specified, tracemalloc module is available, but tracemalloc is not tracing memory allocations. --- Lib/test/test_warnings/__init__.py | 25 +++++++++++++++---- Lib/warnings.py | 23 +++++++++++------ .../2018-11-12-17-40-04.bpo-29564.SFNBT5.rst | 3 +++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-11-12-17-40-04.bpo-29564.SFNBT5.rst diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index a4775d03f55656..2c54e6137ba80b 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -897,12 +897,27 @@ def 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 - 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 diff --git a/Lib/warnings.py b/Lib/warnings.py index 6830b602de9ab2..539329bf5b5b12 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,18 @@ def _formatwarnmsg_impl(msg): if msg.source is not None: try: import tracemalloc - 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 + except ImportError: + # 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 +83,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 diff --git a/Misc/NEWS.d/next/Library/2018-11-12-17-40-04.bpo-29564.SFNBT5.rst b/Misc/NEWS.d/next/Library/2018-11-12-17-40-04.bpo-29564.SFNBT5.rst new file mode 100644 index 00000000000000..7ef3adeb73b9d2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-11-12-17-40-04.bpo-29564.SFNBT5.rst @@ -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. From 8af77653bfbce43bd937e1f754e056a974f51421 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Nov 2018 23:45:16 +0100 Subject: [PATCH 2/2] Catch any exception --- Lib/warnings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/warnings.py b/Lib/warnings.py index 539329bf5b5b12..cf88131f87b4f1 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -54,7 +54,9 @@ def _formatwarnmsg_impl(msg): if msg.source is not None: try: import tracemalloc - except ImportError: + # Logging a warning should not raise a new exception: + # catch Exception, not only ImportError and RecursionError. + except Exception: # don't suggest to enable tracemalloc if it's not available tracing = True tb = None