Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3465: template variable lookups like {{ foobar.13 }} now (corr…

…ectly) fail silently on unsubscriptable objects. Thanks, Gary Wilson.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4639 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 19e0bf1d4463c24f901951a7d8ab4a1d54b1ac9d 1 parent 9c2c03a
Jacob Kaplan-Moss authored February 27, 2007
6  django/template/__init__.py
@@ -667,7 +667,11 @@ def resolve_variable(path, context):
667 667
                 except (TypeError, AttributeError):
668 668
                     try: # list-index lookup
669 669
                         current = current[int(bits[0])]
670  
-                    except (IndexError, ValueError, KeyError):
  670
+                    except (IndexError, # list index out of range
  671
+                            ValueError, # invalid literal for int()
  672
+                            KeyError,   # current is a dict without `int(bits[0])` key
  673
+                            TypeError,  # unsubscriptable object
  674
+                            ):
671 675
                         raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bits[0], current)) # missing attribute
672 676
                 except Exception, e:
673 677
                     if getattr(e, 'silent_variable_failure', False):
25  tests/regressiontests/templates/tests.py
@@ -127,6 +127,29 @@ def test_templates(self):
127 127
             # Fail silently when accessing a non-simple method
128 128
             'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")),
129 129
 
  130
+            # List-index syntax allows a template to access a certain item of a subscriptable object.
  131
+            'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
  132
+
  133
+            # Fail silently when the list index is out of range.
  134
+            'list-index02': ("{{ var.5 }}", {"var": ["first item", "second item"]}, ("", "INVALID")),
  135
+
  136
+            # Fail silently when the variable is not a subscriptable object.
  137
+            'list-index03': ("{{ var.1 }}", {"var": None}, ("", "INVALID")),
  138
+
  139
+            # Fail silently when variable is a dict without the specified key.
  140
+            'list-index04': ("{{ var.1 }}", {"var": {}}, ("", "INVALID")),
  141
+
  142
+            # Dictionary lookup wins out when dict's key is a string.
  143
+            'list-index05': ("{{ var.1 }}", {"var": {'1': "hello"}}, "hello"),
  144
+
  145
+            # But list-index lookup wins out when dict's key is an int, which
  146
+            # behind the scenes is really a dictionary lookup (for a dict)
  147
+            # after converting the key to an int.
  148
+            'list-index06': ("{{ var.1 }}", {"var": {1: "hello"}}, "hello"),
  149
+
  150
+            # Dictionary lookup wins out when there is a string and int version of the key.
  151
+            'list-index07': ("{{ var.1 }}", {"var": {'1': "hello", 1: "world"}}, "hello"),
  152
+            
130 153
             # Basic filter usage
131 154
             'basic-syntax21': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"),
132 155
 
@@ -167,7 +190,7 @@ def test_templates(self):
167 190
             'basic-syntax33': (r'1{{ var.method3 }}2', {"var": SomeClass()}, ("12", "1INVALID2")),
168 191
 
169 192
             # In methods that raise an exception without a "silent_variable_attribute" set to True,
170  
-            # the exception propogates
  193
+            # the exception propagates
171 194
             'basic-syntax34': (r'1{{ var.method4 }}2', {"var": SomeClass()}, SomeOtherException),
172 195
 
173 196
             # Escaped backslash in argument

0 notes on commit 19e0bf1

Please sign in to comment.
Something went wrong with that request. Please try again.