Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize render_stacktrace() #1571

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

47 changes: 26 additions & 21 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import re
import sys
from importlib import import_module
from itertools import chain
from pprint import pformat

import django
from django.core.exceptions import ImproperlyConfigured
from django.template import Node
from django.template.loader import render_to_string
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from debug_toolbar import settings as dt_settings
Expand Down Expand Up @@ -69,26 +69,31 @@ def tidy_stacktrace(stack):


def render_stacktrace(trace):
stacktrace = []
for frame in trace:
params = (v for v in chain(frame[0].rsplit(os.path.sep, 1), frame[1:]))
params_dict = {str(idx): v for idx, v in enumerate(params)}
try:
stacktrace.append(params_dict)
except KeyError:
# This frame doesn't have the expected format, so skip it and move
# on to the next one
continue
Comment on lines -78 to -81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this didn't make sense - list.append cannot raise a KeyError


return mark_safe(
render_to_string(
"debug_toolbar/panels/sql_stacktrace.html",
{
"stacktrace": stacktrace,
"show_locals": dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"],
},
show_locals = dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"]
html = ""
for abspath, lineno, func, code, locals_ in trace:
directory, filename = abspath.rsplit(os.path.sep, 1)
html += format_html(
(
'<span class="djdt-path">{}/</span>'
+ '<span class="djdt-file">{}</span> in'
+ ' <span class="djdt-func">{}</span>'
+ '(<span class="djdt-lineno">{}</span>)\n'
+ ' <span class="djdt-code">{}</span>\n'
),
directory,
filename,
func,
lineno,
code,
)
)
if show_locals:
html += format_html(
' <pre class="djdt-locals">{}</pre>\n',
pformat(locals_),
)
html += "\n"
return mark_safe(html)


def get_template_info():
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Next version
* Reset settings when overridden in tests. Packages or projects using
django-debug-toolbar can now use Django’s test settings tools, like
``@override_settings``, to reconfigure the toolbar during tests.
* Optimize rendering of SQL panel, saving about 15% of its run time.

3.2.4 (2021-12-15)
------------------
Expand Down