1 # -*- coding: utf-8 -*-
6 This module implements additional nodes derived from the ast base node.
8 It also provides some node tree helper functions like `in_lineno` and
9 `get_nodes` used by the parser and translator in order to normalize
10 python and jinja nodes.
12 :copyright: (c) 2010 by the Jinja Team.
13 :license: BSD, see LICENSE for more details.
16 from itertools import chain, izip
17 from collections import deque
18 from jinja2.utils import Markup, MethodType, FunctionType
21 #: the types we support for context functions
22 _context_function_types = (FunctionType, MethodType)
27 '/': operator.truediv,
28 '//': operator.floordiv,
48 'in': lambda a, b: a in b,
49 'notin': lambda a, b: a not in b
53 class Impossible(Exception):
54 """Raised if the node could not perform a requested action."""
58 """A metaclass for nodes that handles the field and attribute
59 inheritance. fields and attributes from the parent class are
60 automatically forwarded to the child."""
62 def __new__(cls, name, bases, d):
63 for attr in 'fields', 'attributes':
65 storage.extend(getattr(bases[0], attr, ()))
66 storage.extend(d.get(attr, ()))
67 assert len(bases) == 1, 'multiple inheritance not allowed'
68 assert len(storage) == len(set(storage)), 'layout conflict'
69 d[attr] = tuple(storage)
70 d.setdefault('abstract', False)
71 return type.__new__(cls, name, bases, d)
74 class EvalContext(object):
75 """Holds evaluation time information. Custom attributes can be attached
79 def __init__(self, environment, template_name=None):
80 if callable(environment.autoescape):
81 self.autoescape = environment.autoescape(template_name)
83 self.autoescape = environment.autoescape
87 return self.__dict__.copy()
89 def revert(self, old):
91 self.__dict__.update(old)
94 def get_eval_context(node, ctx):
96 if node.environment is None:
97 raise RuntimeError('if no eval context is passed, the '
98 'node must have an attached '
100 return EvalContext(node.environment)
105 """Baseclass for all Jinja2 nodes. There are a number of nodes available
106 of different types. There are three major types:
108 - :class:`Stmt`: statements
109 - :class:`Expr`: expressions
110 - :class:`Helper`: helper nodes
111 - :class:`Template`: the outermost wrapper node
113 All nodes have fields and attributes. Fields may be other nodes, lists,
114 or arbitrary values. Fields are passed to the constructor as regular
115 positional arguments, attributes as keyword arguments. Each node has
116 two attributes: `lineno` (the line number of the node) and `environment`.
117 The `environment` attribute is set at the end of the parsing process for
118 all nodes automatically.
120 __metaclass__ = NodeType
122 attributes = ('lineno', 'environment')
125 def __init__(self, *fields, **attributes):
127 raise TypeError('abstract nodes are not instanciable')
129 if len(fields) != len(self.fields):
131 raise TypeError('%r takes 0 arguments' %
132 self.__class__.__name__)
133 raise TypeError('%r takes 0 or %d argument%s' % (
134 self.__class__.__name__,
136 len(self.fields) != 1 and 's' or ''
138 for name, arg in izip(self.fields, fields):
139 setattr(self, name, arg)
140 for attr in self.attributes:
141 setattr(self, attr, attributes.pop(attr, None))
143 raise TypeError('unknown attribute %r' %
144 iter(attributes).next())
146 def iter_fields(self, exclude=None, only=None):
147 """This method iterates over all fields that are defined and yields
148 ``(key, value)`` tuples. Per default all fields are returned, but
149 it's possible to limit that to some fields by providing the `only`
150 parameter or to exclude some using the `exclude` parameter. Both
151 should be sets or tuples of field names.
153 for name in self.fields:
154 if (exclude is only is None) or \
155 (exclude is not None and name not in exclude) or \
156 (only is not None and name in only):
158 yield name, getattr(self, name)
159 except AttributeError:
162 def iter_child_nodes(self, exclude=None, only=None):
163 """Iterates over all direct child nodes of the node. This iterates
164 over all fields and yields the values of they are nodes. If the value
165 of a field is a list all the nodes in that list are returned.
167 for field, item in self.iter_fields(exclude, only):
168 if isinstance(item, list):
170 if isinstance(n, Node):
172 elif isinstance(item, Node):
175 def find(self, node_type):
176 """Find the first node of a given type. If no such node exists the
177 return value is `None`.
179 for result in self.find_all(node_type):
182 def find_all(self, node_type):
183 """Find all the nodes of a given type. If the type is a tuple,
184 the check is performed for any of the tuple items.
186 for child in self.iter_child_nodes():
187 if isinstance(child, node_type):
189 for result in child.find_all(node_type):
192 def set_ctx(self, ctx):
193 """Reset the context of a node and all child nodes. Per default the
194 parser will all generate nodes that have a 'load' context as it's the
195 most common one. This method is used in the parser to set assignment
196 targets and other nodes to a store context.
200 node = todo.popleft()
201 if 'ctx' in node.fields:
203 todo.extend(node.iter_child_nodes())
206 def set_lineno(self, lineno, override=False):
207 """Set the line numbers of the node and children."""
210 node = todo.popleft()
211 if 'lineno' in node.attributes:
212 if node.lineno is None or override:
214 todo.extend(node.iter_child_nodes())
217 def set_environment(self, environment):
218 """Set the environment for all nodes."""
221 node = todo.popleft()
222 node.environment = environment
223 todo.extend(node.iter_child_nodes())
226 def __eq__(self, other):
227 return type(self) is type(other) and \
228 tuple(self.iter_fields()) == tuple(other.iter_fields())
230 def __ne__(self, other):
231 return not self.__eq__(other)
235 self.__class__.__name__,
236 ', '.join('%s=%r' % (arg, getattr(self, arg, None)) for
242 """Base node for all statements."""
247 """Nodes that exist in a specific context only."""
251 class Template(Node):
252 """Node that represents a template. This must be the outermost node that
253 is passed to the compiler.
259 """A node that holds multiple expressions which are then printed out.
260 This is used both for the `print` statement and the regular template data.
266 """Represents an extends statement."""
267 fields = ('template',)
271 """The for loop. `target` is the target for the iteration (usually a
272 :class:`Name` or :class:`Tuple`), `iter` the iterable. `body` is a list
273 of nodes that are used as loop-body, and `else_` a list of nodes for the
274 `else` block. If no else node exists it has to be an empty list.
276 For filtered nodes an expression can be stored as `test`, otherwise `None`.
278 fields = ('target', 'iter', 'body', 'else_', 'test', 'recursive')
282 """If `test` is true, `body` is rendered, else `else_`."""
283 fields = ('test', 'body', 'else_')
287 """A macro definition. `name` is the name of the macro, `args` a list of
288 arguments and `defaults` a list of defaults if there are any. `body` is
289 a list of nodes for the macro body.
291 fields = ('name', 'args', 'defaults', 'body')
294 class CallBlock(Stmt):
295 """Like a macro without a name but a call instead. `call` is called with
296 the unnamed macro as `caller` argument this node holds.
298 fields = ('call', 'args', 'defaults', 'body')
301 class FilterBlock(Stmt):
302 """Node for filter sections."""
303 fields = ('body', 'filter')
307 """A node that represents a block."""
308 fields = ('name', 'body', 'scoped')
312 """A node that represents the include tag."""
313 fields = ('template', 'with_context', 'ignore_missing')
317 """A node that represents the import tag."""
318 fields = ('template', 'target', 'with_context')
321 class FromImport(Stmt):
322 """A node that represents the from import tag. It's important to not
323 pass unsafe names to the name attribute. The compiler translates the
324 attribute lookups directly into getattr calls and does *not* use the
325 subscript callback of the interface. As exported variables may not
326 start with double underscores (which the parser asserts) this is not a
327 problem for regular Jinja code, but if this node is used in an extension
328 extra care must be taken.
330 The list of names may contain tuples if aliases are wanted.
332 fields = ('template', 'names', 'with_context')
335 class ExprStmt(Stmt):
336 """A statement that evaluates an expression and discards the result."""
341 """Assigns an expression to a target."""
342 fields = ('target', 'node')
346 """Baseclass for all expressions."""
349 def as_const(self, eval_ctx=None):
350 """Return the value of the expression as constant or raise
351 :exc:`Impossible` if this was not possible.
353 An :class:`EvalContext` can be provided, if none is given
354 a default context is created which requires the nodes to have
355 an attached environment.
357 .. versionchanged:: 2.4
358 the `eval_ctx` parameter was added.
362 def can_assign(self):
363 """Check if it's possible to assign something to this node."""
368 """Baseclass for all binary expressions."""
369 fields = ('left', 'right')
373 def as_const(self, eval_ctx=None):
374 eval_ctx = get_eval_context(self, eval_ctx)
375 f = _binop_to_func[self.operator]
377 return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
382 class UnaryExpr(Expr):
383 """Baseclass for all unary expressions."""
388 def as_const(self, eval_ctx=None):
389 eval_ctx = get_eval_context(self, eval_ctx)
390 f = _uaop_to_func[self.operator]
392 return f(self.node.as_const(eval_ctx))
398 """Looks up a name or stores a value in a name.
399 The `ctx` of the node can be one of the following values:
401 - `store`: store a value in the name
402 - `load`: load that name
403 - `param`: like `store` but if the name was defined as function parameter.
405 fields = ('name', 'ctx')
407 def can_assign(self):
408 return self.name not in ('true', 'false', 'none',
409 'True', 'False', 'None')
413 """Baseclass for literals."""
417 class Const(Literal):
418 """All constant values. The parser will return this node for simple
419 constants such as ``42`` or ``"foo"`` but it can be used to store more
420 complex values such as lists too. Only constants with a safe
421 representation (objects where ``eval(repr(x)) == x`` is true).
425 def as_const(self, eval_ctx=None):
429 def from_untrusted(cls, value, lineno=None, environment=None):
430 """Return a const object if the value is representable as
431 constant value in the generated code, otherwise it will raise
432 an `Impossible` exception.
434 from compiler import has_safe_repr
435 if not has_safe_repr(value):
437 return cls(value, lineno=lineno, environment=environment)
440 class TemplateData(Literal):
441 """A constant template string."""
444 def as_const(self, eval_ctx=None):
445 eval_ctx = get_eval_context(self, eval_ctx)
446 if eval_ctx.volatile:
448 if eval_ctx.autoescape:
449 return Markup(self.data)
453 class Tuple(Literal):
454 """For loop unpacking and some other things like multiple arguments
455 for subscripts. Like for :class:`Name` `ctx` specifies if the tuple
456 is used for loading the names or storing.
458 fields = ('items', 'ctx')
460 def as_const(self, eval_ctx=None):
461 eval_ctx = get_eval_context(self, eval_ctx)
462 return tuple(x.as_const(eval_ctx) for x in self.items)
464 def can_assign(self):
465 for item in self.items:
466 if not item.can_assign():
472 """Any list literal such as ``[1, 2, 3]``"""
475 def as_const(self, eval_ctx=None):
476 eval_ctx = get_eval_context(self, eval_ctx)
477 return [x.as_const(eval_ctx) for x in self.items]
481 """Any dict literal such as ``{1: 2, 3: 4}``. The items must be a list of
486 def as_const(self, eval_ctx=None):
487 eval_ctx = get_eval_context(self, eval_ctx)
488 return dict(x.as_const(eval_ctx) for x in self.items)
492 """A key, value pair for dicts."""
493 fields = ('key', 'value')
495 def as_const(self, eval_ctx=None):
496 eval_ctx = get_eval_context(self, eval_ctx)
497 return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)
500 class Keyword(Helper):
501 """A key, value pair for keyword arguments where key is a string."""
502 fields = ('key', 'value')
504 def as_const(self, eval_ctx=None):
505 eval_ctx = get_eval_context(self, eval_ctx)
506 return self.key, self.value.as_const(eval_ctx)
509 class CondExpr(Expr):
510 """A conditional expression (inline if expression). (``{{
511 foo if bar else baz }}``)
513 fields = ('test', 'expr1', 'expr2')
515 def as_const(self, eval_ctx=None):
516 eval_ctx = get_eval_context(self, eval_ctx)
517 if self.test.as_const(eval_ctx):
518 return self.expr1.as_const(eval_ctx)
520 # if we evaluate to an undefined object, we better do that at runtime
521 if self.expr2 is None:
524 return self.expr2.as_const(eval_ctx)
528 """This node applies a filter on an expression. `name` is the name of
529 the filter, the rest of the fields are the same as for :class:`Call`.
531 If the `node` of a filter is `None` the contents of the last buffer are
532 filtered. Buffers are created by macros and filter blocks.
534 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
536 def as_const(self, eval_ctx=None):
537 eval_ctx = get_eval_context(self, eval_ctx)
538 if eval_ctx.volatile or self.node is None:
540 # we have to be careful here because we call filter_ below.
541 # if this variable would be called filter, 2to3 would wrap the
542 # call in a list beause it is assuming we are talking about the
543 # builtin filter function here which no longer returns a list in
544 # python 3. because of that, do not rename filter_ to filter!
545 filter_ = self.environment.filters.get(self.name)
546 if filter_ is None or getattr(filter_, 'contextfilter', False):
548 obj = self.node.as_const(eval_ctx)
549 args = [x.as_const(eval_ctx) for x in self.args]
550 if getattr(filter_, 'evalcontextfilter', False):
551 args.insert(0, eval_ctx)
552 elif getattr(filter_, 'environmentfilter', False):
553 args.insert(0, self.environment)
554 kwargs = dict(x.as_const(eval_ctx) for x in self.kwargs)
555 if self.dyn_args is not None:
557 args.extend(self.dyn_args.as_const(eval_ctx))
560 if self.dyn_kwargs is not None:
562 kwargs.update(self.dyn_kwargs.as_const(eval_ctx))
566 return filter_(obj, *args, **kwargs)
572 """Applies a test on an expression. `name` is the name of the test, the
573 rest of the fields are the same as for :class:`Call`.
575 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
579 """Calls an expression. `args` is a list of arguments, `kwargs` a list
580 of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`
581 and `dyn_kwargs` has to be either `None` or a node that is used as
582 node for dynamic positional (``*args``) or keyword (``**kwargs``)
585 fields = ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
587 def as_const(self, eval_ctx=None):
588 eval_ctx = get_eval_context(self, eval_ctx)
589 if eval_ctx.volatile:
591 obj = self.node.as_const(eval_ctx)
593 # don't evaluate context functions
594 args = [x.as_const(eval_ctx) for x in self.args]
595 if isinstance(obj, _context_function_types):
596 if getattr(obj, 'contextfunction', False):
598 elif getattr(obj, 'evalcontextfunction', False):
599 args.insert(0, eval_ctx)
600 elif getattr(obj, 'environmentfunction', False):
601 args.insert(0, self.environment)
603 kwargs = dict(x.as_const(eval_ctx) for x in self.kwargs)
604 if self.dyn_args is not None:
606 args.extend(self.dyn_args.as_const(eval_ctx))
609 if self.dyn_kwargs is not None:
611 kwargs.update(self.dyn_kwargs.as_const(eval_ctx))
615 return obj(*args, **kwargs)
621 """Get an attribute or item from an expression and prefer the item."""
622 fields = ('node', 'arg', 'ctx')
624 def as_const(self, eval_ctx=None):
625 eval_ctx = get_eval_context(self, eval_ctx)
626 if self.ctx != 'load':
629 return self.environment.getitem(self.node.as_const(eval_ctx),
630 self.arg.as_const(eval_ctx))
634 def can_assign(self):
639 """Get an attribute or item from an expression that is a ascii-only
640 bytestring and prefer the attribute.
642 fields = ('node', 'attr', 'ctx')
644 def as_const(self, eval_ctx=None):
645 if self.ctx != 'load':
648 eval_ctx = get_eval_context(self, eval_ctx)
649 return self.environment.getattr(self.node.as_const(eval_ctx),
654 def can_assign(self):
659 """Represents a slice object. This must only be used as argument for
662 fields = ('start', 'stop', 'step')
664 def as_const(self, eval_ctx=None):
665 eval_ctx = get_eval_context(self, eval_ctx)
669 return obj.as_const(eval_ctx)
670 return slice(const(self.start), const(self.stop), const(self.step))
674 """Concatenates the list of expressions provided after converting them to
679 def as_const(self, eval_ctx=None):
680 eval_ctx = get_eval_context(self, eval_ctx)
681 return ''.join(unicode(x.as_const(eval_ctx)) for x in self.nodes)
685 """Compares an expression with some other expressions. `ops` must be a
686 list of :class:`Operand`\s.
688 fields = ('expr', 'ops')
690 def as_const(self, eval_ctx=None):
691 eval_ctx = get_eval_context(self, eval_ctx)
692 result = value = self.expr.as_const(eval_ctx)
695 new_value = op.expr.as_const(eval_ctx)
696 result = _cmpop_to_func[op.op](value, new_value)
703 class Operand(Helper):
704 """Holds an operator and an expression."""
705 fields = ('op', 'expr')
708 Operand.__doc__ += '\nThe following operators are available: ' + \
709 ', '.join(sorted('``%s``' % x for x in set(_binop_to_func) |
710 set(_uaop_to_func) | set(_cmpop_to_func)))
714 """Multiplies the left with the right node."""
719 """Divides the left by the right node."""
723 class FloorDiv(BinExpr):
724 """Divides the left by the right node and truncates conver the
725 result into an integer by truncating.
731 """Add the left to the right node."""
736 """Substract the right from the left node."""
741 """Left modulo right."""
746 """Left to the power of right."""
751 """Short circuited AND."""
754 def as_const(self, eval_ctx=None):
755 eval_ctx = get_eval_context(self, eval_ctx)
756 return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)
760 """Short circuited OR."""
763 def as_const(self, eval_ctx=None):
764 eval_ctx = get_eval_context(self, eval_ctx)
765 return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)
768 class Not(UnaryExpr):
769 """Negate the expression."""
773 class Neg(UnaryExpr):
774 """Make the expression negative."""
778 class Pos(UnaryExpr):
779 """Make the expression positive (noop for most expressions)"""
783 # Helpers for extensions
786 class EnvironmentAttribute(Expr):
787 """Loads an attribute from the environment object. This is useful for
788 extensions that want to call a callback stored on the environment.
793 class ExtensionAttribute(Expr):
794 """Returns the attribute of an extension bound to the environment.
795 The identifier is the identifier of the :class:`Extension`.
797 This node is usually constructed by calling the
798 :meth:`~jinja2.ext.Extension.attr` method on an extension.
800 fields = ('identifier', 'name')
803 class ImportedName(Expr):
804 """If created with an import name the import name is returned on node
805 access. For example ``ImportedName('cgi.escape')`` returns the `escape`
806 function from the cgi module on evaluation. Imports are optimized by the
807 compiler so there is no need to assign them to local variables.
809 fields = ('importname',)
812 class InternalName(Expr):
813 """An internal name in the compiler. You cannot create these nodes
814 yourself but the parser provides a
815 :meth:`~jinja2.parser.Parser.free_identifier` method that creates
816 a new identifier for you. This identifier is not available from the
817 template and is not threated specially by the compiler.
822 raise TypeError('Can\'t create internal names. Use the '
823 '`free_identifier` method on a parser.')
826 class MarkSafe(Expr):
827 """Mark the wrapped expression as safe (wrap it as `Markup`)."""
830 def as_const(self, eval_ctx=None):
831 eval_ctx = get_eval_context(self, eval_ctx)
832 return Markup(self.expr.as_const(eval_ctx))
835 class MarkSafeIfAutoescape(Expr):
836 """Mark the wrapped expression as safe (wrap it as `Markup`) but
837 only if autoescaping is active.
839 .. versionadded:: 2.5
843 def as_const(self, eval_ctx=None):
844 eval_ctx = get_eval_context(self, eval_ctx)
845 if eval_ctx.volatile:
847 expr = self.expr.as_const(eval_ctx)
848 if eval_ctx.autoescape:
853 class ContextReference(Expr):
854 """Returns the current template context. It can be used like a
855 :class:`Name` node, with a ``'load'`` ctx and will return the
856 current :class:`~jinja2.runtime.Context` object.
858 Here an example that assigns the current template name to a
859 variable named `foo`::
861 Assign(Name('foo', ctx='store'),
862 Getattr(ContextReference(), 'name'))
866 class Continue(Stmt):
867 """Continue a loop."""
875 """An artificial scope."""
879 class EvalContextModifier(Stmt):
880 """Modifies the eval context. For each option that should be modified,
881 a :class:`Keyword` has to be added to the :attr:`options` list.
883 Example to change the `autoescape` setting::
885 EvalContextModifier(options=[Keyword('autoescape', Const(True))])
887 fields = ('options',)
890 class ScopedEvalContextModifier(EvalContextModifier):
891 """Modifies the eval context and reverts it later. Works exactly like
892 :class:`EvalContextModifier` but will only modify the
893 :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.
898 # make sure nobody creates custom nodes
899 def _failing_new(*args, **kwargs):
900 raise TypeError('can\'t create custom node types')
901 NodeType.__new__ = staticmethod(_failing_new); del _failing_new