Skip to content
Myr

Fixed a scoping bug that was introduced in the development version and was

Before adoption: this commit came in with the history the project adopted, so no landing records it.

triggered by multiple layers of local variables not tracked properly in if statements. --HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 2b48839 · Browse files · 3 files+54 added−16 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+54 added−16 removed

  • tests:+25 added−0 removed
  • other:+29 added−16 removed

3 files+54 added−16 removed

modified:jinja2/compiler.py+24 added-15 removed
8 unmodified lines
9 :license: BSD.10"""11from cStringIO import StringIO12from itertools import chain13from copy import deepcopy14from jinja2 import nodes15from jinja2.visitor import NodeVisitor, NodeTransformer16from jinja2.exceptions import TemplateAssertionError17from jinja2.utils import Markup, concat, escape, is_python_keyword, next
97 unmodified lines
115 if local_only:116 return False117 return name in self.declared118119 def copy(self):120 return deepcopy(self)121122123class Frame(object):124 """Holds compile time information for us."""125126 def __init__(self, parent=None):
143 unmodified lines
270 self.identifiers.undeclared.add(node.name)271272 def visit_If(self, node):273 self.visit(node.test)274 real_identifiers = self.identifiers275271 # remember all the names that are locally assigned in the body272 old_locals = self.identifiers.declared_locally.copy()273 for subnode in node.body:274 self.visit(subnode)275 body = self.identifiers.declared_locally - old_locals276 old_names = real_identifiers.declared | \277 real_identifiers.declared_locally | \278 real_identifiers.declared_parameter279277 # same for else.278 self.identifiers.declared_locally = old_locals.copy()279 for subnode in node.else_ or ():280 self.visit(subnode)281 else_ = self.identifiers.declared_locally - old_locals280 def inner_visit(nodes):281 self.identifiers = real_identifiers.copy()282 for subnode in nodes:283 self.visit(subnode)284 rv = self.identifiers.declared_locally - old_names285 # we have to remember the undeclared variables of this branch286 # because we will have to pull them.287 real_identifiers.undeclared.update(self.identifiers.undeclared)288 self.identifiers = real_identifiers289 return rv290291 body = inner_visit(node.body)292 else_ = inner_visit(node.else_ or ())293294 # the differences between the two branches are also pulled as295 # undeclared variables285 self.identifiers.undeclared.update(body.symmetric_difference(else_))296 real_identifiers.undeclared.update(body.symmetric_difference(else_))297287 # declared_locally is currently the set of all variables assigned288 # in the else part, add the new vars from body as well. That means289 # that undeclared variables if unbalanced are considered local.290 self.identifiers.declared_locally.update(body)298 # remember those that are declared.299 real_identifiers.declared_locally.update(body | else_)300301 def visit_Macro(self, node):302 self.identifiers.declared_locally.add(node.name)303
1244 unmodified lines
modified:jinja2/utils.py+5 added-1 removed
626 unmodified lines
627 """628 self._wlock.acquire()629 try:630 if key in self._mapping:631 try:631 self._remove(key)632 self._remove(key)633 except ValueError:634 # __getitem__ is not locked, it might happen635 pass636 elif len(self._mapping) == self.capacity:637 del self._mapping[self._popleft()]638 self._append(key)639 self._mapping[key] = value
145 unmodified lines
modified:tests/test_old_bugs.py+25 added
86 unmodified lines
87 '{% macro foo() %}CHILD{% endmacro %}'88 '{% block body %}{{ foo() }}{% endblock %}')89 }))90 assert env.get_template('child.html').render() == 'CHILD'919293def test_stacked_locals_scoping_bug():94 env = Environment(line_statement_prefix='#')95 t = env.from_string('''\96# for j in [1, 2]:97# set x = 198# for i in [1, 2]:99# print x100# if i % 2 == 0:101# set x = x + 1102# endif103# endfor104# endfor105# if a106# print 'A'107# elif b108# print 'B'109# elif c == d110# print 'C'111# else112# print 'D'113# endif114''')115 assert t.render(a=0, b=False, c=42, d=42.0) == '1111C'