PHP 8.2.30
Preview: optimizer.py Size: 1.61 KB
//opt/alt/python37/lib/python3.7/site-packages/jinja2/optimizer.py

"""The optimizer tries to constant fold expressions and modify the AST
in place so that it should be faster to evaluate.

Because the AST does not contain all the scoping information and the
compiler has to find that out, we cannot do all the optimizations we
want. For example, loop unrolling doesn't work because unrolled loops
would have a different scope. The solution would be a second syntax tree
that stored the scoping rules.
"""
import typing as t

from . import nodes
from .visitor import NodeTransformer

if t.TYPE_CHECKING:
    from .environment import Environment


def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:
    """The context hint can be used to perform an static optimization
    based on the context given."""
    optimizer = Optimizer(environment)
    return t.cast(nodes.Node, optimizer.visit(node))


class Optimizer(NodeTransformer):
    def __init__(self, environment: "t.Optional[Environment]") -> None:
        self.environment = environment

    def generic_visit(
        self, node: nodes.Node, *args: t.Any, **kwargs: t.Any
    ) -> nodes.Node:
        node = super().generic_visit(node, *args, **kwargs)

        # Do constant folding. Some other nodes besides Expr have
        # as_const, but folding them causes errors later on.
        if isinstance(node, nodes.Expr):
            try:
                return nodes.Const.from_untrusted(
                    node.as_const(args[0] if args else None),
                    lineno=node.lineno,
                    environment=self.environment,
                )
            except nodes.Impossible:
                pass

        return node

Directory Contents

Dirs: 1 × Files: 26

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 23:11:10
Edit Download
1.90 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
12.37 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
70.52 KB lrw-r--r-- 2021-11-09 16:37:43
Edit Download
1.40 KB lrw-r--r-- 2021-04-05 17:47:37
Edit Download
8.29 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
1.24 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
59.55 KB lrw-r--r-- 2021-11-09 18:10:36
Edit Download
4.95 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
31.37 KB lrw-r--r-- 2021-05-14 01:01:18
Edit Download
51.38 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
10.47 KB lrw-r--r-- 2021-08-10 13:34:22
Edit Download
29.23 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
22.22 KB lrw-r--r-- 2021-11-09 20:21:27
Edit Download
4.29 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
3.88 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
33.74 KB lrw-r--r-- 2021-11-09 17:18:01
Edit Download
1.61 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
38.83 KB lrw-r--r-- 2021-05-14 01:01:13
Edit Download
0 B lrw-r--r-- 2021-05-10 13:52:40
Edit Download
34.23 KB lrw-r--r-- 2021-08-10 13:34:22
Edit Download
14.26 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
5.77 KB lrw-r--r-- 2021-04-10 17:20:38
Edit Download
26.34 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
3.49 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
1.73 KB lrw-r--r-- 2020-02-17 17:14:56
Edit Download
2.15 KB lrw-r--r-- 2021-11-09 20:25:25
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).