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

Method _get_default_module

src/jinja2/environment.py:1426–1450  ·  view source on GitHub ↗

If a context is passed in, this means that the template was imported. Imported templates have access to the current template's globals by default, but they can only be accessed via the context during runtime. If there are new globals, we need to create a new module b

(self, ctx: t.Optional[Context] = None)

Source from the content-addressed store, hash-verified

1424
1425 @internalcode
1426 def _get_default_module(self, ctx: t.Optional[Context] = None) -> "TemplateModule":
1427 """If a context is passed in, this means that the template was
1428 imported. Imported templates have access to the current
1429 template's globals by default, but they can only be accessed via
1430 the context during runtime.
1431
1432 If there are new globals, we need to create a new module because
1433 the cached module is already rendered and will not have access
1434 to globals from the current context. This new module is not
1435 cached because the template can be imported elsewhere, and it
1436 should have access to only the current template's globals.
1437 """
1438 if self.environment.is_async:
1439 raise RuntimeError("Module is not available in async mode.")
1440
1441 if ctx is not None:
1442 keys = ctx.globals_keys - self.globals.keys()
1443
1444 if keys:
1445 return self.make_module({k: ctx.parent[k] for k in keys})
1446
1447 if self._module is None:
1448 self._module = self.make_module()
1449
1450 return self._module
1451
1452 async def _get_default_module_async(
1453 self, ctx: t.Optional[Context] = None

Callers 1

moduleMethod · 0.95

Calls 2

make_moduleMethod · 0.95
keysMethod · 0.80

Tested by

no test coverage detected