Skip to content
Myr

WIP for overlay scopes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 95fb2bf · Browse files · 4 files+61 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+61 added−5 removed

  • other:+61 added−5 removed

4 files+61 added−5 removed

modified:jinja2/compiler.py+45 added-4 removed
300 unmodified lines
301302 # Tracks parameter definition blocks303 self._param_def_block = []304305 # Tracks the current context.306 self._context_reference_stack = ['context']307308 # -- Various compilation helpers309310 def fail(self, msg, lineno):311 """Fail with a :exc:`TemplateAssertionError`."""
163 unmodified lines
475 for target, (action, param) in iteritems(frame.symbols.loads):476 if action == VAR_LOAD_PARAMETER:477 pass478 elif action == VAR_LOAD_RESOLVE:476 self.writeline('%s = resolve(%r)' %477 (target, param))479 self.writeline('%s = %s(%r)' %480 (target, self.get_resolve_func(), param))481 elif action == VAR_LOAD_ALIAS:482 self.writeline('%s = %s' % (target, param))483 elif action == VAR_LOAD_UNDEFINED:484 undefs.append(target)
143 unmodified lines
628 """629 if self._param_def_block:630 self._param_def_block[-1].discard(target)631632 def push_context_reference(self, target):633 self._context_reference_stack.append(target)634635 def pop_context_reference(self):636 self._context_reference_stack.pop()637638 def get_context_ref(self):639 return self._context_reference_stack[-1]640641 def get_resolve_func(self):642 target = self._context_reference_stack[-1]643 if target == 'context':644 return 'resolve'645 return '%s.resolve' % target646647 def derive_context(self, frame):648 return '%s.derived(%s)' % (649 self.get_context_ref(),650 self.dump_local_context(frame),651 )652653 def parameter_is_undeclared(self, target):654 """Checks if a given target is an undeclared parameter."""655 if not self._param_def_block:656 return False
159 unmodified lines
816 if self.extends_so_far > 0:817 self.writeline('if parent_template is None:')818 self.indent()819 level += 1796 context = node.scoped and (797 'context.derived(%s)' % self.dump_local_context(frame)) or 'context'820821 if node.scoped:822 context = self.derive_context(frame)823 else:824 context = self.get_context_ref()825826 if supports_yield_from and not self.environment.is_async and \827 frame.buffer is None:828 self.writeline('yield from context.blocks[%r][0](%s)' % (829 node.name, context), node)
815 unmodified lines
1645 self.enter_frame(scope_frame)1646 self.blockvisit(node.body, scope_frame)1647 self.leave_frame(scope_frame)16481649 def visit_OverlayScope(self, node, frame):1650 ctx = self.temporary_identifier()1651 self.writeline('%s = %s' % (ctx, self.derive_context(frame)))1652 self.writeline('%s.vars = ' % ctx)1653 self.visit(node.context, frame)1654 self.push_context_reference(ctx)16551656 scope_frame = Frame(frame.eval_ctx)1657 scope_frame.symbols.analyze_node(node)1658 self.enter_frame(scope_frame)1659 self.blockvisit(node.body, scope_frame)1660 self.leave_frame(scope_frame)1661 self.pop_context_reference()16621663 def visit_EvalContextModifier(self, node, frame):1664 for keyword in node.options:1665 self.writeline('context.eval_ctx.%s = ' % keyword.key)1666 self.visit(keyword.value, frame)
16 unmodified lines
modified:jinja2/idtracking.py+7 added
165 unmodified lines
166 def visit_CallBlock(self, node, **kwargs):167 for child in node.iter_child_nodes(exclude=('call',)):168 self.sym_visitor.visit(child)169170 def visit_OverlayScope(self, node, **kwargs):171 for child in node.body:172 self.sym_visitor.visit(child)173174 def visit_For(self, node, for_branch='body', **kwargs):175 if node.test is not None:176 self.sym_visitor.visit(node.test)177 if for_branch == 'body':
83 unmodified lines
261 """Stop visiting at scopes."""262263 def visit_Block(self, node, **kwargs):264 """Stop visiting at blocks."""265266 def visit_OverlayScope(self, node, **kwargs):267 """Do not visit into overlay scopes."""
modified:jinja2/nodes.py+8 added
903 unmodified lines
904 """An artificial scope."""905 fields = ('body',)906907908class OverlayScope(Stmt):909 """An overlay scope for extensions. This is a largely unoptimized scope910 that however can be used to introduce completely arbitrar variables into911 a sub scope from a dictionary or dictionary like object.912 """913 fields = ('context', 'body')914915916class EvalContextModifier(Stmt):917 """Modifies the eval context. For each option that should be modified,918 a :class:`Keyword` has to be added to the :attr:`options` list.919
19 unmodified lines
modified:jinja2/runtime.py+1 added-1 removed
22 unmodified lines
23# these variables are exported to the template runtime24__all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',25 'TemplateRuntimeError', 'missing', 'concat', 'escape',26 'markup_join', 'unicode_join', 'to_string', 'identity',27 'TemplateNotFound']27 'make_overlay_resolve', 'TemplateNotFound']2829#: the name of the function that is used to convert something into30#: a string. We can just use the text type here.31to_string = text_type
702 unmodified lines