Merge pull request #740 from davidism/optimize-tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Inline template tests during optimization
authored by David Lord · committed by GitHub · parents ca1abd4, c581d0e · Browse files · 1 file·+47 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+47 added−15 removed
- other:+47 added−15 removed
Provenance
- Commit
2c61df1- Tree
8f46d5e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
ca1abd4- Parent 2
c581d0e
1 file changed+47 added−15 removed
modified:jinja2/nodes.py+47 added-15 removed
597 unmodified lines598 599 return self.expr2.as_const(eval_ctx)600 601 602+def args_as_const(node, eval_ctx):603+ args = [x.as_const(eval_ctx) for x in node.args]604+ kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)605+606+ if node.dyn_args is not None:607+ try:608+ args.extend(node.dyn_args.as_const(eval_ctx))609+ except Exception:610+ raise Impossible()611+612+ if node.dyn_kwargs is not None:613+ try:614+ kwargs.update(node.dyn_kwargs.as_const(eval_ctx))615+ except Exception:616+ raise Impossible()617+618+ return args, kwargs619+620+621 class Filter(Expr):622 """This node applies a filter on an expression. `name` is the name of623 the filter, the rest of the fields are the same as for :class:`Call`.624 625 If the `node` of a filter is `None` the contents of the last buffer are626 filtered. Buffers are created by macros and filter blocks.627 """628+629 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')630 631 def as_const(self, eval_ctx=None):632 eval_ctx = get_eval_context(self, eval_ctx)633+634 if eval_ctx.volatile or self.node is None:635 raise Impossible()636+637 # we have to be careful here because we call filter_ below.638 # if this variable would be called filter, 2to3 would wrap the639 # call in a list beause it is assuming we are talking about the640 # builtin filter function here which no longer returns a list in641 # python 3. because of that, do not rename filter_ to filter!642 filter_ = self.environment.filters.get(self.name)643+644 if filter_ is None or getattr(filter_, 'contextfilter', False):645 raise Impossible()646 647 # We cannot constant handle async filters, so we need to make sure648 # to not go down this path.626- if eval_ctx.environment.is_async and \627- getattr(filter_, 'asyncfiltervariant', False):649+ if (650+ eval_ctx.environment.is_async651+ and getattr(filter_, 'asyncfiltervariant', False)652+ ):653 raise Impossible()654 630- obj = self.node.as_const(eval_ctx)631- args = [obj] + [x.as_const(eval_ctx) for x in self.args]655+ args, kwargs = args_as_const(self, eval_ctx)656+ args.insert(0, self.node.as_const(eval_ctx))657+658 if getattr(filter_, 'evalcontextfilter', False):659 args.insert(0, eval_ctx)660 elif getattr(filter_, 'environmentfilter', False):661 args.insert(0, self.environment)636- kwargs = dict(x.as_const(eval_ctx) for x in self.kwargs)637- if self.dyn_args is not None:638- try:639- args.extend(self.dyn_args.as_const(eval_ctx))640- except Exception:641- raise Impossible()642- if self.dyn_kwargs is not None:643- try:644- kwargs.update(self.dyn_kwargs.as_const(eval_ctx))645- except Exception:646- raise Impossible()662+663 try:664 return filter_(*args, **kwargs)665 except Exception:666 raise Impossible()2 unmodified lines669 class Test(Expr):670 """Applies a test on an expression. `name` is the name of the test, the671 rest of the fields are the same as for :class:`Call`.672 """673+674 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')675 676+ def as_const(self, eval_ctx=None):677+ test = self.environment.tests.get(self.name)678 679+ if test is None:680+ raise Impossible()681+682+ eval_ctx = get_eval_context(self, eval_ctx)683+ args, kwargs = args_as_const(self, eval_ctx)684+ args.insert(0, self.node.as_const(eval_ctx))685+686+ try:687+ return test(*args, **kwargs)688+ except Exception:689+ raise Impossible()690+691+692 class Call(Expr):693 """Calls an expression. `args` is a list of arguments, `kwargs` a list694 of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`695 and `dyn_kwargs` has to be either `None` or a node that is used as304 unmodified lines