Skip to content
Myr

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 file

Take 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 back
1"""The optimizer tries to constant fold expressions and modify the AST
2in 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 back
10

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 11–12why, one step back
11import typing as t
12

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 13–15why, one step back
13from . import nodes
14from .visitor import NodeTransformer15

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 16–17why, one step back
16if t.TYPE_CHECKING:
17 from .environment import Environment

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 18why, one step back
18

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 19–20why, one step back
19
20def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 21–23why, one step back
21 """The context hint can be used to perform an static optimization
22 based on the context given."""23 optimizer = Optimizer(environment)

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 24why, one step back
24 return t.cast(nodes.Node, optimizer.visit(node))

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 25–27why, one step back
25
2627class Optimizer(NodeTransformer):

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 28why, one step back
28 def __init__(self, environment: "Environment | None") -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 29–30why, one step back
29 self.environment = environment
30

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 31–33why, one step back
31 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 back
34 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