optimizer.py
This file
Path
jinja2/optimizer.py
68 lines · 2.2 KiB · Python
Last changed by
Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
History of this fileTake it
The walk stopped after 200 commits: lines older than 76c280b point at it.
improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 1–15why, one step back1# -*- coding: utf-8 -*-2"""3 jinja2.optimizer4 ~~~~~~~~~~~~~~~~56 The jinja optimizer is currently trying to constant fold a few expressions7 and modify the AST in place so that it should be easier to evaluate it.89 Because the AST does not contain all the scoping information and the10 compiler has to find that out, we cannot do all the optimizations we11 want. For example loop unrolling doesn't work because unrolled loops would12 have a different scoping.1314 The solution would be a second syntax tree that has the scoping rules stored.15Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
line 16why, one step back16 :copyright: (c) 2009 by the Jinja Team.fixed a few broken license declarations. should have been BSD not GPLArmin Ronacherbefore adoptiond776437
line 17why, one step back17 :license: BSD.improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 18–19why, one step back18"""19from jinja2 import nodesremoved unused importsArmin Ronacherbefore adoption981cbf6
line 20why, one step back20from jinja2.visitor import NodeTransformerimproved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 21–22why, one step back2122removed unused importsArmin Ronacherbefore adoption981cbf6
line 23why, one step back23def optimize(node, environment):improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 24–26why, one step back24 """The context hint can be used to perform an static optimization25 based on the context given."""26 optimizer = Optimizer(environment)removed unused importsArmin Ronacherbefore adoption981cbf6
line 27why, one step back27 return optimizer.visit(node)improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 28–34why, one step back282930class Optimizer(NodeTransformer):3132 def __init__(self, environment):33 self.environment = environment34removed unused importsArmin Ronacherbefore adoption981cbf6
lines 35–36why, one step back35 def visit_If(self, node):36 """Eliminate dead code."""more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 37–40why, one step back37 # do not optimize ifs that have a block inside so that it doesn't38 # break super().39 if node.find(nodes.Block) is not None:40 return self.generic_visit(node)improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
line 41why, one step back41 try:removed unused importsArmin Ronacherbefore adoption981cbf6
line 42why, one step back42 val = self.visit(node.test).as_const()improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
line 43why, one step back43 except nodes.Impossible:removed unused importsArmin Ronacherbefore adoption981cbf6
line 44why, one step back44 return self.generic_visit(node)improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 45–50why, one step back45 if val:46 body = node.body47 else:48 body = node.else_49 result = []50 for node in body:removed unused importsArmin Ronacherbefore adoption981cbf6
line 51why, one step back51 result.extend(self.visit_list(node))improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 52–53why, one step back52 return result53removed unused importsArmin Ronacherbefore adoption981cbf6
line 54why, one step back54 def fold(self, node):improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
line 55why, one step back55 """Do constant folding."""removed unused importsArmin Ronacherbefore adoption981cbf6
line 56why, one step back56 node = self.generic_visit(node)improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 57–65why, one step back57 try:58 return nodes.Const.from_untrusted(node.as_const(),59 lineno=node.lineno,60 environment=self.environment)61 except nodes.Impossible:62 return node6364 visit_Add = visit_Sub = visit_Mul = visit_Div = visit_FloorDiv = \65 visit_Pow = visit_Mod = visit_And = visit_Or = visit_Pos = visit_Neg = \Improved attribute and item lookup by allowing template designers to express the priority. foo.bar checks foo.bar first and then foo['bar'] and the other way round.Armin Ronacherbefore adoption6dc6f29
line 66why, one step back66 visit_Not = visit_Compare = visit_Getitem = visit_Getattr = visit_Call = \improved sandbox, added proper striptags and updated documentation to latest sphinx changesArmin Ronacherbefore adoption76c280b
lines 67–68why, one step back67 visit_Filter = visit_Test = visit_CondExpr = fold68 del fold