fixed a bug with nested loops and the special loop variable.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
H: Enter commit message. Lines beginning with 'HG:' are removed. --HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 9a0078d · Browse files · 3 files·+31 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−15 removed
- tests:+6 added−0 removed
- other:+25 added−15 removed
Provenance
- Commit
ff53c78- Tree
8649bb6- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9a0078d
3 files changed+31 added−15 removed
modified:jinja2/compiler.py+21 added-15 removed
113 unmodified lines114 if local_only:115 return False116 return name in self.declared117 118- def find_shadowed(self):119- """Find all the shadowed names."""118+ def find_shadowed(self, extra=()):119+ """Find all the shadowed names. extra is an iterable of variables120+ that may be defined with `add_special` which may occour scoped.121+ """122 return (self.declared | self.outer_undeclared) & \121- (self.declared_locally | self.declared_parameter)123+ (self.declared_locally | self.declared_parameter) | \124+ set(x for x in extra if self.is_declared(x))125 126 127 class Frame(object):128 """Holds compile time information for us."""384 unmodified lines513 mapping[name] = self.temporary_identifier()514 self.writeline('%s = environment.%s[%r]' %515 (mapping[name], dependency, name))516 514- def collect_shadowed(self, frame):517+ def collect_shadowed(self, frame, extra_vars=()):518 """This function returns all the shadowed variables in a dict519 in the form name: alias and will write the required assignments520 into the current scope. No indentation takes place.521+522+ `extra_vars` is passed to `Identifiers.find_shadowed`.523 """524 aliases = {}520- for name in frame.identifiers.find_shadowed():525+ for name in frame.identifiers.find_shadowed(extra_vars):526 aliases[name] = ident = self.temporary_identifier()527 self.writeline('%s = l_%s' % (ident, name))528 return aliases529 363 unmodified lines893 extended_loop = node.recursive or 'loop' in \894 find_undeclared(node.iter_child_nodes(895 only=('body',)), ('loop',))896 892- # make sure the loop variable is a special one and raise a template893- # assertion error if a loop tries to write to loop894- loop_frame.identifiers.add_special('loop')895- for name in node.find_all(nodes.Name):896- if name.ctx == 'store' and name.name == 'loop':897- self.fail('Can\'t assign to special loop variable '898- 'in for-loop target', name.lineno)899-897 # if we don't have an recursive loop we have to find the shadowed901- # variables at that point898+ # variables at that point. Because loops can be nested but the loop899+ # variable is a special one we have to enforce aliasing for it.900 if not node.recursive:903- aliases = self.collect_shadowed(loop_frame)901+ aliases = self.collect_shadowed(loop_frame, ('loop',))902 903 # otherwise we set up a buffer and add a function def904 else:905 self.writeline('def loop(reciter, loop_render_func):', node)906 self.indent()907 self.buffer(loop_frame)908 aliases = {}909 910+ # make sure the loop variable is a special one and raise a template911+ # assertion error if a loop tries to write to loop912+ loop_frame.identifiers.add_special('loop')913+ for name in node.find_all(nodes.Name):914+ if name.ctx == 'store' and name.name == 'loop':915+ self.fail('Can\'t assign to special loop variable '916+ 'in for-loop target', name.lineno)917+918 self.pull_locals(loop_frame)919 if node.else_:920 iteration_indicator = self.temporary_identifier()921 self.writeline('%s = 1' % iteration_indicator)500 unmodified linesmodified:tests/test_forloop.py+6 added
136 unmodified lines137 138 139 def test_loop_unassignable(env):140 raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)141+142+143+def test_scoped_special_var(env):144+ t = env.from_string('{% for s in seq %}[{{ loop.first }}{% for c in s %}'145+ '|{{ loop.first }}{% endfor %}]{% endfor %}')146+ assert t.render(seq=('ab', 'cd')) == '[True|True|False][False|True|False]'modified:CHANGES+4 added
3 unmodified lines4 Version 2.15 -----------6 (codename to be selected, release date yet unknown)7 8+- fixed a bug with nested loops and the special loop variable. Before the9+ change an inner loop overwrote the loop variable from the outer one after10+ iteration.11+12 Version 2.013 -----------14 (codename jinjavitus, released on July 17th 2008)15 32 unmodified lines