Fix loop.last memory issue #92

merged 1 commit into from Jan 24, 2012


None yet

2 participants

We are using a generator to input data to jinja2 templates. When we render, the data is streamed out using TemplateStream, which works great unless we have loop.last in our template.

The code for loop.last currently reads in the whole iterator (in our case a generator which reads files) in order to determine the length of the loop, and check to see if this is the last iteration.

There is an alternative to checking the length, which is to keep the next element in the iteration stored in memory, and checking that element for a sentinel value to see if the loop is on the last iteration.

That is the approach I took in this pull request. We are patching our jinja2 packages internally, in order to allow us to use loop.last on big data (though loop.length is still dangerous, and by extension, the repr method on LoopContext). Thanks.

Jason Kotenko Fixed loop.last to not consume the entire iterator to determine if th…
…is is

the last iteration of the loop.
@mitsuhiko mitsuhiko merged commit ea89feb into pallets:master Jan 24, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment