From 38359d8adb6291029595bc18b9121f10bff2d8da Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 26 Jul 2018 13:43:16 +0200 Subject: [PATCH] stacktrace: include number of hidden frames --- debug_toolbar/panels/cache.py | 2 +- debug_toolbar/panels/sql/tracking.py | 2 +- debug_toolbar/static/debug_toolbar/css/toolbar.css | 3 +++ debug_toolbar/utils.py | 13 ++++++++----- tests/panels/test_sql.py | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/debug_toolbar/panels/cache.py b/debug_toolbar/panels/cache.py index 366af3fb6..262a987a2 100644 --- a/debug_toolbar/panels/cache.py +++ b/debug_toolbar/panels/cache.py @@ -32,7 +32,7 @@ def wrapped(self, *args, **kwargs): if dt_settings.get_config()['ENABLE_STACKTRACES']: stacktrace = tidy_stacktrace(reversed(get_stack())) else: - stacktrace = [] + stacktrace = ([], 0) template_info = get_template_info() cache_called.send(sender=self.__class__, time_taken=t, diff --git a/debug_toolbar/panels/sql/tracking.py b/debug_toolbar/panels/sql/tracking.py index 4f8466d46..a71a918a2 100644 --- a/debug_toolbar/panels/sql/tracking.py +++ b/debug_toolbar/panels/sql/tracking.py @@ -113,7 +113,7 @@ def _record(self, method, sql, params): if dt_settings.get_config()['ENABLE_STACKTRACES']: stacktrace = tidy_stacktrace(reversed(get_stack())) else: - stacktrace = [] + stacktrace = ([], 0) _params = '' try: _params = json.dumps([self._decode(p) for p in params]) diff --git a/debug_toolbar/static/debug_toolbar/css/toolbar.css b/debug_toolbar/static/debug_toolbar/css/toolbar.css index 537f59af0..4e729d4e4 100644 --- a/debug_toolbar/static/debug_toolbar/css/toolbar.css +++ b/debug_toolbar/static/debug_toolbar/css/toolbar.css @@ -626,6 +626,9 @@ #djDebug .djdt-stack span.djdt-code { font-weight: normal; } +#djDebug .djdt-stack span.djdt-hidden_count { + font-weight: normal; +} #djDebug .djdt-width-20 { width: 20%; diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index d311e9ad4..a2aa1fc5a 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -52,24 +52,24 @@ def omit_path(path): def tidy_stacktrace(stack): """ - Clean up stacktrace and remove all entries that: - 1. Are part of Django (except contrib apps) - 2. Are part of socketserver (used by Django's dev server) - 3. Are the last entry (which is part of our stacktracing code) + Clean up stacktrace and remove entries according to HIDE_IN_STACKTRACES. ``stack`` should be a list of frame tuples from ``inspect.stack()`` """ trace = [] + hidden_count = 0 for frame, path, line_no, func_name, text in (f[:5] for f in stack): if omit_path(os.path.realpath(path)): + hidden_count += 1 continue text = (''.join(force_text(t) for t in text)).strip() if text else '' trace.append((path, line_no, func_name, text)) - return trace + return trace, hidden_count def render_stacktrace(trace): stacktrace = [] + trace, hidden_count = trace for frame in trace: params = (escape(v) for v in chain(frame[0].rsplit(os.path.sep, 1), frame[1:])) params_dict = {six.text_type(idx): v for idx, v in enumerate(params)} @@ -83,6 +83,9 @@ def render_stacktrace(trace): except KeyError: # This frame doesn't have the expected format, so skip it and move on to the next one continue + if hidden_count: + stacktrace.append('' + '%d hidden frames.' % hidden_count) return mark_safe('\n'.join(stacktrace)) diff --git a/tests/panels/test_sql.py b/tests/panels/test_sql.py index e4fa16c27..c2196427f 100644 --- a/tests/panels/test_sql.py +++ b/tests/panels/test_sql.py @@ -162,7 +162,7 @@ def test_disable_stacktraces(self): self.assertTrue('stacktrace' in query[1]) # ensure the stacktrace is empty - self.assertEqual([], query[1]['stacktrace']) + self.assertEqual(([], 0), query[1]['stacktrace']) @override_settings(DEBUG=True, TEMPLATES=[{ 'BACKEND': 'django.template.backends.django.DjangoTemplates',