Added decorator support for finalize. This fixes #431
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 84f39ff · Browse files · 2 files·+26 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+26 added−2 removed
- other:+26 added−2 removed
Provenance
- Commit
0c1f192- Tree
b7461bb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
84f39ff
2 files changed+26 added−2 removed
modified:jinja2/compiler.py+24 added-2 removed
1217 unmodified lines1218 # if we are in a require_output_check section1219 if self.has_known_extends and frame.require_output_check:1220 return1221 1222+ allow_constant_finalize = True1223 if self.environment.finalize:1224+ func = self.environment.finalize1225+ if getattr(func, 'contextfunction', False) or \1226+ getattr(func, 'evalcontextfunction', False):1227+ allow_constant_finalize = False1228+ elif getattr(func, 'environmentfunction', False):1229+ finalize = lambda x: text_type(1230+ self.environment.finalize(self.environment, x))1231+ else:1223- finalize = lambda x: text_type(self.environment.finalize(x))1232+ finalize = lambda x: text_type(self.environment.finalize(x))1233 else:1234 finalize = text_type1235 1236 # if we are inside a frame that requires output checking, we do so7 unmodified lines1244 # string at compile time.1245 body = []1246 for child in node.nodes:1247 try:1248+ if not allow_constant_finalize:1249+ raise nodes.Impossible()1250 const = child.as_const(frame.eval_ctx)1251 except nodes.Impossible:1252 body.append(child)1253 continue47 unmodified lines1301 else:1302 self.write('to_string(')1303 if self.environment.finalize is not None:1304 self.write('environment.finalize(')1305+ if getattr(self.environment.finalize,1306+ "contextfunction", False):1307+ self.write('context, ')1308 close += 11309 self.visit(item, frame)1310 self.write(')' * close)1311 if frame.buffer is not None:14 unmodified lines1326 format.append('%s')1327 arguments.append(item)1328 self.writeline('yield ')1329 self.write(repr(concat(format)) + ' % (')1316- idx = -11330 self.indent()1331 for argument in arguments:1332 self.newline(argument)1333 close = 05 unmodified lines1339 self.write('escape(')1340 close += 11341 if self.environment.finalize is not None:1342 self.write('environment.finalize(')1343+ if getattr(self.environment.finalize,1344+ 'contextfunction', False):1345+ self.write('context, ')1346+ elif getattr(self.environment.finalize,1347+ 'evalcontextfunction', False):1348+ self.write('context.eval_ctx, ')1349+ elif getattr(self.environment.finalize,1350+ 'environmentfunction', False):1351+ self.write('environment, ')1352 close += 11353 self.visit(argument, frame)1354 self.write(')' * close + ', ')1355 self.outdent()331 unmodified linesmodified:CHANGES+2 added
27 unmodified lines28 value being applied to `y`).29 - Add ability to use custom subclasses of ``jinja2.compiler.CodeGenerator`` and30 ``jinja2.runtime.Context`` by adding two new attributes to the environment31 (`code_generator_class` and `context_class`) (pull request ``#404``).32+- added support for context/environment/evalctx decorator functions on33+ the finalize callback of the environment.34 35 Version 2.7.336 -------------37 (bugfix release, released on June 6th 2014)328 unmodified lines