Skip to content

Commit

Permalink
panels(templates): avoid evaluating LazyObject
Browse files Browse the repository at this point in the history
LazyObject is typically used for something expensive to evaluate, so avoid evaluating it just for showing it in the debug toolbar.
  • Loading branch information
nijel committed Sep 19, 2023
1 parent 199c2b3 commit 000d4e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ def _store_template_info(self, sender, **kwargs):
if pformatted is None:
temp_layer = {}
for key, value in context_layer.items():
# Do not force evaluating LazyObject
if hasattr(value, "_wrapped"):
# SimpleLazyObject has __repr__ which includes actual value
# if it has been already evaluated
temp_layer[key] = repr(value)
# Replace any request elements - they have a large
# Unicode representation and the request data is
# already made available from the Request panel.
if isinstance(value, http.HttpRequest):
elif isinstance(value, http.HttpRequest):
temp_layer[key] = "<<request>>"
# Replace the debugging sql_queries element. The SQL
# data is already made available from the SQL panel.
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Pending
-------

* Removed outdated third-party panels from the list.
* Avoid evaluating ``LazyObject`` in the templates panel.

4.2.0 (2023-08-10)
------------------
Expand Down
9 changes: 9 additions & 0 deletions tests/panels/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth.models import User
from django.template import Context, RequestContext, Template
from django.test import override_settings
from django.utils.functional import SimpleLazyObject

from ..base import BaseTestCase, IntegrationTestCase
from ..forms import TemplateReprForm
Expand Down Expand Up @@ -109,6 +110,14 @@ def test_empty_context(self):
["{'False': False, 'None': None, 'True': True}"],
)

def test_lazyobject(self):
response = self.panel.process_request(self.request)
t = Template("")
c = Context({"lazy": SimpleLazyObject(lambda: "lazy_value")})
t.render(c)
self.assertNotIn("lazy_value", self.panel.content)



@override_settings(
DEBUG=True, DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.templates.TemplatesPanel"]
Expand Down

0 comments on commit 000d4e8

Please sign in to comment.