Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #92 from jasonkotenko/master

Fix loop.last memory issue
  • Loading branch information...
commit ea89feb0d9e5abec9791b57e69fdd7035f80369a 2 parents 5145401 + 776567c
@mitsuhiko authored
Showing with 14 additions and 2 deletions.
  1. +14 −2 jinja2/runtime.py
View
16 jinja2/runtime.py
@@ -266,10 +266,12 @@ def __call__(self):
class LoopContext(object):
"""A loop context for dynamic iteration."""
+ End = object()
def __init__(self, iterable, recurse=None):
self._iterator = iter(iterable)
self._recurse = recurse
+ self._after = self._safe_next()
self.index0 = -1
# try to get the length of the iterable early. This must be done
@@ -288,7 +290,7 @@ def cycle(self, *args):
return args[self.index0 % len(args)]
first = property(lambda x: x.index0 == 0)
- last = property(lambda x: x.index0 + 1 == x.length)
+ last = property(lambda x: x._after is LoopContext.End)
index = property(lambda x: x.index0 + 1)
revindex = property(lambda x: x.length - x.index0)
revindex0 = property(lambda x: x.length - x.index)
@@ -299,6 +301,12 @@ def __len__(self):
def __iter__(self):
return LoopContextIterator(self)
+ def _safe_next(self):
+ try:
+ return next(self._iterator)
+ except StopIteration:
+ return self.End
+
@internalcode
def loop(self, iterable):
if self._recurse is None:
@@ -344,7 +352,11 @@ def __iter__(self):
def next(self):
ctx = self.context
ctx.index0 += 1
- return next(ctx._iterator), ctx
+ if ctx._after is LoopContext.End:
+ raise StopIteration
+ next_elem = ctx._after
+ ctx._after = ctx._safe_next()
+ return next_elem, ctx
class Macro(object):
Please sign in to comment.
Something went wrong with that request. Please try again.