Fixed bug in finalizer
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 14 Jul 2008 11:41:46 +0000 (13:41 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 14 Jul 2008 11:41:46 +0000 (13:41 +0200)
--HG--
branch : trunk

CHANGES
jinja2/compiler.py
tests/test_various.py

diff --git a/CHANGES b/CHANGES
index f27f5f435b0baff2d9e9ae33c937b1864503bf73..b9410d47cf6827518f9eb2ce64ad68c952074bef 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,8 @@ Version 2.0
 - `filesizeformat` filter uses decimal prefixes now per default and can be
   set to binary mode with the second parameter.
 
+- fixed bug in finalizer
+
 Version 2.0rc1
 --------------
 (no codename, released on June 9th 2008)
index a90526ce05ae6d4ea565b1cc17a8f0d2dadf6a04..2d96d0d432d1ee443090151acc855ec9ad27c8aa 100644 (file)
@@ -1045,6 +1045,11 @@ class CodeGenerator(NodeVisitor):
         if self.has_known_extends and frame.toplevel:
             return
 
+        if self.environment.finalize:
+            finalize = lambda x: unicode(self.environment.finalize(x))
+        else:
+            finalize = unicode
+
         self.newline(node)
 
         # if we are in the toplevel scope and there was already an extends
@@ -1071,7 +1076,7 @@ class CodeGenerator(NodeVisitor):
                         const = const.__html__()
                     else:
                         const = escape(const)
-                const = unicode(const)
+                const = finalize(const)
             except:
                 # if something goes wrong here we evaluate the node
                 # at runtime for easier debugging
index 36f039a2c11a35bf1a83a40447e0eb4faa4f5a9d..21244731f72e2d062f127f50aec320b9cd55036a 100644 (file)
@@ -71,3 +71,16 @@ def test_item_and_attribute():
         assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
         tmpl = env.from_string('{{ foo["items"] }}')
         assert tmpl.render(foo={'items': 42}) == '42'
+
+
+def test_finalizer():
+    from jinja2 import Environment
+    def finalize_none_empty(value):
+        if value is None:
+            value = u''
+        return value
+    env = Environment(finalize=finalize_none_empty)
+    tmpl = env.from_string('{% for item in seq %}|{{ item }}{% endfor %}')
+    assert tmpl.render(seq=(None, 1, "foo")) == '||1|foo'
+    tmpl = env.from_string('<{{ none }}>')
+    assert tmpl.render() == '<>'