optimizer.py
This file
Path
src/jinja2/optimizer.py
48 lines · 1.6 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 1–9why, one step back1"""The optimizer tries to constant fold expressions and modify the AST2in place so that it should be faster to evaluate.34Because the AST does not contain all the scoping information and the5compiler has to find that out, we cannot do all the optimizations we6want. For example, loop unrolling doesn't work because unrolled loops7would have a different scope. The solution would be a second syntax tree8that stored the scoping rules.9"""Merge branch '3.1.x'David Lordbefore adoptionde61312
line 10why, one step back10Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 11–12why, one step back11import typing as t12update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 13–15why, one step back13from . import nodes14from .visitor import NodeTransformer15Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 16–17why, one step back16if t.TYPE_CHECKING:17 from .environment import Environmentupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 18why, one step back18Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 19–20why, one step back1920def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 21–23why, one step back21 """The context hint can be used to perform an static optimization22 based on the context given."""23 optimizer = Optimizer(environment)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 24why, one step back24 return t.cast(nodes.Node, optimizer.visit(node))update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 25–27why, one step back252627class Optimizer(NodeTransformer):drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 28why, one step back28 def __init__(self, environment: "Environment | None") -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 29–30why, one step back29 self.environment = environment30Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 31–33why, one step back31 def generic_visit(32 self, node: nodes.Node, *args: t.Any, **kwargs: t.Any33 ) -> nodes.Node:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 34–48why, one step back34 node = super().generic_visit(node, *args, **kwargs)3536 # Do constant folding. Some other nodes besides Expr have37 # as_const, but folding them causes errors later on.38 if isinstance(node, nodes.Expr):39 try:40 return nodes.Const.from_untrusted(41 node.as_const(args[0] if args else None),42 lineno=node.lineno,43 environment=self.environment,44 )45 except nodes.Impossible:46 pass4748 return node