class documentation

class CodeGenerator(NodeVisitor):

Known subclasses: jinja2.meta.TrackingCodeGenerator, jinja2.nativetypes.NativeCodeGenerator

View In Hierarchy

Undocumented

Class _​Finalize​Info Undocumented
Static Method ​_default​_finalize 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.
Method __init__ Undocumented
Method ​_filter​_test​_common Undocumented
Method ​_import​_common Undocumented
Method ​_make​_finalize Build the finalize function to be used on constants and at runtime. Cached so it's only created once for all output nodes.
Method ​_output​_child​_post Output extra source code after visiting a child of an Output node.
Method ​_output​_child​_pre Output extra source code before visiting a child of an Output node.
Method ​_output​_child​_to​_const Try to optimize a child of an Output node by trying to convert it to constant, finalized data at compile time.
Method ​_output​_const​_repr Given a group of constant values converted from Output child nodes, produce a string to write to the template module source.
Method blockvisit 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.
Method buffer Enable buffering for the frame from that point onwards.
Method choose​_async Undocumented
Method derive​_context Undocumented
Method dump​_local​_context Undocumented
Method end​_write End the writing process started by start_write.
Method enter​_frame Undocumented
Method fail Fail with a TemplateAssertionError.
Method func Undocumented
Method get​_context​_ref Undocumented
Method get​_resolve​_func Undocumented
Method indent Indent by one.
Method leave​_frame Undocumented
Method macro​_body Dump the function def of a macro or call block.
Method macro​_def Dump the macro definition for the def created by macro_body.
Method mark​_parameter​_stored Marks a parameter in the current parameter definitions as stored. This will skip the enforced undefined checks.
Method newline Add one or more newlines before the next write.
Method outdent Outdent by step.
Method parameter​_is​_undeclared Checks if a given target is an undeclared parameter.
Method pop​_assign​_tracking Pops the topmost level for assignment tracking and updates the context variables if necessary.
Method pop​_context​_reference Undocumented
Method pop​_parameter​_definitions Pops the current parameter definitions set.
Method position Return a human readable position for the node.
Method pull​_dependencies No summary
Method push​_assign​_tracking Pushes a new layer for assignment tracking.
Method push​_context​_reference Undocumented
Method push​_parameter​_definitions No summary
Method return​_buffer​_contents Return the buffer contents of the frame.
Method signature No summary
Method simple​_write Simple shortcut for start_write + write + end_write.
Method start​_write Yield or write into the frame buffer.
Method temporary​_identifier Get a new unique identifier.
Method visit_​Assign Undocumented
Method visit_​Assign​Block Undocumented
Method visit_​Block Call a block and register it for the template.
Method visit_​Break Undocumented
Method visit_​Call Undocumented
Method visit_​Call​Block Undocumented
Method visit_​Compare Undocumented
Method visit_​Concat Undocumented
Method visit_​Cond​Expr Undocumented
Method visit_​Const Undocumented
Method visit_​Context​Reference Undocumented
Method visit_​Continue Undocumented
Method visit_​Derived​Context​Reference Undocumented
Method visit_​Dict Undocumented
Method visit_​Environment​Attribute Undocumented
Method visit_​Eval​Context​Modifier Undocumented
Method visit_​Expr​Stmt Undocumented
Method visit_​Extends Calls the extender.
Method visit_​Extension​Attribute Undocumented
Method visit_​Filter Undocumented
Method visit_​Filter​Block Undocumented
Method visit_​For Undocumented
Method visit_​From​Import Visit named imports.
Method visit_​Getattr Undocumented
Method visit_​Getitem Undocumented
Method visit_​If Undocumented
Method visit_​Import Visit regular imports.
Method visit_​Imported​Name Undocumented
Method visit_​Include Handles includes.
Method visit_​Internal​Name Undocumented
Method visit_​Keyword Undocumented
Method visit_​List Undocumented
Method visit_​Macro Undocumented
Method visit_​Mark​Safe Undocumented
Method visit_​Mark​Safe​If​Autoescape Undocumented
Method visit_​Name Undocumented
Method visit_​NSRef Undocumented
Method visit_​Operand Undocumented
Method visit_​Output Undocumented
Method visit_​Overlay​Scope Undocumented
Method visit_​Scope Undocumented
Method visit_​Scoped​Eval​Context​Modifier Undocumented
Method visit_​Slice Undocumented
Method visit_​Template Undocumented
Method visit_​Template​Data Undocumented
Method visit_​Test Undocumented
Method visit_​Tuple Undocumented
Method visit_​With Undocumented
Method write Write a string into the output stream.
Method write​_commons 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.
Method writeline Combination of newline and write.
Class Variable visit_​Add Undocumented
Class Variable visit_​And Undocumented
Class Variable visit_​Div Undocumented
Class Variable visit_​Floor​Div Undocumented
Class Variable visit_​Mod Undocumented
Class Variable visit_​Mul Undocumented
Class Variable visit_​Neg Undocumented
Class Variable visit_​Not Undocumented
Class Variable visit_​Or Undocumented
Class Variable visit_​Pos Undocumented
Class Variable visit_​Pow Undocumented
Class Variable visit_​Sub Undocumented
Instance Variable ​_assign​_stack Undocumented
Instance Variable ​_context​_reference​_stack Undocumented
Instance Variable ​_finalize Undocumented
Instance Variable ​_first​_write Undocumented
Instance Variable ​_indentation Undocumented
Instance Variable ​_last​_identifier Undocumented
Instance Variable ​_last​_line Undocumented
Instance Variable ​_new​_lines Undocumented
Instance Variable ​_param​_def​_block Undocumented
Instance Variable ​_write​_debug​_info Undocumented
Instance Variable blocks Undocumented
Instance Variable code​_lineno Undocumented
Instance Variable created​_block​_context Undocumented
Instance Variable debug​_info Undocumented
Instance Variable defer​_init Undocumented
Instance Variable environment Undocumented
Instance Variable extends​_so​_far Undocumented
Instance Variable filename Undocumented
Instance Variable filters Undocumented
Instance Variable has​_known​_extends Undocumented
Instance Variable import​_aliases Undocumented
Instance Variable name Undocumented
Instance Variable optimizer Undocumented
Instance Variable stream Undocumented
Instance Variable tests Undocumented
Property optimized Undocumented

