From 2403f581b3cd0e7528f4b60c712f460fed91e2c2 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sat, 20 Mar 2010 20:27:57 +0000 Subject: [PATCH] Fixed #12554: Silence exceptions that have specified silent_variable_failure=True. Thanks Thomas Steinacher, copelco, mlavin. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12823 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/__init__.py | 10 +++++----- tests/regressiontests/templates/tests.py | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/django/template/__init__.py b/django/template/__init__.py index 5d238d427c..eee0808b5d 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -745,11 +745,11 @@ class Variable(object): TypeError, # unsubscriptable object ): raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current)) # missing attribute - except Exception, e: - if getattr(e, 'silent_variable_failure', False): - current = settings.TEMPLATE_STRING_IF_INVALID - else: - raise + except Exception, e: + if getattr(e, 'silent_variable_failure', False): + current = settings.TEMPLATE_STRING_IF_INVALID + else: + raise return current diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index c3ffb6f81d..303c365cc8 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -97,6 +97,10 @@ class OtherClass: def method(self): return "OtherClass.method" +class SilentGetItemClass(object): + def __getitem__(self, key): + raise SomeException + class UTF8Class: "Class whose __str__ returns non-ASCII data" def __str__(self): @@ -465,6 +469,11 @@ class Templates(unittest.TestCase): 'basic-syntax26': (r'{{ "\"fred\"" }}', {}, "\"fred\""), 'basic-syntax27': (r'{{ _("\"fred\"") }}', {}, "\"fred\""), + # regression test for ticket #12554 + # make sure a silent_variable_failure Exception is supressed + # on dictionary lookup + 'basic-syntax28': ("{{ a.b }}", {'a': SilentGetItemClass()}, ('', 'INVALID')), + # List-index syntax allows a template to access a certain item of a subscriptable object. 'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),