Fix loop context length calculation for iterators. Fixes #244
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This resulted in wrong values for revindex and revindex0 while looping over an iterator.
authored by Christoph Reiter · committed by Christoph Reiter · parent 27ffd01 · Browse files · 2 files·+21 added−18 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+21 added−18 removed
- other:+21 added−18 removed
Provenance
- Commit
4eb30f1- Tree
3e189d0- Author
Christoph Reiter <reiter.christoph@gmail.com>- Committer
Christoph Reiter <reiter.christoph@gmail.com>- Parent
27ffd01
2 files changed+21 added−18 removed
modified:jinja2/testsuite/core_tags.py+18 added-16 removed
31 unmodified lines32 tmpl = env.from_string('<{% for item in seq %}{% else %}{% endfor %}>')33 assert tmpl.render() == '<>'34 35 def test_context_vars(self):36+ slist = [42, 24]37+ for seq in [slist, iter(slist), reversed(slist), (_ for _ in slist)]:36- tmpl = env.from_string('''{% for item in seq -%}37- {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{38- loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{39- loop.length }}###{% endfor %}''')40- one, two, _ = tmpl.render(seq=[0, 1]).split('###')41- (one_index, one_index0, one_revindex, one_revindex0, one_first,42- one_last, one_length) = one.split('|')43- (two_index, two_index0, two_revindex, two_revindex0, two_first,44- two_last, two_length) = two.split('|')38+ tmpl = env.from_string('''{% for item in seq -%}39+ {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{40+ loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{41+ loop.length }}###{% endfor %}''')42+ one, two, _ = tmpl.render(seq=seq).split('###')43+ (one_index, one_index0, one_revindex, one_revindex0, one_first,44+ one_last, one_length) = one.split('|')45+ (two_index, two_index0, two_revindex, two_revindex0, two_first,46+ two_last, two_length) = two.split('|')47 46- assert int(one_index) == 1 and int(two_index) == 247- assert int(one_index0) == 0 and int(two_index0) == 148- assert int(one_revindex) == 2 and int(two_revindex) == 149- assert int(one_revindex0) == 1 and int(two_revindex0) == 050- assert one_first == 'True' and two_first == 'False'51- assert one_last == 'False' and two_last == 'True'52- assert one_length == two_length == '2'48+ assert int(one_index) == 1 and int(two_index) == 249+ assert int(one_index0) == 0 and int(two_index0) == 150+ assert int(one_revindex) == 2 and int(two_revindex) == 151+ assert int(one_revindex0) == 1 and int(two_revindex0) == 052+ assert one_first == 'True' and two_first == 'False'53+ assert one_last == 'False' and two_last == 'True'54+ assert one_length == two_length == '2'55 56 def test_cycling(self):57 tmpl = env.from_string('''{% for item in seq %}{{58 loop.cycle('<1>', '<2>') }}{% endfor %}{%249 unmodified linesmodified:jinja2/runtime.py+3 added-2 removed
337 unmodified lines338 if self._length is None:339 # if was not possible to get the length of the iterator when340 # the loop context was created (ie: iterating over a generator)341 # we have to convert the iterable into a sequence and use the342- # length of that.342+ # length of that + the number of iterations so far.343 iterable = tuple(self._iterator)344 self._iterator = iter(iterable)345+ iterations_done = self.index0 + 2345- self._length = len(iterable) + self.index0 + 1346+ self._length = len(iterable) + iterations_done347 return self._length348 349 def __repr__(self):350 return '<%s %r/%r>' % (232 unmodified lines