From: Armin Ronacher Date: Mon, 14 Jul 2008 11:41:46 +0000 (+0200) Subject: Fixed bug in finalizer X-Git-Tag: 2.0~4 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=665bfb8c95926ae92bd37cb58b783dcdf6855d2c;p=jinja2.git Fixed bug in finalizer --HG-- branch : trunk --- diff --git a/CHANGES b/CHANGES index f27f5f4..b9410d4 100644 --- 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) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index a90526c..2d96d0d 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -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 diff --git a/tests/test_various.py b/tests/test_various.py index 36f039a..2124473 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -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() == '<>'