MCPcopy
hub / github.com/pallets/jinja / _output_child_to_const

Method _output_child_to_const

src/jinja2/compiler.py:1455–1474  ·  view source on GitHub ↗

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 eas

(
        self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo
    )

Source from the content-addressed store, hash-verified

1453 return repr(concat(group))
1454
1455 def _output_child_to_const(
1456 self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo
1457 ) -> str:
1458 """Try to optimize a child of an ``Output`` node by trying to
1459 convert it to constant, finalized data at compile time.
1460
1461 If :exc:`Impossible` is raised, the node is not constant and
1462 will be evaluated at runtime. Any other exception will also be
1463 evaluated at runtime for easier debugging.
1464 """
1465 const = node.as_const(frame.eval_ctx)
1466
1467 if frame.eval_ctx.autoescape:
1468 const = escape(const)
1469
1470 # Template data doesn't go through finalize.
1471 if isinstance(node, nodes.TemplateData):
1472 return str(const)
1473
1474 return finalize.const(const) # type: ignore
1475
1476 def _output_child_pre(
1477 self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo

Callers 1

visit_OutputMethod · 0.95

Calls 2

constMethod · 0.80
as_constMethod · 0.45

Tested by

no test coverage detected