Browse files

Added changelog entry for #92 and changed LoopContext.End to _last_it…

…eration
  • Loading branch information...
1 parent ea89feb commit 99b22854ea6322dbd228e6a7ebfe3285cd68a138 @mitsuhiko mitsuhiko committed Jan 24, 2012
Showing with 8 additions and 5 deletions.
  1. +2 −0 CHANGES
  2. +6 −5 jinja2/runtime.py
View
2 CHANGES
@@ -13,6 +13,8 @@ Version 2.7
- Added `urlencode` filter that automatically quotes values for
URL safe usage with utf-8 as only supported encoding. If applications
want to change this encoding they can override the filter.
+- Accessing `last` on the loop context no longer causes the iterator
+ to be consumed into a list.
Version 2.6
-----------
View
11 jinja2/runtime.py
@@ -30,6 +30,8 @@
#: the identity function. Useful for certain things in the environment
identity = lambda x: x
+_last_iteration = object()
+
def markup_join(seq):
"""Concatenation that escapes if necessary and converts to unicode."""
@@ -266,7 +268,6 @@ def __call__(self):
class LoopContext(object):
"""A loop context for dynamic iteration."""
- End = object()
def __init__(self, iterable, recurse=None):
self._iterator = iter(iterable)
@@ -290,7 +291,7 @@ def cycle(self, *args):
return args[self.index0 % len(args)]
first = property(lambda x: x.index0 == 0)
- last = property(lambda x: x._after is LoopContext.End)
+ last = property(lambda x: x._after is _last_iteration)
index = property(lambda x: x.index0 + 1)
revindex = property(lambda x: x.length - x.index0)
revindex0 = property(lambda x: x.length - x.index)
@@ -305,7 +306,7 @@ def _safe_next(self):
try:
return next(self._iterator)
except StopIteration:
- return self.End
+ return _last_iteration
@internalcode
def loop(self, iterable):
@@ -352,8 +353,8 @@ def __iter__(self):
def next(self):
ctx = self.context
ctx.index0 += 1
- if ctx._after is LoopContext.End:
- raise StopIteration
+ if ctx._after is _last_iteration:
+ raise StopIteration()
next_elem = ctx._after
ctx._after = ctx._safe_next()
return next_elem, ctx

0 comments on commit 99b2285

Please sign in to comment.