Skip to content
Myr

fixes issue with code generator that causes unbound variables

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

to be generated if set was used in if-blocks. --HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent c4fc031 · Browse files · 4 files+38 added−1 removed
This commit

State

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

Diff stats

4 files+38 added−1 removed

  • tests:+6 added−0 removed
  • other:+32 added−1 removed

4 files+38 added−1 removed

modified:jinja2/compiler.py+24 added
263 unmodified lines
264 elif node.ctx == 'load' and not \265 self.identifiers.is_declared(node.name, self.hard_scope):266 self.identifiers.undeclared.add(node.name)267268 def visit_If(self, node):269 self.visit(node.test)270271 # 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_locals276277 # 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_locals282283 # the differences between the two branches are also pulled as284 # undeclared variables285 self.identifiers.undeclared.update(body.symmetric_difference(else_))286287 # 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)291292 def visit_Macro(self, node):293 self.identifiers.declared_locally.add(node.name)294295 def visit_Import(self, node):
1236 unmodified lines
modified:tests/test_old_bugs.py+6 added
67 unmodified lines
68def test_old_macro_loop_scoping_bug():69 tmpl = env.from_string('{% for i in (1, 2) %}{{ i }}{% endfor %}'70 '{% macro i() %}3{% endmacro %}{{ i() }}')71 assert tmpl.render() == '123'727374def test_partial_conditional_assignments():75 tmpl = env.from_string('{% if b %}{% set a = 42 %}{% endif %}{{ a }}')76 assert tmpl.render(a=23) == '23'77 assert tmpl.render(b=True) == '42'
modified:CHANGES+7 added
1Jinja2 Changelog2================34Version 2.2.25-------------6(bugfix release, to be released soon)78- fixes issue with code generator that causes unbound variables9 to be generated if set was used in if-blocks.1011Version 2.2.112-------------13(bugfix release, released on September 14th 2009)14
113 unmodified lines
modified:setup.py+1 added-1 removed
47 unmodified lines
484950setup(51 name='Jinja2',52 version='2.3',52 version='2.2.2',53 url='http://jinja.pocoo.org/',54 license='BSD',55 author='Armin Ronacher',56 author_email='armin.ronacher@active-4.com',
31 unmodified lines