Inherited from NodeVisitor:

Method generic​_visit Called if no explicit visitor function exists for a node.
Method get​_visitor Return the visitor function for this node or None if no visitor exists for this node. In that case the generic visit function is used instead.
Method visit Visit a node.
@staticmethod
def _default_finalize(value):
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.
Parameters
value:t.AnyUndocumented
Returns
t.AnyUndocumented
def __init__(self, environment, name, filename, stream=None, defer_init=False, optimized=True):

Undocumented

Parameters
environment:EnvironmentUndocumented
name:t.Optional[str]Undocumented
filename:t.Optional[str]Undocumented
stream:t.Optional[t.TextIO]Undocumented
defer​_init:boolUndocumented
optimized:boolUndocumented
@contextmanager
def _filter_test_common(self, node, frame, is_filter):

Undocumented

Parameters
node:t.Union[nodes.Filter, nodes.Test]Undocumented
frame:FrameUndocumented
is​_filter:boolUndocumented
Returns
t.Iterator[None]Undocumented
def _import_common(self, node, frame):

Undocumented

Parameters
node:t.Union[nodes.Import, nodes.FromImport]Undocumented
frame:FrameUndocumented
def _make_finalize(self):

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.
Returns
_FinalizeInfoUndocumented
def _output_child_post(self, node, frame, finalize):
Output extra source code after visiting a child of an Output node.
Parameters
node:nodes.ExprUndocumented
frame:FrameUndocumented
finalize:_FinalizeInfoUndocumented
def _output_child_pre(self, node, frame, finalize):
Output extra source code before visiting a child of an Output node.
Parameters
node:nodes.ExprUndocumented
frame:FrameUndocumented
finalize:_FinalizeInfoUndocumented
def _output_child_to_const(self, node, frame, finalize):

Try to optimize a child of an Output node by trying to convert it to constant, finalized data at compile time.

If 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.

