From 7c4289d0b974c9b33bdc569308d8c99d7cac45d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryan=20O=E2=80=99Hara?= Date: Thu, 26 Jan 2017 12:10:15 -0800 Subject: [PATCH] Fixed #27783 -- Switched VariableDoesNotExist.__str__() to repr() context. Using __str__() and then repr'ing the result looks strange and can lead to recursive rendering of forms. --- django/template/base.py | 2 +- tests/template_tests/test_base.py | 8 ++++++++ tests/template_tests/test_logging.py | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 tests/template_tests/test_base.py diff --git a/django/template/base.py b/django/template/base.py index de1a21a2ac..51e78635de 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -119,7 +119,7 @@ class VariableDoesNotExist(Exception): self.params = params def __str__(self): - return self.msg % tuple(force_text(p, errors='replace') for p in self.params) + return self.msg % self.params class Origin: diff --git a/tests/template_tests/test_base.py b/tests/template_tests/test_base.py new file mode 100644 index 0000000000..5320af5e9a --- /dev/null +++ b/tests/template_tests/test_base.py @@ -0,0 +1,8 @@ +from django.template.base import VariableDoesNotExist +from django.test import SimpleTestCase + + +class VariableDoesNotExistTests(SimpleTestCase): + def test_str(self): + exc = VariableDoesNotExist(msg='Failed lookup in %r', params=({'foo': 'bar'},)) + self.assertEqual(str(exc), "Failed lookup in {'foo': 'bar'}") diff --git a/tests/template_tests/test_logging.py b/tests/template_tests/test_logging.py index 802015cf97..89db04fafd 100644 --- a/tests/template_tests/test_logging.py +++ b/tests/template_tests/test_logging.py @@ -75,7 +75,7 @@ class VariableResolveLoggingTests(BaseTemplateLoggingTestCase): raised_exception = self.test_handler.log_record.exc_info[1] self.assertEqual( str(raised_exception), - 'Failed lookup for key [author] in %r' % ("{%r: %r}" % ('section', 'News')) + "Failed lookup for key [author] in {'section': 'News'}" ) def test_no_log_when_variable_exists(self):