Merge pull request #728 from jphalip/ticket-19829

Fixed #19829 -- Fixed index lookups for NumPy arrays in templates.
This commit is contained in:
Julien Phalip 2013-02-15 00:12:45 -08:00
commit 400fba9ca8
2 changed files with 18 additions and 1 deletions

View File

@ -763,7 +763,7 @@ class Variable(object):
for bit in self.lookups: for bit in self.lookups:
try: # dictionary lookup try: # dictionary lookup
current = current[bit] current = current[bit]
except (TypeError, AttributeError, KeyError): except (TypeError, AttributeError, KeyError, ValueError):
try: # attribute lookup try: # attribute lookup
current = getattr(current, bit) current = getattr(current, bit)
except (TypeError, AttributeError): except (TypeError, AttributeError):

View File

@ -54,6 +54,12 @@ except ImportError as e:
else: else:
raise raise
# NumPy installed?
try:
import numpy
except ImportError:
numpy = False
from . import filters from . import filters
################################# #################################
@ -1649,6 +1655,17 @@ class Templates(TestCase):
'verbatim-tag05': ('{% verbatim %}{% endverbatim %}{% verbatim %}{% endverbatim %}', {}, ''), 'verbatim-tag05': ('{% verbatim %}{% endverbatim %}{% verbatim %}{% endverbatim %}', {}, ''),
'verbatim-tag06': ("{% verbatim special %}Don't {% endverbatim %} just yet{% endverbatim special %}", {}, "Don't {% endverbatim %} just yet"), '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 return tests
class TemplateTagLoading(unittest.TestCase): class TemplateTagLoading(unittest.TestCase):