Parameters
node:nodes.ExprUndocumented
frame:FrameUndocumented
finalize:_FinalizeInfoUndocumented
Returns
strUndocumented
def _output_const_repr(self, group):
Given a group of constant values converted from Output child nodes, produce a string to write to the template module source.
Parameters
group:t.Iterable[t.Any]Undocumented
Returns
strUndocumented
def blockvisit(self, nodes, frame):
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.
Parameters
nodes:t.Iterable[nodes.Node]Undocumented
frame:FrameUndocumented
def buffer(self, frame):
Enable buffering for the frame from that point onwards.
Parameters
frame:FrameUndocumented
def choose_async(self, async_value='async ', sync_value=''):

Undocumented

Parameters
async​_value:strUndocumented
sync​_value:strUndocumented
Returns
strUndocumented
def derive_context(self, frame):

Undocumented

Parameters
frame:FrameUndocumented
Returns
strUndocumented
def dump_local_context(self, frame):

Undocumented

Parameters
frame:FrameUndocumented
Returns
strUndocumented
def end_write(self, frame):
End the writing process started by start_write.
Parameters
frame:FrameUndocumented
def enter_frame(self, frame):

Undocumented

Parameters
frame:FrameUndocumented
def fail(self, msg, lineno):
Fail with a TemplateAssertionError.
Parameters
msg:strUndocumented
lineno:intUndocumented
Returns
te.NoReturnUndocumented
def func(self, name):

Undocumented

Parameters
name:strUndocumented
Returns
strUndocumented
def get_context_ref(self):

Undocumented

Returns
strUndocumented
def get_resolve_func(self):

Undocumented

Returns
strUndocumented
def indent(self):
Indent by one.
def leave_frame(self, frame, with_python_scope=False):

Undocumented

Parameters
frame:FrameUndocumented
with​_python​_scope:boolUndocumented
def macro_body(self, node, frame):
Dump the function def of a macro or call block.
Parameters
node:t.Union[nodes.Macro, nodes.CallBlock]Undocumented
frame:FrameUndocumented
Returns
t.Tuple[Frame, MacroRef]Undocumented
def macro_def(self, macro_ref, frame):
Dump the macro definition for the def created by macro_body.
Parameters
macro​_ref:MacroRefUndocumented
frame:FrameUndocumented
def mark_parameter_stored(self, target):
Marks a parameter in the current parameter definitions as stored. This will skip the enforced undefined checks.
Parameters
target:strUndocumented
def newline(self, node=None, extra=0):
Add one or more newlines before the next write.
Parameters
node:t.Optional[nodes.Node]Undocumented
extra:intUndocumented
def outdent(self, step=1):
Outdent by step.
Parameters
step:intUndocumented
def parameter_is_undeclared(self, target):
Checks if a given target is an undeclared parameter.
Parameters
target:strUndocumented
Returns
boolUndocumented
def pop_assign_tracking(self, frame):
Pops the topmost level for assignment tracking and updates the context variables if necessary.
Parameters
frame:FrameUndocumented
def pop_context_reference(self):

Undocumented

def pop_parameter_definitions(self):
Pops the current parameter definitions set.
def position(self, node):
Return a human readable position for the node.
Parameters
node:nodes.NodeUndocumented
Returns
strUndocumented
def pull_dependencies(self, nodes):

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.

Changed in version 3.0: Filters and tests in If and CondExpr nodes are checked at runtime instead of compile time.
Parameters
nodes:t.Iterable[nodes.Node]Undocumented
def push_assign_tracking(self):
Pushes a new layer for assignment tracking.
def push_context_reference(self, target):

Undocumented

Parameters
target:strUndocumented
def push_parameter_definitions(self, frame):
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.
Parameters
frame:FrameUndocumented
def return_buffer_contents(self, frame, force_unescaped=False):
Return the buffer contents of the frame.
Parameters
frame:FrameUndocumented
force​_unescaped:boolUndocumented
def signature(self, node, frame, extra_kwargs=None):
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 error could occur. The extra keyword arguments should be given as python dict.
Parameters
node:t.Union[nodes.Call, nodes.Filter, nodes.Test]Undocumented
frame:FrameUndocumented
extra​_kwargs:t.Optional[t.Mapping[str, t.Any]]Undocumented
def simple_write(self, s, frame, node=None):
Simple shortcut for start_write + write + end_write.
Parameters
s:strUndocumented
frame:FrameUndocumented
node:t.Optional[nodes.Node]Undocumented
def start_write(self, frame, node=None):
Yield or write into the frame buffer.
Parameters
frame:FrameUndocumented
node:t.Optional[nodes.Node]Undocumented
def temporary_identifier(self):
Get a new unique identifier.
Returns
strUndocumented
def visit_Assign(self, node, frame):

