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
Provenance
- Commit
75176bd- Tree
b0be57c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
95fb2bf
4 files changed+61 added−5 removed
modified:jinja2/compiler.py+45 added-4 removed
300 unmodified lines301 302 # Tracks parameter definition blocks303 self._param_def_block = []304 305+ # Tracks the current context.306+ self._context_reference_stack = ['context']307+308 # -- Various compilation helpers309 310 def fail(self, msg, lineno):311 """Fail with a :exc:`TemplateAssertionError`."""163 unmodified lines475 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 lines628 """629 if self._param_def_block:630 self._param_def_block[-1].discard(target)631 632+ def push_context_reference(self, target):633+ self._context_reference_stack.append(target)634+635+ def pop_context_reference(self):636+ self._context_reference_stack.pop()637+638+ def get_context_ref(self):639+ return self._context_reference_stack[-1]640+641+ def get_resolve_func(self):642+ target = self._context_reference_stack[-1]643+ if target == 'context':644+ return 'resolve'645+ return '%s.resolve' % target646+647+ def derive_context(self, frame):648+ return '%s.derived(%s)' % (649+ self.get_context_ref(),650+ self.dump_local_context(frame),651+ )652+653 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 False159 unmodified lines816 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'820 821+ if node.scoped:822+ context = self.derive_context(frame)823+ else:824+ context = self.get_context_ref()825+826 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 lines1645 self.enter_frame(scope_frame)1646 self.blockvisit(node.body, scope_frame)1647 self.leave_frame(scope_frame)1648 1649+ 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)1655+1656+ 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()1662+1663 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 linesmodified:jinja2/idtracking.py+7 added
165 unmodified lines166 def visit_CallBlock(self, node, **kwargs):167 for child in node.iter_child_nodes(exclude=('call',)):168 self.sym_visitor.visit(child)169 170+ def visit_OverlayScope(self, node, **kwargs):171+ for child in node.body:172+ self.sym_visitor.visit(child)173+174 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 lines261 """Stop visiting at scopes."""262 263 def visit_Block(self, node, **kwargs):264 """Stop visiting at blocks."""265+266+ def visit_OverlayScope(self, node, **kwargs):267+ """Do not visit into overlay scopes."""modified:jinja2/nodes.py+8 added
903 unmodified lines904 """An artificial scope."""905 fields = ('body',)906 907 908+class 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')914+915+916 class 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 linesmodified:jinja2/runtime.py+1 added-1 removed
22 unmodified lines23 # 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']28 29 #: the name of the function that is used to convert something into30 #: a string. We can just use the text type here.31 to_string = text_type702 unmodified lines