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
Provenance
- Commit
74230e6- Tree
2e35ecc- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2b48839
3 files changed+54 added−16 removed
modified:jinja2/compiler.py+24 added-15 removed
8 unmodified lines9 :license: BSD.10 """11 from cStringIO import StringIO12 from itertools import chain13+from copy import deepcopy14 from jinja2 import nodes15 from jinja2.visitor import NodeVisitor, NodeTransformer16 from jinja2.exceptions import TemplateAssertionError17 from jinja2.utils import Markup, concat, escape, is_python_keyword, next97 unmodified lines115 if local_only:116 return False117 return name in self.declared118 119+ def copy(self):120+ return deepcopy(self)121 122+123 class Frame(object):124 """Holds compile time information for us."""125 126 def __init__(self, parent=None):143 unmodified lines270 self.identifiers.undeclared.add(node.name)271 272 def visit_If(self, node):273 self.visit(node.test)274+ real_identifiers = self.identifiers275 271- # 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_parameter279 277- # 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 rv290 291+ body = inner_visit(node.body)292+ else_ = inner_visit(node.else_ or ())293+294 # 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_))297 287- # 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_)300 301 def visit_Macro(self, node):302 self.identifiers.declared_locally.add(node.name)303 1244 unmodified linesmodified:jinja2/utils.py+5 added-1 removed
626 unmodified lines627 """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] = value145 unmodified linesmodified:tests/test_old_bugs.py+25 added
86 unmodified lines87 '{% macro foo() %}CHILD{% endmacro %}'88 '{% block body %}{{ foo() }}{% endblock %}')89 }))90 assert env.get_template('child.html').render() == 'CHILD'91+92+93+def 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'