Undocumented

Parameters
node:nodes.AssignUndocumented
frame:FrameUndocumented
def visit_AssignBlock(self, node, frame):

Undocumented

Parameters
node:nodes.AssignBlockUndocumented
frame:FrameUndocumented
def visit_Block(self, node, frame):
Call a block and register it for the template.
Parameters
node:nodes.BlockUndocumented
frame:FrameUndocumented
def visit_Break(self, node, frame):

Undocumented

Parameters
node:nodes.BreakUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Call(self, node, frame, forward_caller=False):

Undocumented

Parameters
node:nodes.CallUndocumented
frame:FrameUndocumented
forward​_caller:boolUndocumented
def visit_CallBlock(self, node, frame):

Undocumented

Parameters
node:nodes.CallBlockUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Compare(self, node, frame):

Undocumented

Parameters
node:nodes.CompareUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Concat(self, node, frame):

Undocumented

Parameters
node:nodes.ConcatUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_CondExpr(self, node, frame):

Undocumented

Parameters
node:nodes.CondExprUndocumented
frame:FrameUndocumented
def visit_Const(self, node, frame):

Undocumented

Parameters
node:nodes.ConstUndocumented
frame:FrameUndocumented
def visit_ContextReference(self, node, frame):

Undocumented

Parameters
node:nodes.ContextReferenceUndocumented
frame:FrameUndocumented
def visit_Continue(self, node, frame):

Undocumented

Parameters
node:nodes.ContinueUndocumented
frame:FrameUndocumented
def visit_DerivedContextReference(self, node, frame):

Undocumented

Parameters
node:nodes.DerivedContextReferenceUndocumented
frame:FrameUndocumented
def visit_Dict(self, node, frame):

Undocumented

Parameters
node:nodes.DictUndocumented
frame:FrameUndocumented
def visit_EnvironmentAttribute(self, node, frame):

Undocumented

Parameters
node:nodes.EnvironmentAttributeUndocumented
frame:FrameUndocumented
def visit_EvalContextModifier(self, node, frame):

Undocumented

Parameters
node:nodes.EvalContextModifierUndocumented
frame:FrameUndocumented
def visit_ExprStmt(self, node, frame):

Undocumented

Parameters
node:nodes.ExprStmtUndocumented
frame:FrameUndocumented
def visit_Extends(self, node, frame):
Calls the extender.
Parameters
node:nodes.ExtendsUndocumented
frame:FrameUndocumented
def visit_ExtensionAttribute(self, node, frame):

Undocumented

Parameters
node:nodes.ExtensionAttributeUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Filter(self, node, frame):

Undocumented

Parameters
node:nodes.FilterUndocumented
frame:FrameUndocumented
def visit_FilterBlock(self, node, frame):

Undocumented

Parameters
node:nodes.FilterBlockUndocumented
frame:FrameUndocumented
def visit_For(self, node, frame):

Undocumented

Parameters
node:nodes.ForUndocumented
frame:FrameUndocumented
def visit_FromImport(self, node, frame):
Visit named imports.
Parameters
node:nodes.FromImportUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Getattr(self, node, frame):

Undocumented

Parameters
node:nodes.GetattrUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Getitem(self, node, frame):

Undocumented

Parameters
node:nodes.GetitemUndocumented
frame:FrameUndocumented
def visit_If(self, node, frame):

Undocumented

Parameters
node:nodes.IfUndocumented
frame:FrameUndocumented
def visit_Import(self, node, frame):
Visit regular imports.
Parameters
node:nodes.ImportUndocumented
frame:FrameUndocumented
def visit_ImportedName(self, node, frame):

Undocumented

Parameters
node:nodes.ImportedNameUndocumented
frame:FrameUndocumented
def visit_Include(self, node, frame):
Handles includes.
Parameters
node:nodes.IncludeUndocumented
frame:FrameUndocumented
def visit_InternalName(self, node, frame):

Undocumented

Parameters
node:nodes.InternalNameUndocumented
frame:FrameUndocumented
def visit_Keyword(self, node, frame):

Undocumented

Parameters
node:nodes.KeywordUndocumented
frame:FrameUndocumented
def visit_List(self, node, frame):

