]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Changed lookup logic for make_attrgetter to support integers like the regular syntax
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 08:07:08 +0000 (09:07 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 08:07:08 +0000 (09:07 +0100)
jinja2/filters.py
jinja2/testsuite/filters.py

index 2f6ffd07a4084b6a9e3624ade3515f768d6f51e9..49e82aad398eea14934db2dfd014a4fbb0557aa6 100644 (file)
@@ -54,13 +54,17 @@ def environmentfilter(f):
 def make_attrgetter(environment, attribute):
     """Returns a callable that looks up the given attribute from a
     passed object with the rules of the environment.  Dots are allowed
-    to access attributes of attributes.
+    to access attributes of attributes.  Integer parts in paths are
+    looked up as integers.
     """
-    if not isinstance(attribute, string_types) or '.' not in attribute:
+    if not isinstance(attribute, string_types) \
+       or ('.' not in attribute and not attribute.isdigit()):
         return lambda x: environment.getitem(x, attribute)
     attribute = attribute.split('.')
     def attrgetter(item):
         for part in attribute:
+            if part.isdigit():
+                part = int(part)
             item = environment.getitem(item, part)
         return item
     return attrgetter
index 8a6ff7128011fc9a3691326c77f79e68fb4500fd..5219f767bf239b53f317479effa20d58c280368b 100644 (file)
@@ -256,6 +256,14 @@ class FilterTestCase(JinjaTestCase):
             {'real': {'value': 18}},
         ]) == '42'
 
+    def test_sum_attributes_tuple(self):
+        tmpl = env.from_string('''{{ values.items()|sum('1') }}''')
+        assert tmpl.render(values={
+            'foo': 23,
+            'bar': 1,
+            'baz': 18,
+        }) == '42'
+
     def test_abs(self):
         tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
         assert tmpl.render() == '1|1', tmpl.render()