2022-01-24 04:07:52 +00:00
|
|
|
"""Compiles nodes from the parser into Python code."""
|
|
|
|
import typing as t
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from functools import update_wrapper
|
|
|
|
from io import StringIO
|
2019-12-02 21:46:54 +00:00
|
|
|
from itertools import chain
|
|
|
|
from keyword import iskeyword as is_python_keyword
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from markupsafe import escape
|
|
|
|
from markupsafe import Markup
|
|
|
|
|
|
|
|
from . import nodes
|
|
|
|
from .exceptions import TemplateAssertionError
|
|
|
|
from .idtracking import Symbols
|
|
|
|
from .idtracking import VAR_LOAD_ALIAS
|
|
|
|
from .idtracking import VAR_LOAD_PARAMETER
|
|
|
|
from .idtracking import VAR_LOAD_RESOLVE
|
|
|
|
from .idtracking import VAR_LOAD_UNDEFINED
|
|
|
|
from .nodes import EvalContext
|
|
|
|
from .optimizer import Optimizer
|
|
|
|
from .utils import _PassArg
|
|
|
|
from .utils import concat
|
|
|
|
from .visitor import NodeVisitor
|
|
|
|
|
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
import typing_extensions as te
|
|
|
|
from .environment import Environment
|
|
|
|
|
|
|
|
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
operators = {
|
2022-01-24 04:07:52 +00:00
|
|
|
"eq": "==",
|
|
|
|
"ne": "!=",
|
|
|
|
"gt": ">",
|
|
|
|
"gteq": ">=",
|
|
|
|
"lt": "<",
|
|
|
|
"lteq": "<=",
|
|
|
|
"in": "in",
|
|
|
|
"notin": "not in",
|
2019-12-02 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
def optimizeconst(f: F) -> F:
|
|
|
|
def new_func(
|
|
|
|
self: "CodeGenerator", node: nodes.Expr, frame: "Frame", **kwargs: t.Any
|
|
|
|
) -> t.Any:
|
2019-12-02 21:46:54 +00:00
|
|
|
# Only optimize if the frame is not volatile
|
2022-01-24 04:07:52 +00:00
|
|
|
if self.optimizer is not None and not frame.eval_ctx.volatile:
|
2019-12-02 21:46:54 +00:00
|
|
|
new_node = self.optimizer.visit(node, frame.eval_ctx)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if new_node != node:
|
|
|
|
return self.visit(new_node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
return f(self, node, frame, **kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
return update_wrapper(t.cast(F, new_func), f)
|
|
|
|
|
|
|
|
|
|
|
|
def _make_binop(op: str) -> t.Callable[["CodeGenerator", nodes.BinExpr, "Frame"], None]:
|
|
|
|
@optimizeconst
|
|
|
|
def visitor(self: "CodeGenerator", node: nodes.BinExpr, frame: Frame) -> None:
|
|
|
|
if (
|
|
|
|
self.environment.sandboxed
|
|
|
|
and op in self.environment.intercepted_binops # type: ignore
|
|
|
|
):
|
|
|
|
self.write(f"environment.call_binop(context, {op!r}, ")
|
|
|
|
self.visit(node.left, frame)
|
|
|
|
self.write(", ")
|
|
|
|
self.visit(node.right, frame)
|
|
|
|
else:
|
|
|
|
self.write("(")
|
|
|
|
self.visit(node.left, frame)
|
|
|
|
self.write(f" {op} ")
|
|
|
|
self.visit(node.right, frame)
|
|
|
|
|
|
|
|
self.write(")")
|
|
|
|
|
|
|
|
return visitor
|
|
|
|
|
|
|
|
|
|
|
|
def _make_unop(
|
|
|
|
op: str,
|
|
|
|
) -> t.Callable[["CodeGenerator", nodes.UnaryExpr, "Frame"], None]:
|
|
|
|
@optimizeconst
|
|
|
|
def visitor(self: "CodeGenerator", node: nodes.UnaryExpr, frame: Frame) -> None:
|
|
|
|
if (
|
|
|
|
self.environment.sandboxed
|
|
|
|
and op in self.environment.intercepted_unops # type: ignore
|
|
|
|
):
|
|
|
|
self.write(f"environment.call_unop(context, {op!r}, ")
|
|
|
|
self.visit(node.node, frame)
|
|
|
|
else:
|
|
|
|
self.write("(" + op)
|
|
|
|
self.visit(node.node, frame)
|
|
|
|
|
|
|
|
self.write(")")
|
|
|
|
|
|
|
|
return visitor
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def generate(
|
|
|
|
node: nodes.Template,
|
|
|
|
environment: "Environment",
|
|
|
|
name: t.Optional[str],
|
|
|
|
filename: t.Optional[str],
|
|
|
|
stream: t.Optional[t.TextIO] = None,
|
|
|
|
defer_init: bool = False,
|
|
|
|
optimized: bool = True,
|
|
|
|
) -> t.Optional[str]:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Generate the python source for a node tree."""
|
|
|
|
if not isinstance(node, nodes.Template):
|
2022-01-24 04:07:52 +00:00
|
|
|
raise TypeError("Can't compile non template nodes")
|
|
|
|
|
|
|
|
generator = environment.code_generator_class(
|
|
|
|
environment, name, filename, stream, defer_init, optimized
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
generator.visit(node)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if stream is None:
|
2022-01-24 04:07:52 +00:00
|
|
|
return generator.stream.getvalue() # type: ignore
|
|
|
|
|
|
|
|
return None
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def has_safe_repr(value: t.Any) -> bool:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Does the node have a safe representation?"""
|
|
|
|
if value is None or value is NotImplemented or value is Ellipsis:
|
|
|
|
return True
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
if type(value) in {bool, int, float, complex, range, str, Markup}:
|
2019-12-02 21:46:54 +00:00
|
|
|
return True
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
if type(value) in {tuple, list, set, frozenset}:
|
|
|
|
return all(has_safe_repr(v) for v in value)
|
|
|
|
|
|
|
|
if type(value) is dict:
|
|
|
|
return all(has_safe_repr(k) and has_safe_repr(v) for k, v in value.items())
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def find_undeclared(
|
|
|
|
nodes: t.Iterable[nodes.Node], names: t.Iterable[str]
|
|
|
|
) -> t.Set[str]:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Check if the names passed are accessed undeclared. The return value
|
|
|
|
is a set of all the undeclared names from the sequence of names found.
|
|
|
|
"""
|
|
|
|
visitor = UndeclaredNameVisitor(names)
|
|
|
|
try:
|
|
|
|
for node in nodes:
|
|
|
|
visitor.visit(node)
|
|
|
|
except VisitorExit:
|
|
|
|
pass
|
|
|
|
return visitor.undeclared
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class MacroRef:
|
|
|
|
def __init__(self, node: t.Union[nodes.Macro, nodes.CallBlock]) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.node = node
|
|
|
|
self.accesses_caller = False
|
|
|
|
self.accesses_kwargs = False
|
|
|
|
self.accesses_varargs = False
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class Frame:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Holds compile time information for us."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
eval_ctx: EvalContext,
|
|
|
|
parent: t.Optional["Frame"] = None,
|
|
|
|
level: t.Optional[int] = None,
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.eval_ctx = eval_ctx
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
# the parent of this frame
|
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
if parent is None:
|
|
|
|
self.symbols = Symbols(level=level)
|
|
|
|
|
|
|
|
# in some dynamic inheritance situations the compiler needs to add
|
|
|
|
# write tests around output statements.
|
|
|
|
self.require_output_check = False
|
|
|
|
|
|
|
|
# inside some tags we are using a buffer rather than yield statements.
|
|
|
|
# this for example affects {% filter %} or {% macro %}. If a frame
|
|
|
|
# is buffered this variable points to the name of the list used as
|
|
|
|
# buffer.
|
|
|
|
self.buffer: t.Optional[str] = None
|
|
|
|
|
|
|
|
# the name of the block we're in, otherwise None.
|
|
|
|
self.block: t.Optional[str] = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.symbols = Symbols(parent.symbols, level=level)
|
|
|
|
self.require_output_check = parent.require_output_check
|
|
|
|
self.buffer = parent.buffer
|
|
|
|
self.block = parent.block
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# a toplevel frame is the root + soft frames such as if conditions.
|
|
|
|
self.toplevel = False
|
|
|
|
|
|
|
|
# the root frame is basically just the outermost frame, so no if
|
|
|
|
# conditions. This information is used to optimize inheritance
|
|
|
|
# situations.
|
|
|
|
self.rootlevel = False
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# variables set inside of loops and blocks should not affect outer frames,
|
|
|
|
# but they still needs to be kept track of as part of the active context.
|
|
|
|
self.loop_frame = False
|
|
|
|
self.block_frame = False
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# track whether the frame is being used in an if-statement or conditional
|
|
|
|
# expression as it determines which errors should be raised during runtime
|
|
|
|
# or compile time.
|
|
|
|
self.soft_frame = False
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def copy(self) -> "Frame":
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Create a copy of the current one."""
|
2022-01-24 04:07:52 +00:00
|
|
|
rv = t.cast(Frame, object.__new__(self.__class__))
|
2019-12-02 21:46:54 +00:00
|
|
|
rv.__dict__.update(self.__dict__)
|
|
|
|
rv.symbols = self.symbols.copy()
|
|
|
|
return rv
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def inner(self, isolated: bool = False) -> "Frame":
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Return an inner frame."""
|
|
|
|
if isolated:
|
|
|
|
return Frame(self.eval_ctx, level=self.symbols.level + 1)
|
|
|
|
return Frame(self.eval_ctx, self)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def soft(self) -> "Frame":
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Return a soft frame. A soft frame may not be modified as
|
|
|
|
standalone thing as it shares the resources with the frame it
|
|
|
|
was created of, but it's not a rootlevel frame any longer.
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
This is only used to implement if-statements and conditional
|
|
|
|
expressions.
|
2019-12-02 21:46:54 +00:00
|
|
|
"""
|
|
|
|
rv = self.copy()
|
|
|
|
rv.rootlevel = False
|
2022-01-24 04:07:52 +00:00
|
|
|
rv.soft_frame = True
|
2019-12-02 21:46:54 +00:00
|
|
|
return rv
|
|
|
|
|
|
|
|
__copy__ = copy
|
|
|
|
|
|
|
|
|
|
|
|
class VisitorExit(RuntimeError):
|
|
|
|
"""Exception used by the `UndeclaredNameVisitor` to signal a stop."""
|
|
|
|
|
|
|
|
|
|
|
|
class DependencyFinderVisitor(NodeVisitor):
|
|
|
|
"""A visitor that collects filter and test calls."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.filters: t.Set[str] = set()
|
|
|
|
self.tests: t.Set[str] = set()
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Filter(self, node: nodes.Filter) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.generic_visit(node)
|
|
|
|
self.filters.add(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Test(self, node: nodes.Test) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.generic_visit(node)
|
|
|
|
self.tests.add(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Block(self, node: nodes.Block) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Stop visiting at blocks."""
|
|
|
|
|
|
|
|
|
|
|
|
class UndeclaredNameVisitor(NodeVisitor):
|
|
|
|
"""A visitor that checks if a name is accessed without being
|
|
|
|
declared. This is different from the frame visitor as it will
|
|
|
|
not stop at closure frames.
|
|
|
|
"""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self, names: t.Iterable[str]) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.names = set(names)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.undeclared: t.Set[str] = set()
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Name(self, node: nodes.Name) -> None:
|
|
|
|
if node.ctx == "load" and node.name in self.names:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.undeclared.add(node.name)
|
|
|
|
if self.undeclared == self.names:
|
|
|
|
raise VisitorExit()
|
|
|
|
else:
|
|
|
|
self.names.discard(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Block(self, node: nodes.Block) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Stop visiting a blocks."""
|
|
|
|
|
|
|
|
|
|
|
|
class CompilerExit(Exception):
|
|
|
|
"""Raised if the compiler encountered a situation where it just
|
|
|
|
doesn't make sense to further process the code. Any block that
|
|
|
|
raises such an exception is not further processed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class CodeGenerator(NodeVisitor):
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
environment: "Environment",
|
|
|
|
name: t.Optional[str],
|
|
|
|
filename: t.Optional[str],
|
|
|
|
stream: t.Optional[t.TextIO] = None,
|
|
|
|
defer_init: bool = False,
|
|
|
|
optimized: bool = True,
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if stream is None:
|
2022-01-24 04:07:52 +00:00
|
|
|
stream = StringIO()
|
2019-12-02 21:46:54 +00:00
|
|
|
self.environment = environment
|
|
|
|
self.name = name
|
|
|
|
self.filename = filename
|
|
|
|
self.stream = stream
|
|
|
|
self.created_block_context = False
|
|
|
|
self.defer_init = defer_init
|
2022-01-24 04:07:52 +00:00
|
|
|
self.optimizer: t.Optional[Optimizer] = None
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if optimized:
|
|
|
|
self.optimizer = Optimizer(environment)
|
|
|
|
|
|
|
|
# aliases for imports
|
2022-01-24 04:07:52 +00:00
|
|
|
self.import_aliases: t.Dict[str, str] = {}
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# a registry for all blocks. Because blocks are moved out
|
|
|
|
# into the global python scope they are registered here
|
2022-01-24 04:07:52 +00:00
|
|
|
self.blocks: t.Dict[str, nodes.Block] = {}
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# the number of extends statements so far
|
|
|
|
self.extends_so_far = 0
|
|
|
|
|
|
|
|
# some templates have a rootlevel extends. In this case we
|
|
|
|
# can safely assume that we're a child template and do some
|
|
|
|
# more optimizations.
|
|
|
|
self.has_known_extends = False
|
|
|
|
|
|
|
|
# the current line number
|
|
|
|
self.code_lineno = 1
|
|
|
|
|
|
|
|
# registry of all filters and tests (global, not block local)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.tests: t.Dict[str, str] = {}
|
|
|
|
self.filters: t.Dict[str, str] = {}
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# the debug information
|
2022-01-24 04:07:52 +00:00
|
|
|
self.debug_info: t.List[t.Tuple[int, int]] = []
|
|
|
|
self._write_debug_info: t.Optional[int] = None
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# the number of new lines before the next write()
|
|
|
|
self._new_lines = 0
|
|
|
|
|
|
|
|
# the line number of the last written statement
|
|
|
|
self._last_line = 0
|
|
|
|
|
|
|
|
# true if nothing was written so far.
|
|
|
|
self._first_write = True
|
|
|
|
|
|
|
|
# used by the `temporary_identifier` method to get new
|
|
|
|
# unique, temporary identifier
|
|
|
|
self._last_identifier = 0
|
|
|
|
|
|
|
|
# the current indentation
|
|
|
|
self._indentation = 0
|
|
|
|
|
|
|
|
# Tracks toplevel assignments
|
2022-01-24 04:07:52 +00:00
|
|
|
self._assign_stack: t.List[t.Set[str]] = []
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# Tracks parameter definition blocks
|
2022-01-24 04:07:52 +00:00
|
|
|
self._param_def_block: t.List[t.Set[str]] = []
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# Tracks the current context.
|
2022-01-24 04:07:52 +00:00
|
|
|
self._context_reference_stack = ["context"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def optimized(self) -> bool:
|
|
|
|
return self.optimizer is not None
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# -- Various compilation helpers
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def fail(self, msg: str, lineno: int) -> "te.NoReturn":
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Fail with a :exc:`TemplateAssertionError`."""
|
|
|
|
raise TemplateAssertionError(msg, lineno, self.name, self.filename)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def temporary_identifier(self) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Get a new unique identifier."""
|
|
|
|
self._last_identifier += 1
|
2022-01-24 04:07:52 +00:00
|
|
|
return f"t_{self._last_identifier}"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def buffer(self, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Enable buffering for the frame from that point onwards."""
|
|
|
|
frame.buffer = self.temporary_identifier()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{frame.buffer} = []")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def return_buffer_contents(
|
|
|
|
self, frame: Frame, force_unescaped: bool = False
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Return the buffer contents of the frame."""
|
|
|
|
if not force_unescaped:
|
|
|
|
if frame.eval_ctx.volatile:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if context.eval_ctx.autoescape:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"return Markup(concat({frame.buffer}))")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("else:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"return concat({frame.buffer})")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
return
|
|
|
|
elif frame.eval_ctx.autoescape:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"return Markup(concat({frame.buffer}))")
|
2019-12-02 21:46:54 +00:00
|
|
|
return
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"return concat({frame.buffer})")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def indent(self) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Indent by one."""
|
|
|
|
self._indentation += 1
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def outdent(self, step: int = 1) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Outdent by step."""
|
|
|
|
self._indentation -= step
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def start_write(self, frame: Frame, node: t.Optional[nodes.Node] = None) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Yield or write into the frame buffer."""
|
|
|
|
if frame.buffer is None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("yield ", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{frame.buffer}.append(", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def end_write(self, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""End the writing process started by `start_write`."""
|
|
|
|
if frame.buffer is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def simple_write(
|
|
|
|
self, s: str, frame: Frame, node: t.Optional[nodes.Node] = None
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Simple shortcut for start_write + write + end_write."""
|
|
|
|
self.start_write(frame, node)
|
|
|
|
self.write(s)
|
|
|
|
self.end_write(frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def blockvisit(self, nodes: t.Iterable[nodes.Node], frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Visit a list of nodes as block in a frame. If the current frame
|
|
|
|
is no buffer a dummy ``if 0: yield None`` is written automatically.
|
|
|
|
"""
|
|
|
|
try:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("pass")
|
2019-12-02 21:46:54 +00:00
|
|
|
for node in nodes:
|
|
|
|
self.visit(node, frame)
|
|
|
|
except CompilerExit:
|
|
|
|
pass
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def write(self, x: str) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Write a string into the output stream."""
|
|
|
|
if self._new_lines:
|
|
|
|
if not self._first_write:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.stream.write("\n" * self._new_lines)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.code_lineno += self._new_lines
|
|
|
|
if self._write_debug_info is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.debug_info.append((self._write_debug_info, self.code_lineno))
|
2019-12-02 21:46:54 +00:00
|
|
|
self._write_debug_info = None
|
|
|
|
self._first_write = False
|
2022-01-24 04:07:52 +00:00
|
|
|
self.stream.write(" " * self._indentation)
|
2019-12-02 21:46:54 +00:00
|
|
|
self._new_lines = 0
|
|
|
|
self.stream.write(x)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def writeline(
|
|
|
|
self, x: str, node: t.Optional[nodes.Node] = None, extra: int = 0
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Combination of newline and write."""
|
|
|
|
self.newline(node, extra)
|
|
|
|
self.write(x)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def newline(self, node: t.Optional[nodes.Node] = None, extra: int = 0) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Add one or more newlines before the next write."""
|
|
|
|
self._new_lines = max(self._new_lines, 1 + extra)
|
|
|
|
if node is not None and node.lineno != self._last_line:
|
|
|
|
self._write_debug_info = node.lineno
|
|
|
|
self._last_line = node.lineno
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def signature(
|
|
|
|
self,
|
|
|
|
node: t.Union[nodes.Call, nodes.Filter, nodes.Test],
|
|
|
|
frame: Frame,
|
|
|
|
extra_kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Writes a function call to the stream for the current node.
|
|
|
|
A leading comma is added automatically. The extra keyword
|
|
|
|
arguments may not include python keywords otherwise a syntax
|
2022-01-24 04:07:52 +00:00
|
|
|
error could occur. The extra keyword arguments should be given
|
2019-12-02 21:46:54 +00:00
|
|
|
as python dict.
|
|
|
|
"""
|
|
|
|
# if any of the given keyword arguments is a python keyword
|
|
|
|
# we have to make sure that no invalid call is created.
|
2022-01-24 04:07:52 +00:00
|
|
|
kwarg_workaround = any(
|
|
|
|
is_python_keyword(t.cast(str, k))
|
|
|
|
for k in chain((x.key for x in node.kwargs), extra_kwargs or ())
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
for arg in node.args:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(arg, frame)
|
|
|
|
|
|
|
|
if not kwarg_workaround:
|
|
|
|
for kwarg in node.kwargs:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(kwarg, frame)
|
|
|
|
if extra_kwargs is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
for key, value in extra_kwargs.items():
|
|
|
|
self.write(f", {key}={value}")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.dyn_args:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", *")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.dyn_args, frame)
|
|
|
|
|
|
|
|
if kwarg_workaround:
|
|
|
|
if node.dyn_kwargs is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", **dict({")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", **{")
|
2019-12-02 21:46:54 +00:00
|
|
|
for kwarg in node.kwargs:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"{kwarg.key!r}: ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(kwarg.value, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
if extra_kwargs is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
for key, value in extra_kwargs.items():
|
|
|
|
self.write(f"{key!r}: {value}, ")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.dyn_kwargs is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("}, **")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.dyn_kwargs, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
elif node.dyn_kwargs is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", **")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.dyn_kwargs, frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def pull_dependencies(self, nodes: t.Iterable[nodes.Node]) -> None:
|
|
|
|
"""Find all filter and test names used in the template and
|
|
|
|
assign them to variables in the compiled namespace. Checking
|
|
|
|
that the names are registered with the environment is done when
|
|
|
|
compiling the Filter and Test nodes. If the node is in an If or
|
|
|
|
CondExpr node, the check is done at runtime instead.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.0
|
|
|
|
Filters and tests in If and CondExpr nodes are checked at
|
|
|
|
runtime instead of compile time.
|
|
|
|
"""
|
2019-12-02 21:46:54 +00:00
|
|
|
visitor = DependencyFinderVisitor()
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
for node in nodes:
|
|
|
|
visitor.visit(node)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
for id_map, names, dependency in (self.filters, visitor.filters, "filters"), (
|
|
|
|
self.tests,
|
|
|
|
visitor.tests,
|
|
|
|
"tests",
|
|
|
|
):
|
|
|
|
for name in sorted(names):
|
|
|
|
if name not in id_map:
|
|
|
|
id_map[name] = self.temporary_identifier()
|
|
|
|
|
|
|
|
# add check during runtime that dependencies used inside of executed
|
|
|
|
# blocks are defined, as this step may be skipped during compile time
|
|
|
|
self.writeline("try:")
|
|
|
|
self.indent()
|
|
|
|
self.writeline(f"{id_map[name]} = environment.{dependency}[{name!r}]")
|
|
|
|
self.outdent()
|
|
|
|
self.writeline("except KeyError:")
|
|
|
|
self.indent()
|
|
|
|
self.writeline("@internalcode")
|
|
|
|
self.writeline(f"def {id_map[name]}(*unused):")
|
|
|
|
self.indent()
|
|
|
|
self.writeline(
|
|
|
|
f'raise TemplateRuntimeError("No {dependency[:-1]}'
|
|
|
|
f' named {name!r} found.")'
|
|
|
|
)
|
|
|
|
self.outdent()
|
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
def enter_frame(self, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
undefs = []
|
2022-01-24 04:07:52 +00:00
|
|
|
for target, (action, param) in frame.symbols.loads.items():
|
2019-12-02 21:46:54 +00:00
|
|
|
if action == VAR_LOAD_PARAMETER:
|
|
|
|
pass
|
|
|
|
elif action == VAR_LOAD_RESOLVE:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{target} = {self.get_resolve_func()}({param!r})")
|
2019-12-02 21:46:54 +00:00
|
|
|
elif action == VAR_LOAD_ALIAS:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{target} = {param}")
|
2019-12-02 21:46:54 +00:00
|
|
|
elif action == VAR_LOAD_UNDEFINED:
|
|
|
|
undefs.append(target)
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
raise NotImplementedError("unknown load instruction")
|
2019-12-02 21:46:54 +00:00
|
|
|
if undefs:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{' = '.join(undefs)} = missing")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def leave_frame(self, frame: Frame, with_python_scope: bool = False) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if not with_python_scope:
|
|
|
|
undefs = []
|
2022-01-24 04:07:52 +00:00
|
|
|
for target in frame.symbols.loads:
|
2019-12-02 21:46:54 +00:00
|
|
|
undefs.append(target)
|
|
|
|
if undefs:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{' = '.join(undefs)} = missing")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def choose_async(self, async_value: str = "async ", sync_value: str = "") -> str:
|
|
|
|
return async_value if self.environment.is_async else sync_value
|
|
|
|
|
|
|
|
def func(self, name: str) -> str:
|
|
|
|
return f"{self.choose_async()}def {name}"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def macro_body(
|
|
|
|
self, node: t.Union[nodes.Macro, nodes.CallBlock], frame: Frame
|
|
|
|
) -> t.Tuple[Frame, MacroRef]:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Dump the function def of a macro or call block."""
|
|
|
|
frame = frame.inner()
|
|
|
|
frame.symbols.analyze_node(node)
|
|
|
|
macro_ref = MacroRef(node)
|
|
|
|
|
|
|
|
explicit_caller = None
|
|
|
|
skip_special_params = set()
|
|
|
|
args = []
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
for idx, arg in enumerate(node.args):
|
2022-01-24 04:07:52 +00:00
|
|
|
if arg.name == "caller":
|
2019-12-02 21:46:54 +00:00
|
|
|
explicit_caller = idx
|
2022-01-24 04:07:52 +00:00
|
|
|
if arg.name in ("kwargs", "varargs"):
|
2019-12-02 21:46:54 +00:00
|
|
|
skip_special_params.add(arg.name)
|
|
|
|
args.append(frame.symbols.ref(arg.name))
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
undeclared = find_undeclared(node.body, ("caller", "kwargs", "varargs"))
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if "caller" in undeclared:
|
|
|
|
# In older Jinja versions there was a bug that allowed caller
|
2019-12-02 21:46:54 +00:00
|
|
|
# to retain the special behavior even if it was mentioned in
|
|
|
|
# the argument list. However thankfully this was only really
|
|
|
|
# working if it was the last argument. So we are explicitly
|
|
|
|
# checking this now and error out if it is anywhere else in
|
|
|
|
# the argument list.
|
|
|
|
if explicit_caller is not None:
|
|
|
|
try:
|
|
|
|
node.defaults[explicit_caller - len(node.args)]
|
|
|
|
except IndexError:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.fail(
|
|
|
|
"When defining macros or call blocks the "
|
|
|
|
'special "caller" argument must be omitted '
|
|
|
|
"or be given a default.",
|
|
|
|
node.lineno,
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
args.append(frame.symbols.declare_parameter("caller"))
|
2019-12-02 21:46:54 +00:00
|
|
|
macro_ref.accesses_caller = True
|
2022-01-24 04:07:52 +00:00
|
|
|
if "kwargs" in undeclared and "kwargs" not in skip_special_params:
|
|
|
|
args.append(frame.symbols.declare_parameter("kwargs"))
|
2019-12-02 21:46:54 +00:00
|
|
|
macro_ref.accesses_kwargs = True
|
2022-01-24 04:07:52 +00:00
|
|
|
if "varargs" in undeclared and "varargs" not in skip_special_params:
|
|
|
|
args.append(frame.symbols.declare_parameter("varargs"))
|
2019-12-02 21:46:54 +00:00
|
|
|
macro_ref.accesses_varargs = True
|
|
|
|
|
|
|
|
# macros are delayed, they never require output checks
|
|
|
|
frame.require_output_check = False
|
|
|
|
frame.symbols.analyze_node(node)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{self.func('macro')}({', '.join(args)}):", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
|
|
|
|
self.buffer(frame)
|
|
|
|
self.enter_frame(frame)
|
|
|
|
|
|
|
|
self.push_parameter_definitions(frame)
|
|
|
|
for idx, arg in enumerate(node.args):
|
|
|
|
ref = frame.symbols.ref(arg.name)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"if {ref} is missing:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
try:
|
|
|
|
default = node.defaults[idx - len(node.args)]
|
|
|
|
except IndexError:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f'{ref} = undefined("parameter {arg.name!r} was not provided",'
|
|
|
|
f" name={arg.name!r})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{ref} = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(default, frame)
|
|
|
|
self.mark_parameter_stored(ref)
|
|
|
|
self.outdent()
|
|
|
|
self.pop_parameter_definitions()
|
|
|
|
|
|
|
|
self.blockvisit(node.body, frame)
|
|
|
|
self.return_buffer_contents(frame, force_unescaped=True)
|
|
|
|
self.leave_frame(frame, with_python_scope=True)
|
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
return frame, macro_ref
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def macro_def(self, macro_ref: MacroRef, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Dump the macro definition for the def created by macro_body."""
|
2022-01-24 04:07:52 +00:00
|
|
|
arg_tuple = ", ".join(repr(x.name) for x in macro_ref.node.args)
|
|
|
|
name = getattr(macro_ref.node, "name", None)
|
2019-12-02 21:46:54 +00:00
|
|
|
if len(macro_ref.node.args) == 1:
|
2022-01-24 04:07:52 +00:00
|
|
|
arg_tuple += ","
|
|
|
|
self.write(
|
|
|
|
f"Macro(environment, macro, {name!r}, ({arg_tuple}),"
|
|
|
|
f" {macro_ref.accesses_kwargs!r}, {macro_ref.accesses_varargs!r},"
|
|
|
|
f" {macro_ref.accesses_caller!r}, context.eval_ctx.autoescape)"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def position(self, node: nodes.Node) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Return a human readable position for the node."""
|
2022-01-24 04:07:52 +00:00
|
|
|
rv = f"line {node.lineno}"
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.name is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
rv = f"{rv} in {self.name!r}"
|
2019-12-02 21:46:54 +00:00
|
|
|
return rv
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def dump_local_context(self, frame: Frame) -> str:
|
|
|
|
items_kv = ", ".join(
|
|
|
|
f"{name!r}: {target}"
|
|
|
|
for name, target in frame.symbols.dump_stores().items()
|
|
|
|
)
|
|
|
|
return f"{{{items_kv}}}"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def write_commons(self) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Writes a common preamble that is used by root and block functions.
|
|
|
|
Primarily this sets up common local helpers and enforces a generator
|
|
|
|
through a dead branch.
|
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("resolve = context.resolve_or_missing")
|
|
|
|
self.writeline("undefined = environment.undefined")
|
|
|
|
# always use the standard Undefined class for the implicit else of
|
|
|
|
# conditional expressions
|
|
|
|
self.writeline("cond_expr_undefined = Undefined")
|
|
|
|
self.writeline("if 0: yield None")
|
|
|
|
|
|
|
|
def push_parameter_definitions(self, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Pushes all parameter targets from the given frame into a local
|
|
|
|
stack that permits tracking of yet to be assigned parameters. In
|
|
|
|
particular this enables the optimization from `visit_Name` to skip
|
|
|
|
undefined expressions for parameters in macros as macros can reference
|
|
|
|
otherwise unbound parameters.
|
|
|
|
"""
|
|
|
|
self._param_def_block.append(frame.symbols.dump_param_targets())
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def pop_parameter_definitions(self) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Pops the current parameter definitions set."""
|
|
|
|
self._param_def_block.pop()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def mark_parameter_stored(self, target: str) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Marks a parameter in the current parameter definitions as stored.
|
|
|
|
This will skip the enforced undefined checks.
|
|
|
|
"""
|
|
|
|
if self._param_def_block:
|
|
|
|
self._param_def_block[-1].discard(target)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def push_context_reference(self, target: str) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self._context_reference_stack.append(target)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def pop_context_reference(self) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self._context_reference_stack.pop()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def get_context_ref(self) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
return self._context_reference_stack[-1]
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def get_resolve_func(self) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
target = self._context_reference_stack[-1]
|
2022-01-24 04:07:52 +00:00
|
|
|
if target == "context":
|
|
|
|
return "resolve"
|
|
|
|
return f"{target}.resolve"
|
|
|
|
|
|
|
|
def derive_context(self, frame: Frame) -> str:
|
|
|
|
return f"{self.get_context_ref()}.derived({self.dump_local_context(frame)})"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def parameter_is_undeclared(self, target: str) -> bool:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Checks if a given target is an undeclared parameter."""
|
|
|
|
if not self._param_def_block:
|
|
|
|
return False
|
|
|
|
return target in self._param_def_block[-1]
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def push_assign_tracking(self) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Pushes a new layer for assignment tracking."""
|
|
|
|
self._assign_stack.append(set())
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def pop_assign_tracking(self, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Pops the topmost level for assignment tracking and updates the
|
|
|
|
context variables if necessary.
|
|
|
|
"""
|
|
|
|
vars = self._assign_stack.pop()
|
2022-01-24 04:07:52 +00:00
|
|
|
if (
|
|
|
|
not frame.block_frame
|
|
|
|
and not frame.loop_frame
|
|
|
|
and not frame.toplevel
|
|
|
|
or not vars
|
|
|
|
):
|
2019-12-02 21:46:54 +00:00
|
|
|
return
|
2022-01-24 04:07:52 +00:00
|
|
|
public_names = [x for x in vars if x[:1] != "_"]
|
2019-12-02 21:46:54 +00:00
|
|
|
if len(vars) == 1:
|
|
|
|
name = next(iter(vars))
|
|
|
|
ref = frame.symbols.ref(name)
|
2022-01-24 04:07:52 +00:00
|
|
|
if frame.loop_frame:
|
|
|
|
self.writeline(f"_loop_vars[{name!r}] = {ref}")
|
|
|
|
return
|
|
|
|
if frame.block_frame:
|
|
|
|
self.writeline(f"_block_vars[{name!r}] = {ref}")
|
|
|
|
return
|
|
|
|
self.writeline(f"context.vars[{name!r}] = {ref}")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
if frame.loop_frame:
|
|
|
|
self.writeline("_loop_vars.update({")
|
|
|
|
elif frame.block_frame:
|
|
|
|
self.writeline("_block_vars.update({")
|
|
|
|
else:
|
|
|
|
self.writeline("context.vars.update({")
|
2019-12-02 21:46:54 +00:00
|
|
|
for idx, name in enumerate(vars):
|
|
|
|
if idx:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
ref = frame.symbols.ref(name)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"{name!r}: {ref}")
|
|
|
|
self.write("})")
|
|
|
|
if not frame.block_frame and not frame.loop_frame and public_names:
|
2019-12-02 21:46:54 +00:00
|
|
|
if len(public_names) == 1:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"context.exported_vars.add({public_names[0]!r})")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
names_str = ", ".join(map(repr, public_names))
|
|
|
|
self.writeline(f"context.exported_vars.update(({names_str}))")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# -- Statement Visitors
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Template(
|
|
|
|
self, node: nodes.Template, frame: t.Optional[Frame] = None
|
|
|
|
) -> None:
|
|
|
|
assert frame is None, "no root frame allowed"
|
2019-12-02 21:46:54 +00:00
|
|
|
eval_ctx = EvalContext(self.environment, self.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from .runtime import exported, async_exported
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
exported_names = sorted(exported + async_exported)
|
|
|
|
else:
|
|
|
|
exported_names = sorted(exported)
|
|
|
|
|
|
|
|
self.writeline("from __future__ import generator_stop") # Python < 3.7
|
|
|
|
self.writeline("from jinja2.runtime import " + ", ".join(exported_names))
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# if we want a deferred initialization we cannot move the
|
|
|
|
# environment into a local name
|
2022-01-24 04:07:52 +00:00
|
|
|
envenv = "" if self.defer_init else ", environment=environment"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# do we have an extends tag at all? If not, we can save some
|
|
|
|
# overhead by just not processing any inheritance code.
|
|
|
|
have_extends = node.find(nodes.Extends) is not None
|
|
|
|
|
|
|
|
# find all blocks
|
|
|
|
for block in node.find_all(nodes.Block):
|
|
|
|
if block.name in self.blocks:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.fail(f"block {block.name!r} defined twice", block.lineno)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.blocks[block.name] = block
|
|
|
|
|
|
|
|
# find all imports and import them
|
|
|
|
for import_ in node.find_all(nodes.ImportedName):
|
|
|
|
if import_.importname not in self.import_aliases:
|
|
|
|
imp = import_.importname
|
|
|
|
self.import_aliases[imp] = alias = self.temporary_identifier()
|
2022-01-24 04:07:52 +00:00
|
|
|
if "." in imp:
|
|
|
|
module, obj = imp.rsplit(".", 1)
|
|
|
|
self.writeline(f"from {module} import {obj} as {alias}")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"import {imp} as {alias}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# add the load name
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"name = {self.name!r}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# generate the root render function.
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f"{self.func('root')}(context, missing=missing{envenv}):", extra=1
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.write_commons()
|
|
|
|
|
|
|
|
# process the root
|
|
|
|
frame = Frame(eval_ctx)
|
2022-01-24 04:07:52 +00:00
|
|
|
if "self" in find_undeclared(node.body, ("self",)):
|
|
|
|
ref = frame.symbols.declare_parameter("self")
|
|
|
|
self.writeline(f"{ref} = TemplateReference(context)")
|
2019-12-02 21:46:54 +00:00
|
|
|
frame.symbols.analyze_node(node)
|
|
|
|
frame.toplevel = frame.rootlevel = True
|
|
|
|
frame.require_output_check = have_extends and not self.has_known_extends
|
|
|
|
if have_extends:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("parent_template = None")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.enter_frame(frame)
|
|
|
|
self.pull_dependencies(node.body)
|
|
|
|
self.blockvisit(node.body, frame)
|
|
|
|
self.leave_frame(frame, with_python_scope=True)
|
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
# make sure that the parent root is called.
|
|
|
|
if have_extends:
|
|
|
|
if not self.has_known_extends:
|
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if parent_template is not None:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
if not self.environment.is_async:
|
|
|
|
self.writeline("yield from parent_template.root_render_func(context)")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
"async for event in parent_template.root_render_func(context):"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("yield event")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
self.outdent(1 + (not self.has_known_extends))
|
|
|
|
|
|
|
|
# at this point we now have the blocks collected and can visit them too.
|
2022-01-24 04:07:52 +00:00
|
|
|
for name, block in self.blocks.items():
|
|
|
|
self.writeline(
|
|
|
|
f"{self.func('block_' + name)}(context, missing=missing{envenv}):",
|
|
|
|
block,
|
|
|
|
1,
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.write_commons()
|
|
|
|
# It's important that we do not make this frame a child of the
|
|
|
|
# toplevel template. This would cause a variety of
|
|
|
|
# interesting issues with identifier tracking.
|
|
|
|
block_frame = Frame(eval_ctx)
|
2022-01-24 04:07:52 +00:00
|
|
|
block_frame.block_frame = True
|
|
|
|
undeclared = find_undeclared(block.body, ("self", "super"))
|
|
|
|
if "self" in undeclared:
|
|
|
|
ref = block_frame.symbols.declare_parameter("self")
|
|
|
|
self.writeline(f"{ref} = TemplateReference(context)")
|
|
|
|
if "super" in undeclared:
|
|
|
|
ref = block_frame.symbols.declare_parameter("super")
|
|
|
|
self.writeline(f"{ref} = context.super({name!r}, block_{name})")
|
2019-12-02 21:46:54 +00:00
|
|
|
block_frame.symbols.analyze_node(block)
|
|
|
|
block_frame.block = name
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("_block_vars = {}")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.enter_frame(block_frame)
|
|
|
|
self.pull_dependencies(block.body)
|
|
|
|
self.blockvisit(block.body, block_frame)
|
|
|
|
self.leave_frame(block_frame, with_python_scope=True)
|
|
|
|
self.outdent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
blocks_kv_str = ", ".join(f"{x!r}: block_{x}" for x in self.blocks)
|
|
|
|
self.writeline(f"blocks = {{{blocks_kv_str}}}", extra=1)
|
|
|
|
debug_kv_str = "&".join(f"{k}={v}" for k, v in self.debug_info)
|
|
|
|
self.writeline(f"debug_info = {debug_kv_str!r}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Block(self, node: nodes.Block, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Call a block and register it for the template."""
|
|
|
|
level = 0
|
|
|
|
if frame.toplevel:
|
|
|
|
# if we know that we are a child template, there is no need to
|
|
|
|
# check if we are one
|
|
|
|
if self.has_known_extends:
|
|
|
|
return
|
|
|
|
if self.extends_so_far > 0:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if parent_template is None:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
level += 1
|
|
|
|
|
|
|
|
if node.scoped:
|
|
|
|
context = self.derive_context(frame)
|
|
|
|
else:
|
|
|
|
context = self.get_context_ref()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if node.required:
|
|
|
|
self.writeline(f"if len(context.blocks[{node.name!r}]) <= 1:", node)
|
|
|
|
self.indent()
|
|
|
|
self.writeline(
|
|
|
|
f'raise TemplateRuntimeError("Required block {node.name!r} not found")',
|
|
|
|
node,
|
|
|
|
)
|
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
if not self.environment.is_async and frame.buffer is None:
|
|
|
|
self.writeline(
|
|
|
|
f"yield from context.blocks[{node.name!r}][0]({context})", node
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f"{self.choose_async()}for event in"
|
|
|
|
f" context.blocks[{node.name!r}][0]({context}):",
|
|
|
|
node,
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.simple_write("event", frame)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
self.outdent(level)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Extends(self, node: nodes.Extends, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Calls the extender."""
|
|
|
|
if not frame.toplevel:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.fail("cannot use extend from a non top-level scope", node.lineno)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# if the number of extends statements in general is zero so
|
|
|
|
# far, we don't have to add a check if something extended
|
|
|
|
# the template before this one.
|
|
|
|
if self.extends_so_far > 0:
|
|
|
|
|
|
|
|
# if we have a known extends we just add a template runtime
|
|
|
|
# error into the generated code. We could catch that at compile
|
|
|
|
# time too, but i welcome it not to confuse users by throwing the
|
|
|
|
# same error at different times just "because we can".
|
|
|
|
if not self.has_known_extends:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if parent_template is not None:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline('raise TemplateRuntimeError("extended multiple times")')
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
# if we have a known extends already we don't need that code here
|
|
|
|
# as we know that the template execution will end here.
|
|
|
|
if self.has_known_extends:
|
|
|
|
raise CompilerExit()
|
|
|
|
else:
|
|
|
|
self.outdent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("parent_template = environment.get_template(", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.template, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f", {self.name!r})")
|
|
|
|
self.writeline("for name, parent_block in parent_template.blocks.items():")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("context.blocks.setdefault(name, []).append(parent_block)")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
# if this extends statement was in the root level we can take
|
|
|
|
# advantage of that information and simplify the generated code
|
|
|
|
# in the top level from this point onwards
|
|
|
|
if frame.rootlevel:
|
|
|
|
self.has_known_extends = True
|
|
|
|
|
|
|
|
# and now we have one more
|
|
|
|
self.extends_so_far += 1
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Include(self, node: nodes.Include, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Handles includes."""
|
|
|
|
if node.ignore_missing:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("try:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "get_or_select_template"
|
2019-12-02 21:46:54 +00:00
|
|
|
if isinstance(node.template, nodes.Const):
|
2022-01-24 04:07:52 +00:00
|
|
|
if isinstance(node.template.value, str):
|
|
|
|
func_name = "get_template"
|
2019-12-02 21:46:54 +00:00
|
|
|
elif isinstance(node.template.value, (tuple, list)):
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "select_template"
|
2019-12-02 21:46:54 +00:00
|
|
|
elif isinstance(node.template, (nodes.Tuple, nodes.List)):
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "select_template"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"template = environment.{func_name}(", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.template, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f", {self.name!r})")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.ignore_missing:
|
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("except TemplateNotFound:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("pass")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("else:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
|
|
|
|
skip_event_yield = False
|
|
|
|
if node.with_context:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f"{self.choose_async()}for event in template.root_render_func("
|
|
|
|
"template.new_context(context.get_all(), True,"
|
|
|
|
f" {self.dump_local_context(frame)})):"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
elif self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
"for event in (await template._get_default_module_async())"
|
|
|
|
"._body_stream:"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("yield from template._get_default_module()._body_stream")
|
|
|
|
skip_event_yield = True
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if not skip_event_yield:
|
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.simple_write("event", frame)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
if node.ignore_missing:
|
|
|
|
self.outdent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _import_common(
|
|
|
|
self, node: t.Union[nodes.Import, nodes.FromImport], frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write(f"{self.choose_async('await ')}environment.get_template(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.template, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f", {self.name!r}).")
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.with_context:
|
2022-01-24 04:07:52 +00:00
|
|
|
f_name = f"make_module{self.choose_async('_async')}"
|
|
|
|
self.write(
|
|
|
|
f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"_get_default_module{self.choose_async('_async')}(context)")
|
|
|
|
|
|
|
|
def visit_Import(self, node: nodes.Import, frame: Frame) -> None:
|
|
|
|
"""Visit regular imports."""
|
|
|
|
self.writeline(f"{frame.symbols.ref(node.target)} = ", node)
|
|
|
|
if frame.toplevel:
|
|
|
|
self.write(f"context.vars[{node.target!r}] = ")
|
|
|
|
|
|
|
|
self._import_common(node, frame)
|
|
|
|
|
|
|
|
if frame.toplevel and not node.target.startswith("_"):
|
|
|
|
self.writeline(f"context.exported_vars.discard({node.target!r})")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Visit named imports."""
|
|
|
|
self.newline(node)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("included_template = ")
|
|
|
|
self._import_common(node, frame)
|
2019-12-02 21:46:54 +00:00
|
|
|
var_names = []
|
|
|
|
discarded_names = []
|
|
|
|
for name in node.names:
|
|
|
|
if isinstance(name, tuple):
|
|
|
|
name, alias = name
|
|
|
|
else:
|
|
|
|
alias = name
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f"{frame.symbols.ref(alias)} ="
|
|
|
|
f" getattr(included_template, {name!r}, missing)"
|
|
|
|
)
|
|
|
|
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
message = (
|
|
|
|
"the template {included_template.__name__!r}"
|
|
|
|
f" (imported on {self.position(node)})"
|
|
|
|
f" does not export the requested name {name!r}"
|
|
|
|
)
|
|
|
|
self.writeline(
|
|
|
|
f"{frame.symbols.ref(alias)} = undefined(f{message!r}, name={name!r})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
if frame.toplevel:
|
|
|
|
var_names.append(alias)
|
2022-01-24 04:07:52 +00:00
|
|
|
if not alias.startswith("_"):
|
2019-12-02 21:46:54 +00:00
|
|
|
discarded_names.append(alias)
|
|
|
|
|
|
|
|
if var_names:
|
|
|
|
if len(var_names) == 1:
|
|
|
|
name = var_names[0]
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"context.vars[{name!r}] = {frame.symbols.ref(name)}")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
names_kv = ", ".join(
|
|
|
|
f"{name!r}: {frame.symbols.ref(name)}" for name in var_names
|
|
|
|
)
|
|
|
|
self.writeline(f"context.vars.update({{{names_kv}}})")
|
2019-12-02 21:46:54 +00:00
|
|
|
if discarded_names:
|
|
|
|
if len(discarded_names) == 1:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"context.exported_vars.discard({discarded_names[0]!r})")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
names_str = ", ".join(map(repr, discarded_names))
|
|
|
|
self.writeline(
|
|
|
|
f"context.exported_vars.difference_update(({names_str}))"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_For(self, node: nodes.For, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
loop_frame = frame.inner()
|
2022-01-24 04:07:52 +00:00
|
|
|
loop_frame.loop_frame = True
|
2019-12-02 21:46:54 +00:00
|
|
|
test_frame = frame.inner()
|
|
|
|
else_frame = frame.inner()
|
|
|
|
|
|
|
|
# try to figure out if we have an extended loop. An extended loop
|
|
|
|
# is necessary if the loop is in recursive mode if the special loop
|
2022-01-24 04:07:52 +00:00
|
|
|
# variable is accessed in the body if the body is a scoped block.
|
|
|
|
extended_loop = (
|
|
|
|
node.recursive
|
|
|
|
or "loop"
|
|
|
|
in find_undeclared(node.iter_child_nodes(only=("body",)), ("loop",))
|
|
|
|
or any(block.scoped for block in node.find_all(nodes.Block))
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
loop_ref = None
|
|
|
|
if extended_loop:
|
2022-01-24 04:07:52 +00:00
|
|
|
loop_ref = loop_frame.symbols.declare_parameter("loop")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
loop_frame.symbols.analyze_node(node, for_branch="body")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.else_:
|
2022-01-24 04:07:52 +00:00
|
|
|
else_frame.symbols.analyze_node(node, for_branch="else")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if node.test:
|
|
|
|
loop_filter_func = self.temporary_identifier()
|
2022-01-24 04:07:52 +00:00
|
|
|
test_frame.symbols.analyze_node(node, for_branch="test")
|
|
|
|
self.writeline(f"{self.func(loop_filter_func)}(fiter):", node.test)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.enter_frame(test_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(self.choose_async("async for ", "for "))
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.target, loop_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" in ")
|
|
|
|
self.write(self.choose_async("auto_aiter(fiter)", "fiter"))
|
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if ", node.test)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.test, test_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("yield ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.target, loop_frame)
|
|
|
|
self.outdent(3)
|
|
|
|
self.leave_frame(test_frame, with_python_scope=True)
|
|
|
|
|
|
|
|
# if we don't have an recursive loop we have to find the shadowed
|
|
|
|
# variables at that point. Because loops can be nested but the loop
|
|
|
|
# variable is a special one we have to enforce aliasing for it.
|
|
|
|
if node.recursive:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
f"{self.func('loop')}(reciter, loop_render_func, depth=0):", node
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.buffer(loop_frame)
|
|
|
|
|
|
|
|
# Use the same buffer for the else frame
|
|
|
|
else_frame.buffer = loop_frame.buffer
|
|
|
|
|
|
|
|
# make sure the loop variable is a special one and raise a template
|
|
|
|
# assertion error if a loop tries to write to loop
|
|
|
|
if extended_loop:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{loop_ref} = missing")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
for name in node.find_all(nodes.Name):
|
2022-01-24 04:07:52 +00:00
|
|
|
if name.ctx == "store" and name.name == "loop":
|
|
|
|
self.fail(
|
|
|
|
"Can't assign to special loop variable in for-loop target",
|
|
|
|
name.lineno,
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if node.else_:
|
|
|
|
iteration_indicator = self.temporary_identifier()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{iteration_indicator} = 1")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(self.choose_async("async for ", "for "), node)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.target, loop_frame)
|
|
|
|
if extended_loop:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f", {loop_ref} in {self.choose_async('Async')}LoopContext(")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" in ")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if node.test:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"{loop_filter_func}(")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.recursive:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("reciter")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
|
|
|
if self.environment.is_async and not extended_loop:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("auto_aiter(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.iter, frame)
|
|
|
|
if self.environment.is_async and not extended_loop:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.test:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if node.recursive:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", undefined, loop_render_func, depth):")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", undefined):" if extended_loop else ":")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
self.indent()
|
|
|
|
self.enter_frame(loop_frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("_loop_vars = {}")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.blockvisit(node.body, loop_frame)
|
|
|
|
if node.else_:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{iteration_indicator} = 0")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.leave_frame(
|
|
|
|
loop_frame, with_python_scope=node.recursive and not node.else_
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if node.else_:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"if {iteration_indicator}:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.enter_frame(else_frame)
|
|
|
|
self.blockvisit(node.else_, else_frame)
|
|
|
|
self.leave_frame(else_frame)
|
|
|
|
self.outdent()
|
|
|
|
|
|
|
|
# if the node was recursive we have to return the buffer contents
|
|
|
|
# and start the iteration code
|
|
|
|
if node.recursive:
|
|
|
|
self.return_buffer_contents(loop_frame)
|
|
|
|
self.outdent()
|
|
|
|
self.start_write(frame, node)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"{self.choose_async('await ')}loop(")
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("auto_aiter(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.iter, frame)
|
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
|
|
|
self.write(", loop)")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.end_write(frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# at the end of the iteration, clear any assignments made in the
|
|
|
|
# loop from the top level
|
|
|
|
if self._assign_stack:
|
|
|
|
self._assign_stack[-1].difference_update(loop_frame.symbols.stores)
|
|
|
|
|
|
|
|
def visit_If(self, node: nodes.If, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if_frame = frame.soft()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("if ", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.test, if_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.blockvisit(node.body, if_frame)
|
|
|
|
self.outdent()
|
|
|
|
for elif_ in node.elif_:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("elif ", elif_)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(elif_.test, if_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.blockvisit(elif_.body, if_frame)
|
|
|
|
self.outdent()
|
|
|
|
if node.else_:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("else:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
self.blockvisit(node.else_, if_frame)
|
|
|
|
self.outdent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Macro(self, node: nodes.Macro, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
macro_frame, macro_ref = self.macro_body(node, frame)
|
|
|
|
self.newline()
|
|
|
|
if frame.toplevel:
|
2022-01-24 04:07:52 +00:00
|
|
|
if not node.name.startswith("_"):
|
|
|
|
self.write(f"context.exported_vars.add({node.name!r})")
|
|
|
|
self.writeline(f"context.vars[{node.name!r}] = ")
|
|
|
|
self.write(f"{frame.symbols.ref(node.name)} = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.macro_def(macro_ref, macro_frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_CallBlock(self, node: nodes.CallBlock, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
call_frame, macro_ref = self.macro_body(node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline("caller = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.macro_def(macro_ref, call_frame)
|
|
|
|
self.start_write(frame, node)
|
|
|
|
self.visit_Call(node.call, frame, forward_caller=True)
|
|
|
|
self.end_write(frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_FilterBlock(self, node: nodes.FilterBlock, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
filter_frame = frame.inner()
|
|
|
|
filter_frame.symbols.analyze_node(node)
|
|
|
|
self.enter_frame(filter_frame)
|
|
|
|
self.buffer(filter_frame)
|
|
|
|
self.blockvisit(node.body, filter_frame)
|
|
|
|
self.start_write(frame, node)
|
|
|
|
self.visit_Filter(node.filter, filter_frame)
|
|
|
|
self.end_write(frame)
|
|
|
|
self.leave_frame(filter_frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_With(self, node: nodes.With, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
with_frame = frame.inner()
|
|
|
|
with_frame.symbols.analyze_node(node)
|
|
|
|
self.enter_frame(with_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
for target, expr in zip(node.targets, node.values):
|
2019-12-02 21:46:54 +00:00
|
|
|
self.newline()
|
|
|
|
self.visit(target, with_frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(expr, frame)
|
|
|
|
self.blockvisit(node.body, with_frame)
|
|
|
|
self.leave_frame(with_frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_ExprStmt(self, node: nodes.ExprStmt, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.newline(node)
|
|
|
|
self.visit(node.node, frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class _FinalizeInfo(t.NamedTuple):
|
|
|
|
const: t.Optional[t.Callable[..., str]]
|
|
|
|
src: t.Optional[str]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _default_finalize(value: t.Any) -> t.Any:
|
|
|
|
"""The default finalize function if the environment isn't
|
|
|
|
configured with one. Or, if the environment has one, this is
|
|
|
|
called on that function's output for constants.
|
|
|
|
"""
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
_finalize: t.Optional[_FinalizeInfo] = None
|
|
|
|
|
|
|
|
def _make_finalize(self) -> _FinalizeInfo:
|
|
|
|
"""Build the finalize function to be used on constants and at
|
|
|
|
runtime. Cached so it's only created once for all output nodes.
|
|
|
|
|
|
|
|
Returns a ``namedtuple`` with the following attributes:
|
|
|
|
|
|
|
|
``const``
|
|
|
|
A function to finalize constant data at compile time.
|
|
|
|
|
|
|
|
``src``
|
|
|
|
Source code to output around nodes to be evaluated at
|
|
|
|
runtime.
|
|
|
|
"""
|
|
|
|
if self._finalize is not None:
|
|
|
|
return self._finalize
|
|
|
|
|
|
|
|
finalize: t.Optional[t.Callable[..., t.Any]]
|
|
|
|
finalize = default = self._default_finalize
|
|
|
|
src = None
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if self.environment.finalize:
|
2022-01-24 04:07:52 +00:00
|
|
|
src = "environment.finalize("
|
|
|
|
env_finalize = self.environment.finalize
|
|
|
|
pass_arg = {
|
|
|
|
_PassArg.context: "context",
|
|
|
|
_PassArg.eval_context: "context.eval_ctx",
|
|
|
|
_PassArg.environment: "environment",
|
|
|
|
}.get(
|
|
|
|
_PassArg.from_obj(env_finalize) # type: ignore
|
|
|
|
)
|
|
|
|
finalize = None
|
|
|
|
|
|
|
|
if pass_arg is None:
|
|
|
|
|
|
|
|
def finalize(value: t.Any) -> t.Any:
|
|
|
|
return default(env_finalize(value))
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
src = f"{src}{pass_arg}, "
|
|
|
|
|
|
|
|
if pass_arg == "environment":
|
|
|
|
|
|
|
|
def finalize(value: t.Any) -> t.Any:
|
|
|
|
return default(env_finalize(self.environment, value))
|
|
|
|
|
|
|
|
self._finalize = self._FinalizeInfo(finalize, src)
|
|
|
|
return self._finalize
|
|
|
|
|
|
|
|
def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:
|
|
|
|
"""Given a group of constant values converted from ``Output``
|
|
|
|
child nodes, produce a string to write to the template module
|
|
|
|
source.
|
|
|
|
"""
|
|
|
|
return repr(concat(group))
|
|
|
|
|
|
|
|
def _output_child_to_const(
|
|
|
|
self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo
|
|
|
|
) -> str:
|
|
|
|
"""Try to optimize a child of an ``Output`` node by trying to
|
|
|
|
convert it to constant, finalized data at compile time.
|
|
|
|
|
|
|
|
If :exc:`Impossible` is raised, the node is not constant and
|
|
|
|
will be evaluated at runtime. Any other exception will also be
|
|
|
|
evaluated at runtime for easier debugging.
|
|
|
|
"""
|
|
|
|
const = node.as_const(frame.eval_ctx)
|
|
|
|
|
|
|
|
if frame.eval_ctx.autoescape:
|
|
|
|
const = escape(const)
|
|
|
|
|
|
|
|
# Template data doesn't go through finalize.
|
|
|
|
if isinstance(node, nodes.TemplateData):
|
|
|
|
return str(const)
|
|
|
|
|
|
|
|
return finalize.const(const) # type: ignore
|
|
|
|
|
|
|
|
def _output_child_pre(
|
|
|
|
self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo
|
|
|
|
) -> None:
|
|
|
|
"""Output extra source code before visiting a child of an
|
|
|
|
``Output`` node.
|
|
|
|
"""
|
|
|
|
if frame.eval_ctx.volatile:
|
|
|
|
self.write("(escape if context.eval_ctx.autoescape else str)(")
|
|
|
|
elif frame.eval_ctx.autoescape:
|
|
|
|
self.write("escape(")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("str(")
|
|
|
|
|
|
|
|
if finalize.src is not None:
|
|
|
|
self.write(finalize.src)
|
|
|
|
|
|
|
|
def _output_child_post(
|
|
|
|
self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo
|
|
|
|
) -> None:
|
|
|
|
"""Output extra source code after visiting a child of an
|
|
|
|
``Output`` node.
|
|
|
|
"""
|
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if finalize.src is not None:
|
|
|
|
self.write(")")
|
|
|
|
|
|
|
|
def visit_Output(self, node: nodes.Output, frame: Frame) -> None:
|
|
|
|
# If an extends is active, don't render outside a block.
|
2019-12-02 21:46:54 +00:00
|
|
|
if frame.require_output_check:
|
2022-01-24 04:07:52 +00:00
|
|
|
# A top-level extends is known to exist at compile time.
|
|
|
|
if self.has_known_extends:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.writeline("if parent_template is None:")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
finalize = self._make_finalize()
|
|
|
|
body: t.List[t.Union[t.List[t.Any], nodes.Expr]] = []
|
|
|
|
|
|
|
|
# Evaluate constants at compile time if possible. Each item in
|
|
|
|
# body will be either a list of static data or a node to be
|
|
|
|
# evaluated at runtime.
|
2019-12-02 21:46:54 +00:00
|
|
|
for child in node.nodes:
|
|
|
|
try:
|
2022-01-24 04:07:52 +00:00
|
|
|
if not (
|
|
|
|
# If the finalize function requires runtime context,
|
|
|
|
# constants can't be evaluated at compile time.
|
|
|
|
finalize.const
|
|
|
|
# Unless it's basic template data that won't be
|
|
|
|
# finalized anyway.
|
|
|
|
or isinstance(child, nodes.TemplateData)
|
|
|
|
):
|
2019-12-02 21:46:54 +00:00
|
|
|
raise nodes.Impossible()
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
const = self._output_child_to_const(child, frame, finalize)
|
|
|
|
except (nodes.Impossible, Exception):
|
|
|
|
# The node was not constant and needs to be evaluated at
|
|
|
|
# runtime. Or another error was raised, which is easier
|
|
|
|
# to debug at runtime.
|
2019-12-02 21:46:54 +00:00
|
|
|
body.append(child)
|
|
|
|
continue
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if body and isinstance(body[-1], list):
|
|
|
|
body[-1].append(const)
|
|
|
|
else:
|
|
|
|
body.append([const])
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if frame.buffer is not None:
|
|
|
|
if len(body) == 1:
|
|
|
|
self.writeline(f"{frame.buffer}.append(")
|
|
|
|
else:
|
|
|
|
self.writeline(f"{frame.buffer}.extend((")
|
|
|
|
|
|
|
|
self.indent()
|
|
|
|
|
|
|
|
for item in body:
|
|
|
|
if isinstance(item, list):
|
|
|
|
# A group of constant data to join and output.
|
|
|
|
val = self._output_const_repr(item)
|
|
|
|
|
|
|
|
if frame.buffer is None:
|
|
|
|
self.writeline("yield " + val)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(val + ",")
|
|
|
|
else:
|
|
|
|
if frame.buffer is None:
|
|
|
|
self.writeline("yield ", item)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.newline(item)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# A node to be evaluated at runtime.
|
|
|
|
self._output_child_pre(item, frame, finalize)
|
|
|
|
self.visit(item, frame)
|
|
|
|
self._output_child_post(item, frame, finalize)
|
|
|
|
|
|
|
|
if frame.buffer is not None:
|
|
|
|
self.write(",")
|
|
|
|
|
|
|
|
if frame.buffer is not None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(")" if len(body) == 1 else "))")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if frame.require_output_check:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Assign(self, node: nodes.Assign, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.push_assign_tracking()
|
|
|
|
self.newline(node)
|
|
|
|
self.visit(node.target, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.node, frame)
|
|
|
|
self.pop_assign_tracking(frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_AssignBlock(self, node: nodes.AssignBlock, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.push_assign_tracking()
|
|
|
|
block_frame = frame.inner()
|
|
|
|
# This is a special case. Since a set block always captures we
|
|
|
|
# will disable output checks. This way one can use set blocks
|
|
|
|
# toplevel even in extended templates.
|
|
|
|
block_frame.require_output_check = False
|
|
|
|
block_frame.symbols.analyze_node(node)
|
|
|
|
self.enter_frame(block_frame)
|
|
|
|
self.buffer(block_frame)
|
|
|
|
self.blockvisit(node.body, block_frame)
|
|
|
|
self.newline(node)
|
|
|
|
self.visit(node.target, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" = (Markup if context.eval_ctx.autoescape else identity)(")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.filter is not None:
|
|
|
|
self.visit_Filter(node.filter, block_frame)
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"concat({block_frame.buffer})")
|
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.pop_assign_tracking(frame)
|
|
|
|
self.leave_frame(block_frame)
|
|
|
|
|
|
|
|
# -- Expression Visitors
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Name(self, node: nodes.Name, frame: Frame) -> None:
|
|
|
|
if node.ctx == "store" and (
|
|
|
|
frame.toplevel or frame.loop_frame or frame.block_frame
|
|
|
|
):
|
2019-12-02 21:46:54 +00:00
|
|
|
if self._assign_stack:
|
|
|
|
self._assign_stack[-1].add(node.name)
|
|
|
|
ref = frame.symbols.ref(node.name)
|
|
|
|
|
|
|
|
# If we are looking up a variable we might have to deal with the
|
|
|
|
# case where it's undefined. We can skip that case if the load
|
|
|
|
# instruction indicates a parameter which are always defined.
|
2022-01-24 04:07:52 +00:00
|
|
|
if node.ctx == "load":
|
2019-12-02 21:46:54 +00:00
|
|
|
load = frame.symbols.find_load(ref)
|
2022-01-24 04:07:52 +00:00
|
|
|
if not (
|
|
|
|
load is not None
|
|
|
|
and load[0] == VAR_LOAD_PARAMETER
|
|
|
|
and not self.parameter_is_undeclared(ref)
|
|
|
|
):
|
|
|
|
self.write(
|
|
|
|
f"(undefined(name={node.name!r}) if {ref} is missing else {ref})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.write(ref)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_NSRef(self, node: nodes.NSRef, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
# NSRefs can only be used to store values; since they use the normal
|
|
|
|
# `foo.bar` notation they will be parsed as a normal attribute access
|
|
|
|
# when used anywhere but in a `set` context
|
|
|
|
ref = frame.symbols.ref(node.name)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"if not isinstance({ref}, Namespace):")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.indent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(
|
|
|
|
"raise TemplateRuntimeError"
|
|
|
|
'("cannot assign attribute on non-namespace object")'
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.outdent()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{ref}[{node.attr!r}]")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Const(self, node: nodes.Const, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
val = node.as_const(frame.eval_ctx)
|
|
|
|
if isinstance(val, float):
|
|
|
|
self.write(str(val))
|
|
|
|
else:
|
|
|
|
self.write(repr(val))
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_TemplateData(self, node: nodes.TemplateData, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
try:
|
|
|
|
self.write(repr(node.as_const(frame.eval_ctx)))
|
|
|
|
except nodes.Impossible:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(
|
|
|
|
f"(Markup if context.eval_ctx.autoescape else identity)({node.data!r})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Tuple(self, node: nodes.Tuple, frame: Frame) -> None:
|
|
|
|
self.write("(")
|
2019-12-02 21:46:54 +00:00
|
|
|
idx = -1
|
|
|
|
for idx, item in enumerate(node.items):
|
|
|
|
if idx:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(item, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(",)" if idx == 0 else ")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_List(self, node: nodes.List, frame: Frame) -> None:
|
|
|
|
self.write("[")
|
2019-12-02 21:46:54 +00:00
|
|
|
for idx, item in enumerate(node.items):
|
|
|
|
if idx:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(item, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("]")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Dict(self, node: nodes.Dict, frame: Frame) -> None:
|
|
|
|
self.write("{")
|
2019-12-02 21:46:54 +00:00
|
|
|
for idx, item in enumerate(node.items):
|
|
|
|
if idx:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(item.key, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(": ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(item.value, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("}")
|
|
|
|
|
|
|
|
visit_Add = _make_binop("+")
|
|
|
|
visit_Sub = _make_binop("-")
|
|
|
|
visit_Mul = _make_binop("*")
|
|
|
|
visit_Div = _make_binop("/")
|
|
|
|
visit_FloorDiv = _make_binop("//")
|
|
|
|
visit_Pow = _make_binop("**")
|
|
|
|
visit_Mod = _make_binop("%")
|
|
|
|
visit_And = _make_binop("and")
|
|
|
|
visit_Or = _make_binop("or")
|
|
|
|
visit_Pos = _make_unop("+")
|
|
|
|
visit_Neg = _make_unop("-")
|
|
|
|
visit_Not = _make_unop("not ")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Concat(self, node: nodes.Concat, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if frame.eval_ctx.volatile:
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "(markup_join if context.eval_ctx.volatile else str_join)"
|
2019-12-02 21:46:54 +00:00
|
|
|
elif frame.eval_ctx.autoescape:
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "markup_join"
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
func_name = "str_join"
|
|
|
|
self.write(f"{func_name}((")
|
2019-12-02 21:46:54 +00:00
|
|
|
for arg in node.nodes:
|
|
|
|
self.visit(arg, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
|
|
|
self.write("))")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Compare(self, node: nodes.Compare, frame: Frame) -> None:
|
|
|
|
self.write("(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.expr, frame)
|
|
|
|
for op in node.ops:
|
|
|
|
self.visit(op, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Operand(self, node: nodes.Operand, frame: Frame) -> None:
|
|
|
|
self.write(f" {operators[node.op]} ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.expr, frame)
|
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Getattr(self, node: nodes.Getattr, frame: Frame) -> None:
|
|
|
|
if self.environment.is_async:
|
|
|
|
self.write("(await auto_await(")
|
|
|
|
|
|
|
|
self.write("environment.getattr(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f", {node.attr!r})")
|
|
|
|
|
|
|
|
if self.environment.is_async:
|
|
|
|
self.write("))")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Getitem(self, node: nodes.Getitem, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
# slices bypass the environment getitem method.
|
|
|
|
if isinstance(node.arg, nodes.Slice):
|
|
|
|
self.visit(node.node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("[")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.arg, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("]")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
if self.environment.is_async:
|
|
|
|
self.write("(await auto_await(")
|
|
|
|
|
|
|
|
self.write("environment.getitem(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(", ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.arg, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
|
|
|
|
|
|
|
if self.environment.is_async:
|
|
|
|
self.write("))")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Slice(self, node: nodes.Slice, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.start is not None:
|
|
|
|
self.visit(node.start, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.stop is not None:
|
|
|
|
self.visit(node.stop, frame)
|
|
|
|
if node.step is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(":")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.step, frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
@contextmanager
|
|
|
|
def _filter_test_common(
|
|
|
|
self, node: t.Union[nodes.Filter, nodes.Test], frame: Frame, is_filter: bool
|
|
|
|
) -> t.Iterator[None]:
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("await auto_await(")
|
|
|
|
|
|
|
|
if is_filter:
|
|
|
|
self.write(f"{self.filters[node.name]}(")
|
|
|
|
func = self.environment.filters.get(node.name)
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(f"{self.tests[node.name]}(")
|
|
|
|
func = self.environment.tests.get(node.name)
|
|
|
|
|
|
|
|
# When inside an If or CondExpr frame, allow the filter to be
|
|
|
|
# undefined at compile time and only raise an error if it's
|
|
|
|
# actually called at runtime. See pull_dependencies.
|
|
|
|
if func is None and not frame.soft_frame:
|
|
|
|
type_name = "filter" if is_filter else "test"
|
|
|
|
self.fail(f"No {type_name} named {node.name!r}.", node.lineno)
|
|
|
|
|
|
|
|
pass_arg = {
|
|
|
|
_PassArg.context: "context",
|
|
|
|
_PassArg.eval_context: "context.eval_ctx",
|
|
|
|
_PassArg.environment: "environment",
|
|
|
|
}.get(
|
|
|
|
_PassArg.from_obj(func) # type: ignore
|
|
|
|
)
|
|
|
|
|
|
|
|
if pass_arg is not None:
|
|
|
|
self.write(f"{pass_arg}, ")
|
|
|
|
|
|
|
|
# Back to the visitor function to handle visiting the target of
|
|
|
|
# the filter or test.
|
|
|
|
yield
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
self.signature(node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Filter(self, node: nodes.Filter, frame: Frame) -> None:
|
|
|
|
with self._filter_test_common(node, frame, True):
|
|
|
|
# if the filter node is None we are inside a filter block
|
|
|
|
# and want to write to the current buffer
|
|
|
|
if node.node is not None:
|
|
|
|
self.visit(node.node, frame)
|
|
|
|
elif frame.eval_ctx.volatile:
|
|
|
|
self.write(
|
|
|
|
f"(Markup(concat({frame.buffer}))"
|
|
|
|
f" if context.eval_ctx.autoescape else concat({frame.buffer}))"
|
|
|
|
)
|
|
|
|
elif frame.eval_ctx.autoescape:
|
|
|
|
self.write(f"Markup(concat({frame.buffer}))")
|
|
|
|
else:
|
|
|
|
self.write(f"concat({frame.buffer})")
|
|
|
|
|
|
|
|
@optimizeconst
|
|
|
|
def visit_Test(self, node: nodes.Test, frame: Frame) -> None:
|
|
|
|
with self._filter_test_common(node, frame, False):
|
|
|
|
self.visit(node.node, frame)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_CondExpr(self, node: nodes.CondExpr, frame: Frame) -> None:
|
|
|
|
frame = frame.soft()
|
|
|
|
|
|
|
|
def write_expr2() -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if node.expr2 is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.visit(node.expr2, frame)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.write(
|
|
|
|
f'cond_expr_undefined("the inline if-expression on'
|
|
|
|
f" {self.position(node)} evaluated to false and no else"
|
|
|
|
f' section was defined.")'
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.expr1, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" if ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.test, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(" else ")
|
2019-12-02 21:46:54 +00:00
|
|
|
write_expr2()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
@optimizeconst
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Call(
|
|
|
|
self, node: nodes.Call, frame: Frame, forward_caller: bool = False
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("await auto_await(")
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.sandboxed:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("environment.call(context, ")
|
2019-12-02 21:46:54 +00:00
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write("context.call(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.node, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
extra_kwargs = {"caller": "caller"} if forward_caller else None
|
|
|
|
loop_kwargs = {"_loop_vars": "_loop_vars"} if frame.loop_frame else {}
|
|
|
|
block_kwargs = {"_block_vars": "_block_vars"} if frame.block_frame else {}
|
|
|
|
if extra_kwargs:
|
|
|
|
extra_kwargs.update(loop_kwargs, **block_kwargs)
|
|
|
|
elif loop_kwargs or block_kwargs:
|
|
|
|
extra_kwargs = dict(loop_kwargs, **block_kwargs)
|
2019-12-02 21:46:54 +00:00
|
|
|
self.signature(node, frame, extra_kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.environment.is_async:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Keyword(self, node: nodes.Keyword, frame: Frame) -> None:
|
|
|
|
self.write(node.key + "=")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.value, frame)
|
|
|
|
|
|
|
|
# -- Unused nodes for extensions
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_MarkSafe(self, node: nodes.MarkSafe, frame: Frame) -> None:
|
|
|
|
self.write("Markup(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.expr, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_MarkSafeIfAutoescape(
|
|
|
|
self, node: nodes.MarkSafeIfAutoescape, frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write("(Markup if context.eval_ctx.autoescape else identity)(")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.expr, frame)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.write(")")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_EnvironmentAttribute(
|
|
|
|
self, node: nodes.EnvironmentAttribute, frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write("environment." + node.name)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_ExtensionAttribute(
|
|
|
|
self, node: nodes.ExtensionAttribute, frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write(f"environment.extensions[{node.identifier!r}].{node.name}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_ImportedName(self, node: nodes.ImportedName, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.write(self.import_aliases[node.importname])
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_InternalName(self, node: nodes.InternalName, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.write(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_ContextReference(
|
|
|
|
self, node: nodes.ContextReference, frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write("context")
|
|
|
|
|
|
|
|
def visit_DerivedContextReference(
|
|
|
|
self, node: nodes.DerivedContextReference, frame: Frame
|
|
|
|
) -> None:
|
|
|
|
self.write(self.derive_context(frame))
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Continue(self, node: nodes.Continue, frame: Frame) -> None:
|
|
|
|
self.writeline("continue", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Break(self, node: nodes.Break, frame: Frame) -> None:
|
|
|
|
self.writeline("break", node)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Scope(self, node: nodes.Scope, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
scope_frame = frame.inner()
|
|
|
|
scope_frame.symbols.analyze_node(node)
|
|
|
|
self.enter_frame(scope_frame)
|
|
|
|
self.blockvisit(node.body, scope_frame)
|
|
|
|
self.leave_frame(scope_frame)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_OverlayScope(self, node: nodes.OverlayScope, frame: Frame) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
ctx = self.temporary_identifier()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{ctx} = {self.derive_context(frame)}")
|
|
|
|
self.writeline(f"{ctx}.vars = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.context, frame)
|
|
|
|
self.push_context_reference(ctx)
|
|
|
|
|
|
|
|
scope_frame = frame.inner(isolated=True)
|
|
|
|
scope_frame.symbols.analyze_node(node)
|
|
|
|
self.enter_frame(scope_frame)
|
|
|
|
self.blockvisit(node.body, scope_frame)
|
|
|
|
self.leave_frame(scope_frame)
|
|
|
|
self.pop_context_reference()
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_EvalContextModifier(
|
|
|
|
self, node: nodes.EvalContextModifier, frame: Frame
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for keyword in node.options:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"context.eval_ctx.{keyword.key} = ")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(keyword.value, frame)
|
|
|
|
try:
|
|
|
|
val = keyword.value.as_const(frame.eval_ctx)
|
|
|
|
except nodes.Impossible:
|
|
|
|
frame.eval_ctx.volatile = True
|
|
|
|
else:
|
|
|
|
setattr(frame.eval_ctx, keyword.key, val)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_ScopedEvalContextModifier(
|
|
|
|
self, node: nodes.ScopedEvalContextModifier, frame: Frame
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
old_ctx_name = self.temporary_identifier()
|
|
|
|
saved_ctx = frame.eval_ctx.save()
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"{old_ctx_name} = context.eval_ctx.save()")
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit_EvalContextModifier(node, frame)
|
|
|
|
for child in node.body:
|
|
|
|
self.visit(child, frame)
|
|
|
|
frame.eval_ctx.revert(saved_ctx)
|
2022-01-24 04:07:52 +00:00
|
|
|
self.writeline(f"context.eval_ctx.revert({old_ctx_name})")
|