Skip to content

Commit

Permalink
Added changelog entry for pallets#92 and changed LoopContext.End to _…
Browse files Browse the repository at this point in the history
…last_iteration
  • Loading branch information
mitsuhiko committed Jan 24, 2012
1 parent ea89feb commit 99b2285
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
-----------
Expand Down
11 changes: 6 additions & 5 deletions jinja2/runtime.py
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 99b2285

Please sign in to comment.