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

Method _make_finalize

src/jinja2/compiler.py:1400–1446  ·  view source on GitHub ↗

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``

(self)

Source from the content-addressed store, hash-verified

1398 _finalize: t.Optional[_FinalizeInfo] = None
1399
1400 def _make_finalize(self) -> _FinalizeInfo:
1401 """Build the finalize function to be used on constants and at
1402 runtime. Cached so it's only created once for all output nodes.
1403
1404 Returns a ``namedtuple`` with the following attributes:
1405
1406 ``const``
1407 A function to finalize constant data at compile time.
1408
1409 ``src``
1410 Source code to output around nodes to be evaluated at
1411 runtime.
1412 """
1413 if self._finalize is not None:
1414 return self._finalize
1415
1416 finalize: t.Optional[t.Callable[..., t.Any]]
1417 finalize = default = self._default_finalize
1418 src = None
1419
1420 if self.environment.finalize:
1421 src = "environment.finalize("
1422 env_finalize = self.environment.finalize
1423 pass_arg = {
1424 _PassArg.context: "context",
1425 _PassArg.eval_context: "context.eval_ctx",
1426 _PassArg.environment: "environment",
1427 }.get(
1428 _PassArg.from_obj(env_finalize) # type: ignore
1429 )
1430 finalize = None
1431
1432 if pass_arg is None:
1433
1434 def finalize(value: t.Any) -> t.Any: # noqa: F811
1435 return default(env_finalize(value))
1436
1437 else:
1438 src = f"{src}{pass_arg}, "
1439
1440 if pass_arg == "environment":
1441
1442 def finalize(value: t.Any) -> t.Any: # noqa: F811
1443 return default(env_finalize(self.environment, value))
1444
1445 self._finalize = self._FinalizeInfo(finalize, src)
1446 return self._finalize
1447
1448 def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:
1449 """Given a group of constant values converted from ``Output``

Callers 1

visit_OutputMethod · 0.95

Calls 2

from_objMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected