Browse files

[1.5.x] Fixed #19829 -- Fixed index lookups for NumPy arrays in templ…

…ates.

Backport of 7d5e35c
  • Loading branch information...
1 parent bc6746a commit 42e87c17f27f68ea43c78938d4972211b141ede3 @jphalip jphalip committed Feb 15, 2013
Showing with 18 additions and 1 deletion.
  1. +1 −1 django/template/base.py
  2. +17 −0 tests/regressiontests/templates/tests.py
View
2 django/template/base.py
@@ -756,7 +756,7 @@ def _resolve_lookup(self, context):
for bit in self.lookups:
try: # dictionary lookup
current = current[bit]
- except (TypeError, AttributeError, KeyError):
+ except (TypeError, AttributeError, KeyError, ValueError):
try: # attribute lookup
current = getattr(current, bit)
except (TypeError, AttributeError):
View
17 tests/regressiontests/templates/tests.py
@@ -54,6 +54,12 @@
else:
raise
+# NumPy installed?
+try:
+ import numpy
+except ImportError:
+ numpy = False
+
from . import filters
#################################
@@ -1649,6 +1655,17 @@ def get_template_tests(self):
'verbatim-tag05': ('{% verbatim %}{% endverbatim %}{% verbatim %}{% endverbatim %}', {}, ''),
'verbatim-tag06': ("{% verbatim special %}Don't {% endverbatim %} just yet{% endverbatim special %}", {}, "Don't {% endverbatim %} just yet"),
}
+
+ if numpy:
+ tests.update({
+ # Numpy's array-index syntax allows a template to access a certain item of a subscriptable object.
+ 'numpy-array-index01': ("{{ var.1 }}", {"var": numpy.array(["first item", "second item"])}, "second item"),
+
+ # Fail silently when the array index is out of range.
+ 'numpy-array-index02': ("{{ var.5 }}", {"var": numpy.array(["first item", "second item"])}, ("", "INVALID")),
+ })
+
+
return tests
class TemplateTagLoading(unittest.TestCase):

0 comments on commit 42e87c1

Please sign in to comment.