Skip to content
Myr

Changed lookup logic for make_attrgetter to support integers like the regular syntax

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 03bea56 · Browse files · 2 files+14 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+14 added−2 removed

  • other:+14 added−2 removed

2 files+14 added−2 removed

modified:jinja2/testsuite/filters.py+8 added
254 unmodified lines
255 {'real': {'value': 1}},256 {'real': {'value': 18}},257 ]) == '42'258259 def test_sum_attributes_tuple(self):260 tmpl = env.from_string('''{{ values.items()|sum('1') }}''')261 assert tmpl.render(values={262 'foo': 23,263 'bar': 1,264 'baz': 18,265 }) == '42'266267 def test_abs(self):268 tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')269 assert tmpl.render() == '1|1', tmpl.render()270
239 unmodified lines
modified:jinja2/filters.py+6 added-2 removed
52 unmodified lines
5354def make_attrgetter(environment, attribute):55 """Returns a callable that looks up the given attribute from a56 passed object with the rules of the environment. Dots are allowed57 to access attributes of attributes.57 to access attributes of attributes. Integer parts in paths are58 looked up as integers.59 """59 if not isinstance(attribute, string_types) or '.' not in attribute:60 if not isinstance(attribute, string_types) \61 or ('.' not in attribute and not attribute.isdigit()):62 return lambda x: environment.getitem(x, attribute)63 attribute = attribute.split('.')64 def attrgetter(item):65 for part in attribute:66 if part.isdigit():67 part = int(part)68 item = environment.getitem(item, part)69 return item70 return attrgetter71
916 unmodified lines