From 99b22854ea6322dbd228e6a7ebfe3285cd68a138 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 25 Jan 2012 00:42:54 +0100 Subject: [PATCH] Added changelog entry for #92 and changed LoopContext.End to _last_iteration --- CHANGES | 2 ++ jinja2/runtime.py | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index a48208a..6539c5f 100644 --- a/CHANGES +++ b/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 ----------- diff --git a/jinja2/runtime.py b/jinja2/runtime.py index c70cb4e..5c39984 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -30,6 +30,8 @@ to_string = unicode #: 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 @@ class BlockReference(object): 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 @@ class LoopContext(object): 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 @@ class LoopContext(object): try: return next(self._iterator) except StopIteration: - return self.End + return _last_iteration @internalcode def loop(self, iterable): @@ -352,8 +353,8 @@ class LoopContextIterator(object): 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 -- 2.26.2