Undocumented

Parameters
node:nodes.ListUndocumented
frame:FrameUndocumented
def visit_Macro(self, node, frame):

Undocumented

Parameters
node:nodes.MacroUndocumented
frame:FrameUndocumented
def visit_MarkSafe(self, node, frame):

Undocumented

Parameters
node:nodes.MarkSafeUndocumented
frame:FrameUndocumented
def visit_MarkSafeIfAutoescape(self, node, frame):

Undocumented

Parameters
node:nodes.MarkSafeIfAutoescapeUndocumented
frame:FrameUndocumented
def visit_Name(self, node, frame):

Undocumented

Parameters
node:nodes.NameUndocumented
frame:FrameUndocumented
def visit_NSRef(self, node, frame):

Undocumented

Parameters
node:nodes.NSRefUndocumented
frame:FrameUndocumented
def visit_Operand(self, node, frame):

Undocumented

Parameters
node:nodes.OperandUndocumented
frame:FrameUndocumented
def visit_Output(self, node, frame):

Undocumented

Parameters
node:nodes.OutputUndocumented
frame:FrameUndocumented
def visit_OverlayScope(self, node, frame):

Undocumented

Parameters
node:nodes.OverlayScopeUndocumented
frame:FrameUndocumented
def visit_Scope(self, node, frame):

Undocumented

Parameters
node:nodes.ScopeUndocumented
frame:FrameUndocumented
def visit_ScopedEvalContextModifier(self, node, frame):

Undocumented

Parameters
node:nodes.ScopedEvalContextModifierUndocumented
frame:FrameUndocumented
def visit_Slice(self, node, frame):

Undocumented

Parameters
node:nodes.SliceUndocumented
frame:FrameUndocumented
def visit_Template(self, node, frame=None):

Undocumented

Parameters
node:nodes.TemplateUndocumented
frame:t.Optional[Frame]Undocumented
def visit_TemplateData(self, node, frame):

Undocumented

Parameters
node:nodes.TemplateDataUndocumented
frame:FrameUndocumented
@optimizeconst
def visit_Test(self, node, frame):

Undocumented

Parameters
node:nodes.TestUndocumented
frame:FrameUndocumented
def visit_Tuple(self, node, frame):

Undocumented

Parameters
node:nodes.TupleUndocumented
frame:FrameUndocumented
def visit_With(self, node, frame):

Undocumented

Parameters
node:nodes.WithUndocumented
frame:FrameUndocumented
def write(self, x):
Write a string into the output stream.
Parameters
x:strUndocumented
def write_commons(self):
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.
def writeline(self, x, node=None, extra=0):
Combination of newline and write.
Parameters
x:strUndocumented
node:t.Optional[nodes.Node]Undocumented
extra:intUndocumented
visit_Add =

Undocumented

visit_And =

Undocumented

visit_Div =

Undocumented

visit_FloorDiv =

Undocumented

visit_Mod =

Undocumented

visit_Mul =

Undocumented

visit_Neg =

Undocumented

visit_Not =

Undocumented

visit_Or =

Undocumented

visit_Pos =

Undocumented

visit_Pow =

Undocumented

visit_Sub =

Undocumented

_assign_stack: t.List[t.Set[str]] =

Undocumented

_context_reference_stack: list[str] =

Undocumented

_finalize =

Undocumented

_first_write: bool =

Undocumented

_indentation: int =

Undocumented

_last_identifier: int =

Undocumented

_last_line =

Undocumented

_new_lines =

Undocumented

_param_def_block: t.List[t.Set[str]] =

Undocumented

_write_debug_info =

Undocumented

blocks: t.Dict[str, nodes.Block] =

Undocumented

code_lineno: int =

Undocumented

created_block_context: bool =

Undocumented

debug_info: t.List[t.Tuple[int, int]] =

Undocumented

defer_init =

Undocumented

environment =

Undocumented

extends_so_far: int =

Undocumented

filename =

Undocumented

filters: t.Dict[str, str] =

Undocumented

has_known_extends: bool =

Undocumented

import_aliases: t.Dict[str, str] =

Undocumented

name =

Undocumented

optimizer =

Undocumented

stream =

Undocumented

tests: t.Dict[str, str] =

Undocumented

@property
optimized: bool =

Undocumented