Browse files

fixes issue with code generator that causes unbound variables

to be generated if set was used in if-blocks.

--HG--
branch : trunk
  • Loading branch information...
1 parent c4fc031 commit f1c421d6d952035cd20a036367b858fe32945f12 @mitsuhiko mitsuhiko committed Sep 17, 2009
Showing with 38 additions and 1 deletion.
  1. +7 −0 CHANGES
  2. +24 −0 jinja2/compiler.py
  3. +1 −1 setup.py
  4. +6 −0 tests/test_old_bugs.py
View
7 CHANGES
@@ -1,6 +1,13 @@
Jinja2 Changelog
================
+Version 2.2.2
+-------------
+(bugfix release, to be released soon)
+
+- fixes issue with code generator that causes unbound variables
+ to be generated if set was used in if-blocks.
+
Version 2.2.1
-------------
(bugfix release, released on September 14th 2009)
View
24 jinja2/compiler.py
@@ -265,6 +265,30 @@ def visit_Name(self, node):
self.identifiers.is_declared(node.name, self.hard_scope):
self.identifiers.undeclared.add(node.name)
+ def visit_If(self, node):
+ self.visit(node.test)
+
+ # remember all the names that are locally assigned in the body
+ old_locals = self.identifiers.declared_locally.copy()
+ for subnode in node.body:
+ self.visit(subnode)
+ body = self.identifiers.declared_locally - old_locals
+
+ # same for else.
+ self.identifiers.declared_locally = old_locals.copy()
+ for subnode in node.else_ or ():
+ self.visit(subnode)
+ else_ = self.identifiers.declared_locally - old_locals
+
+ # the differences between the two branches are also pulled as
+ # undeclared variables
+ self.identifiers.undeclared.update(body.symmetric_difference(else_))
+
+ # declared_locally is currently the set of all variables assigned
+ # in the else part, add the new vars from body as well. That means
+ # that undeclared variables if unbalanced are considered local.
+ self.identifiers.declared_locally.update(body)
+
def visit_Macro(self, node):
self.identifiers.declared_locally.add(node.name)
View
2 setup.py
@@ -49,7 +49,7 @@
setup(
name='Jinja2',
- version='2.3',
+ version='2.2.2',
url='http://jinja.pocoo.org/',
license='BSD',
author='Armin Ronacher',
View
6 tests/test_old_bugs.py
@@ -69,3 +69,9 @@ def test_old_macro_loop_scoping_bug():
tmpl = env.from_string('{% for i in (1, 2) %}{{ i }}{% endfor %}'
'{% macro i() %}3{% endmacro %}{{ i() }}')
assert tmpl.render() == '123'
+
+
+def test_partial_conditional_assignments():
+ tmpl = env.from_string('{% if b %}{% set a = 42 %}{% endif %}{{ a }}')
+ assert tmpl.render(a=23) == '23'
+ assert tmpl.render(b=True) == '42'

0 comments on commit f1c421d

Please sign in to comment.