]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Added decorator support for finalize. This fixes #431
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 25 May 2015 10:24:09 +0000 (12:24 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 25 May 2015 10:24:09 +0000 (12:24 +0200)
CHANGES
jinja2/compiler.py

diff --git a/CHANGES b/CHANGES
index 241a95c89b5c39060fd6f5f7bb91d825b71dbbed..cfe4c43c1b60d8f3de5be593cb7d9d9197d7fdae 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -29,6 +29,8 @@ Version 2.8
 - Add ability to use custom subclasses of ``jinja2.compiler.CodeGenerator`` and
   ``jinja2.runtime.Context`` by adding two new attributes to the environment
   (`code_generator_class` and `context_class`) (pull request ``#404``).
+- added support for context/environment/evalctx decorator functions on
+  the finalize callback of the environment.
 
 Version 2.7.3
 -------------
index 5cd872dfdf31e0e21bad3ba45fe528c5f90b7d5c..fad007b596f66fc090d155bb97a8fc5967749608 100644 (file)
@@ -1219,8 +1219,17 @@ class CodeGenerator(NodeVisitor):
         if self.has_known_extends and frame.require_output_check:
             return
 
+        allow_constant_finalize = True
         if self.environment.finalize:
-            finalize = lambda x: text_type(self.environment.finalize(x))
+            func = self.environment.finalize
+            if getattr(func, 'contextfunction', False) or \
+               getattr(func, 'evalcontextfunction', False):
+                allow_constant_finalize = False
+            elif getattr(func, 'environmentfunction', False):
+                finalize = lambda x: text_type(
+                    self.environment.finalize(self.environment, x))
+            else:
+                finalize = lambda x: text_type(self.environment.finalize(x))
         else:
             finalize = text_type
 
@@ -1236,6 +1245,8 @@ class CodeGenerator(NodeVisitor):
         body = []
         for child in node.nodes:
             try:
+                if not allow_constant_finalize:
+                    raise nodes.Impossible()
                 const = child.as_const(frame.eval_ctx)
             except nodes.Impossible:
                 body.append(child)
@@ -1291,6 +1302,9 @@ class CodeGenerator(NodeVisitor):
                         self.write('to_string(')
                     if self.environment.finalize is not None:
                         self.write('environment.finalize(')
+                        if getattr(self.environment.finalize,
+                                   "contextfunction", False):
+                            self.write('context, ')
                         close += 1
                     self.visit(item, frame)
                     self.write(')' * close)
@@ -1313,7 +1327,6 @@ class CodeGenerator(NodeVisitor):
                     arguments.append(item)
             self.writeline('yield ')
             self.write(repr(concat(format)) + ' % (')
-            idx = -1
             self.indent()
             for argument in arguments:
                 self.newline(argument)
@@ -1327,6 +1340,15 @@ class CodeGenerator(NodeVisitor):
                     close += 1
                 if self.environment.finalize is not None:
                     self.write('environment.finalize(')
+                    if getattr(self.environment.finalize,
+                               'contextfunction', False):
+                        self.write('context, ')
+                    elif getattr(self.environment.finalize,
+                               'evalcontextfunction', False):
+                        self.write('context.eval_ctx, ')
+                    elif getattr(self.environment.finalize,
+                               'environmentfunction', False):
+                        self.write('environment, ')
                     close += 1
                 self.visit(argument, frame)
                 self.write(')' * close + ', ')