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.
17 from collections
import deque
18 from jinja2
.utils
import Markup
19 from jinja2
._compat
import next
, izip
, with_metaclass
, text_type
, \
20 method_type
, function_type
23 #: the types we support for context functions
24 _context_function_types
= (function_type
, method_type
)
29 '/': operator
.truediv
,
30 '//': operator
.floordiv
,
50 'in': lambda a
, b
: a
in b
,
51 'notin': lambda a
, b
: a
not in b
55 class Impossible(Exception):
56 """Raised if the node could not perform a requested action."""
60 """A metaclass for nodes that handles the field and attribute
61 inheritance. fields and attributes from the parent class are
62 automatically forwarded to the child."""
64 def __new__(cls
, name
, bases
, d
):
65 for attr
in 'fields', 'attributes':
67 storage
.extend(getattr(bases
[0], attr
, ()))
68 storage
.extend(d
.get(attr
, ()))
69 assert len(bases
) == 1, 'multiple inheritance not allowed'
70 assert len(storage
) == len(set(storage
)), 'layout conflict'
71 d
[attr
] = tuple(storage
)
72 d
.setdefault('abstract', False)
73 return type.__new
__(cls
, name
, bases
, d
)
76 class EvalContext(object):
77 """Holds evaluation time information. Custom attributes can be attached
81 def __init__(self
, environment
, template_name
=None):
82 self
.environment
= environment
83 if callable(environment
.autoescape
):
84 self
.autoescape
= environment
.autoescape(template_name
)
86 self
.autoescape
= environment
.autoescape
90 return self
.__dict
__.copy()
92 def revert(self
, old
):
94 self
.__dict
__.update(old
)
97 def get_eval_context(node
, ctx
):
99 if node
.environment
is None:
100 raise RuntimeError('if no eval context is passed, the '
101 'node must have an attached '
103 return EvalContext(node
.environment
)
107 class Node(with_metaclass(NodeType
, object)):
108 """Baseclass for all Jinja2 nodes. There are a number of nodes available
109 of different types. There are four major types:
111 - :class:`Stmt`: statements
112 - :class:`Expr`: expressions
113 - :class:`Helper`: helper nodes
114 - :class:`Template`: the outermost wrapper node
116 All nodes have fields and attributes. Fields may be other nodes, lists,
117 or arbitrary values. Fields are passed to the constructor as regular
118 positional arguments, attributes as keyword arguments. Each node has
119 two attributes: `lineno` (the line number of the node) and `environment`.
120 The `environment` attribute is set at the end of the parsing process for
121 all nodes automatically.
124 attributes
= ('lineno', 'environment')
127 def __init__(self
, *fields
, **attributes
):
129 raise TypeError('abstract nodes are not instanciable')
131 if len(fields
) != len(self
.fields
):
133 raise TypeError('%r takes 0 arguments' %
134 self
.__class
__.__name
__)
135 raise TypeError('%r takes 0 or %d argument%s' % (
136 self
.__class
__.__name
__,
138 len(self
.fields
) != 1 and 's' or ''
140 for name
, arg
in izip(self
.fields
, fields
):
141 setattr(self
, name
, arg
)
142 for attr
in self
.attributes
:
143 setattr(self
, attr
, attributes
.pop(attr
, None))
145 raise TypeError('unknown attribute %r' %
146 next(iter(attributes
)))
148 def iter_fields(self
, exclude
=None, only
=None):
149 """This method iterates over all fields that are defined and yields
150 ``(key, value)`` tuples. Per default all fields are returned, but
151 it's possible to limit that to some fields by providing the `only`
152 parameter or to exclude some using the `exclude` parameter. Both
153 should be sets or tuples of field names.
155 for name
in self
.fields
:
156 if (exclude
is only
is None) or \
157 (exclude
is not None and name
not in exclude
) or \
158 (only
is not None and name
in only
):
160 yield name
, getattr(self
, name
)
161 except AttributeError:
164 def iter_child_nodes(self
, exclude
=None, only
=None):
165 """Iterates over all direct child nodes of the node. This iterates
166 over all fields and yields the values of they are nodes. If the value
167 of a field is a list all the nodes in that list are returned.
169 for field
, item
in self
.iter_fields(exclude
, only
):
170 if isinstance(item
, list):
172 if isinstance(n
, Node
):
174 elif isinstance(item
, Node
):
177 def find(self
, node_type
):
178 """Find the first node of a given type. If no such node exists the
179 return value is `None`.
181 for result
in self
.find_all(node_type
):
184 def find_all(self
, node_type
):
185 """Find all the nodes of a given type. If the type is a tuple,
186 the check is performed for any of the tuple items.
188 for child
in self
.iter_child_nodes():
189 if isinstance(child
, node_type
):
191 for result
in child
.find_all(node_type
):
194 def set_ctx(self
, ctx
):
195 """Reset the context of a node and all child nodes. Per default the
196 parser will all generate nodes that have a 'load' context as it's the
197 most common one. This method is used in the parser to set assignment
198 targets and other nodes to a store context.
202 node
= todo
.popleft()
203 if 'ctx' in node
.fields
:
205 todo
.extend(node
.iter_child_nodes())
208 def set_lineno(self
, lineno
, override
=False):
209 """Set the line numbers of the node and children."""
212 node
= todo
.popleft()
213 if 'lineno' in node
.attributes
:
214 if node
.lineno
is None or override
:
216 todo
.extend(node
.iter_child_nodes())
219 def set_environment(self
, environment
):
220 """Set the environment for all nodes."""
223 node
= todo
.popleft()
224 node
.environment
= environment
225 todo
.extend(node
.iter_child_nodes())
228 def __eq__(self
, other
):
229 return type(self
) is type(other
) and \
230 tuple(self
.iter_fields()) == tuple(other
.iter_fields())
232 def __ne__(self
, other
):
233 return not self
.__eq
__(other
)
235 # Restore Python 2 hashing behavior on Python 3
236 __hash__
= object.__hash
__
240 self
.__class
__.__name
__,
241 ', '.join('%s=%r' % (arg
, getattr(self
, arg
, None)) for
247 """Base node for all statements."""
252 """Nodes that exist in a specific context only."""
256 class Template(Node
):
257 """Node that represents a template. This must be the outermost node that
258 is passed to the compiler.
264 """A node that holds multiple expressions which are then printed out.
265 This is used both for the `print` statement and the regular template data.
271 """Represents an extends statement."""
272 fields
= ('template',)
276 """The for loop. `target` is the target for the iteration (usually a
277 :class:`Name` or :class:`Tuple`), `iter` the iterable. `body` is a list
278 of nodes that are used as loop-body, and `else_` a list of nodes for the
279 `else` block. If no else node exists it has to be an empty list.
281 For filtered nodes an expression can be stored as `test`, otherwise `None`.
283 fields
= ('target', 'iter', 'body', 'else_', 'test', 'recursive')
287 """If `test` is true, `body` is rendered, else `else_`."""
288 fields
= ('test', 'body', 'else_')
292 """A macro definition. `name` is the name of the macro, `args` a list of
293 arguments and `defaults` a list of defaults if there are any. `body` is
294 a list of nodes for the macro body.
296 fields
= ('name', 'args', 'defaults', 'body')
299 class CallBlock(Stmt
):
300 """Like a macro without a name but a call instead. `call` is called with
301 the unnamed macro as `caller` argument this node holds.
303 fields
= ('call', 'args', 'defaults', 'body')
306 class FilterBlock(Stmt
):
307 """Node for filter sections."""
308 fields
= ('body', 'filter')
312 """A node that represents a block."""
313 fields
= ('name', 'body', 'scoped')
317 """A node that represents the include tag."""
318 fields
= ('template', 'with_context', 'ignore_missing')
322 """A node that represents the import tag."""
323 fields
= ('template', 'target', 'with_context')
326 class FromImport(Stmt
):
327 """A node that represents the from import tag. It's important to not
328 pass unsafe names to the name attribute. The compiler translates the
329 attribute lookups directly into getattr calls and does *not* use the
330 subscript callback of the interface. As exported variables may not
331 start with double underscores (which the parser asserts) this is not a
332 problem for regular Jinja code, but if this node is used in an extension
333 extra care must be taken.
335 The list of names may contain tuples if aliases are wanted.
337 fields
= ('template', 'names', 'with_context')
340 class ExprStmt(Stmt
):
341 """A statement that evaluates an expression and discards the result."""
346 """Assigns an expression to a target."""
347 fields
= ('target', 'node')
351 """Baseclass for all expressions."""
354 def as_const(self
, eval_ctx
=None):
355 """Return the value of the expression as constant or raise
356 :exc:`Impossible` if this was not possible.
358 An :class:`EvalContext` can be provided, if none is given
359 a default context is created which requires the nodes to have
360 an attached environment.
362 .. versionchanged:: 2.4
363 the `eval_ctx` parameter was added.
367 def can_assign(self
):
368 """Check if it's possible to assign something to this node."""
373 """Baseclass for all binary expressions."""
374 fields
= ('left', 'right')
378 def as_const(self
, eval_ctx
=None):
379 eval_ctx
= get_eval_context(self
, eval_ctx
)
380 # intercepted operators cannot be folded at compile time
381 if self
.environment
.sandboxed
and \
382 self
.operator
in self
.environment
.intercepted_binops
:
384 f
= _binop_to_func
[self
.operator
]
386 return f(self
.left
.as_const(eval_ctx
), self
.right
.as_const(eval_ctx
))
391 class UnaryExpr(Expr
):
392 """Baseclass for all unary expressions."""
397 def as_const(self
, eval_ctx
=None):
398 eval_ctx
= get_eval_context(self
, eval_ctx
)
399 # intercepted operators cannot be folded at compile time
400 if self
.environment
.sandboxed
and \
401 self
.operator
in self
.environment
.intercepted_unops
:
403 f
= _uaop_to_func
[self
.operator
]
405 return f(self
.node
.as_const(eval_ctx
))
411 """Looks up a name or stores a value in a name.
412 The `ctx` of the node can be one of the following values:
414 - `store`: store a value in the name
415 - `load`: load that name
416 - `param`: like `store` but if the name was defined as function parameter.
418 fields
= ('name', 'ctx')
420 def can_assign(self
):
421 return self
.name
not in ('true', 'false', 'none',
422 'True', 'False', 'None')
426 """Baseclass for literals."""
430 class Const(Literal
):
431 """All constant values. The parser will return this node for simple
432 constants such as ``42`` or ``"foo"`` but it can be used to store more
433 complex values such as lists too. Only constants with a safe
434 representation (objects where ``eval(repr(x)) == x`` is true).
438 def as_const(self
, eval_ctx
=None):
442 def from_untrusted(cls
, value
, lineno
=None, environment
=None):
443 """Return a const object if the value is representable as
444 constant value in the generated code, otherwise it will raise
445 an `Impossible` exception.
447 from .compiler
import has_safe_repr
448 if not has_safe_repr(value
):
450 return cls(value
, lineno
=lineno
, environment
=environment
)
453 class TemplateData(Literal
):
454 """A constant template string."""
457 def as_const(self
, eval_ctx
=None):
458 eval_ctx
= get_eval_context(self
, eval_ctx
)
459 if eval_ctx
.volatile
:
461 if eval_ctx
.autoescape
:
462 return Markup(self
.data
)
466 class Tuple(Literal
):
467 """For loop unpacking and some other things like multiple arguments
468 for subscripts. Like for :class:`Name` `ctx` specifies if the tuple
469 is used for loading the names or storing.
471 fields
= ('items', 'ctx')
473 def as_const(self
, eval_ctx
=None):
474 eval_ctx
= get_eval_context(self
, eval_ctx
)
475 return tuple(x
.as_const(eval_ctx
) for x
in self
.items
)
477 def can_assign(self
):
478 for item
in self
.items
:
479 if not item
.can_assign():
485 """Any list literal such as ``[1, 2, 3]``"""
488 def as_const(self
, eval_ctx
=None):
489 eval_ctx
= get_eval_context(self
, eval_ctx
)
490 return [x
.as_const(eval_ctx
) for x
in self
.items
]
494 """Any dict literal such as ``{1: 2, 3: 4}``. The items must be a list of
499 def as_const(self
, eval_ctx
=None):
500 eval_ctx
= get_eval_context(self
, eval_ctx
)
501 return dict(x
.as_const(eval_ctx
) for x
in self
.items
)
505 """A key, value pair for dicts."""
506 fields
= ('key', 'value')
508 def as_const(self
, eval_ctx
=None):
509 eval_ctx
= get_eval_context(self
, eval_ctx
)
510 return self
.key
.as_const(eval_ctx
), self
.value
.as_const(eval_ctx
)
513 class Keyword(Helper
):
514 """A key, value pair for keyword arguments where key is a string."""
515 fields
= ('key', 'value')
517 def as_const(self
, eval_ctx
=None):
518 eval_ctx
= get_eval_context(self
, eval_ctx
)
519 return self
.key
, self
.value
.as_const(eval_ctx
)
522 class CondExpr(Expr
):
523 """A conditional expression (inline if expression). (``{{
524 foo if bar else baz }}``)
526 fields
= ('test', 'expr1', 'expr2')
528 def as_const(self
, eval_ctx
=None):
529 eval_ctx
= get_eval_context(self
, eval_ctx
)
530 if self
.test
.as_const(eval_ctx
):
531 return self
.expr1
.as_const(eval_ctx
)
533 # if we evaluate to an undefined object, we better do that at runtime
534 if self
.expr2
is None:
537 return self
.expr2
.as_const(eval_ctx
)
541 """This node applies a filter on an expression. `name` is the name of
542 the filter, the rest of the fields are the same as for :class:`Call`.
544 If the `node` of a filter is `None` the contents of the last buffer are
545 filtered. Buffers are created by macros and filter blocks.
547 fields
= ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
549 def as_const(self
, eval_ctx
=None):
550 eval_ctx
= get_eval_context(self
, eval_ctx
)
551 if eval_ctx
.volatile
or self
.node
is None:
553 # we have to be careful here because we call filter_ below.
554 # if this variable would be called filter, 2to3 would wrap the
555 # call in a list beause it is assuming we are talking about the
556 # builtin filter function here which no longer returns a list in
557 # python 3. because of that, do not rename filter_ to filter!
558 filter_
= self
.environment
.filters
.get(self
.name
)
559 if filter_
is None or getattr(filter_
, 'contextfilter', False):
561 obj
= self
.node
.as_const(eval_ctx
)
562 args
= [x
.as_const(eval_ctx
) for x
in self
.args
]
563 if getattr(filter_
, 'evalcontextfilter', False):
564 args
.insert(0, eval_ctx
)
565 elif getattr(filter_
, 'environmentfilter', False):
566 args
.insert(0, self
.environment
)
567 kwargs
= dict(x
.as_const(eval_ctx
) for x
in self
.kwargs
)
568 if self
.dyn_args
is not None:
570 args
.extend(self
.dyn_args
.as_const(eval_ctx
))
573 if self
.dyn_kwargs
is not None:
575 kwargs
.update(self
.dyn_kwargs
.as_const(eval_ctx
))
579 return filter_(obj
, *args
, **kwargs
)
585 """Applies a test on an expression. `name` is the name of the test, the
586 rest of the fields are the same as for :class:`Call`.
588 fields
= ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
592 """Calls an expression. `args` is a list of arguments, `kwargs` a list
593 of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`
594 and `dyn_kwargs` has to be either `None` or a node that is used as
595 node for dynamic positional (``*args``) or keyword (``**kwargs``)
598 fields
= ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
600 def as_const(self
, eval_ctx
=None):
601 eval_ctx
= get_eval_context(self
, eval_ctx
)
602 if eval_ctx
.volatile
:
604 obj
= self
.node
.as_const(eval_ctx
)
606 # don't evaluate context functions
607 args
= [x
.as_const(eval_ctx
) for x
in self
.args
]
608 if isinstance(obj
, _context_function_types
):
609 if getattr(obj
, 'contextfunction', False):
611 elif getattr(obj
, 'evalcontextfunction', False):
612 args
.insert(0, eval_ctx
)
613 elif getattr(obj
, 'environmentfunction', False):
614 args
.insert(0, self
.environment
)
616 kwargs
= dict(x
.as_const(eval_ctx
) for x
in self
.kwargs
)
617 if self
.dyn_args
is not None:
619 args
.extend(self
.dyn_args
.as_const(eval_ctx
))
622 if self
.dyn_kwargs
is not None:
624 kwargs
.update(self
.dyn_kwargs
.as_const(eval_ctx
))
628 return obj(*args
, **kwargs
)
634 """Get an attribute or item from an expression and prefer the item."""
635 fields
= ('node', 'arg', 'ctx')
637 def as_const(self
, eval_ctx
=None):
638 eval_ctx
= get_eval_context(self
, eval_ctx
)
639 if self
.ctx
!= 'load':
642 return self
.environment
.getitem(self
.node
.as_const(eval_ctx
),
643 self
.arg
.as_const(eval_ctx
))
647 def can_assign(self
):
652 """Get an attribute or item from an expression that is a ascii-only
653 bytestring and prefer the attribute.
655 fields
= ('node', 'attr', 'ctx')
657 def as_const(self
, eval_ctx
=None):
658 if self
.ctx
!= 'load':
661 eval_ctx
= get_eval_context(self
, eval_ctx
)
662 return self
.environment
.getattr(self
.node
.as_const(eval_ctx
),
667 def can_assign(self
):
672 """Represents a slice object. This must only be used as argument for
675 fields
= ('start', 'stop', 'step')
677 def as_const(self
, eval_ctx
=None):
678 eval_ctx
= get_eval_context(self
, eval_ctx
)
682 return obj
.as_const(eval_ctx
)
683 return slice(const(self
.start
), const(self
.stop
), const(self
.step
))
687 """Concatenates the list of expressions provided after converting them to
692 def as_const(self
, eval_ctx
=None):
693 eval_ctx
= get_eval_context(self
, eval_ctx
)
694 return ''.join(text_type(x
.as_const(eval_ctx
)) for x
in self
.nodes
)
698 """Compares an expression with some other expressions. `ops` must be a
699 list of :class:`Operand`\s.
701 fields
= ('expr', 'ops')
703 def as_const(self
, eval_ctx
=None):
704 eval_ctx
= get_eval_context(self
, eval_ctx
)
705 result
= value
= self
.expr
.as_const(eval_ctx
)
708 new_value
= op
.expr
.as_const(eval_ctx
)
709 result
= _cmpop_to_func
[op
.op
](value
, new_value
)
716 class Operand(Helper
):
717 """Holds an operator and an expression."""
718 fields
= ('op', 'expr')
721 Operand
.__doc
__ += '\nThe following operators are available: ' + \
722 ', '.join(sorted('``%s``' % x
for x
in set(_binop_to_func
) |
723 set(_uaop_to_func
) |
set(_cmpop_to_func
)))
727 """Multiplies the left with the right node."""
732 """Divides the left by the right node."""
736 class FloorDiv(BinExpr
):
737 """Divides the left by the right node and truncates conver the
738 result into an integer by truncating.
744 """Add the left to the right node."""
749 """Substract the right from the left node."""
754 """Left modulo right."""
759 """Left to the power of right."""
764 """Short circuited AND."""
767 def as_const(self
, eval_ctx
=None):
768 eval_ctx
= get_eval_context(self
, eval_ctx
)
769 return self
.left
.as_const(eval_ctx
) and self
.right
.as_const(eval_ctx
)
773 """Short circuited OR."""
776 def as_const(self
, eval_ctx
=None):
777 eval_ctx
= get_eval_context(self
, eval_ctx
)
778 return self
.left
.as_const(eval_ctx
) or self
.right
.as_const(eval_ctx
)
781 class Not(UnaryExpr
):
782 """Negate the expression."""
786 class Neg(UnaryExpr
):
787 """Make the expression negative."""
791 class Pos(UnaryExpr
):
792 """Make the expression positive (noop for most expressions)"""
796 # Helpers for extensions
799 class EnvironmentAttribute(Expr
):
800 """Loads an attribute from the environment object. This is useful for
801 extensions that want to call a callback stored on the environment.
806 class ExtensionAttribute(Expr
):
807 """Returns the attribute of an extension bound to the environment.
808 The identifier is the identifier of the :class:`Extension`.
810 This node is usually constructed by calling the
811 :meth:`~jinja2.ext.Extension.attr` method on an extension.
813 fields
= ('identifier', 'name')
816 class ImportedName(Expr
):
817 """If created with an import name the import name is returned on node
818 access. For example ``ImportedName('cgi.escape')`` returns the `escape`
819 function from the cgi module on evaluation. Imports are optimized by the
820 compiler so there is no need to assign them to local variables.
822 fields
= ('importname',)
825 class InternalName(Expr
):
826 """An internal name in the compiler. You cannot create these nodes
827 yourself but the parser provides a
828 :meth:`~jinja2.parser.Parser.free_identifier` method that creates
829 a new identifier for you. This identifier is not available from the
830 template and is not threated specially by the compiler.
835 raise TypeError('Can\'t create internal names. Use the '
836 '`free_identifier` method on a parser.')
839 class MarkSafe(Expr
):
840 """Mark the wrapped expression as safe (wrap it as `Markup`)."""
843 def as_const(self
, eval_ctx
=None):
844 eval_ctx
= get_eval_context(self
, eval_ctx
)
845 return Markup(self
.expr
.as_const(eval_ctx
))
848 class MarkSafeIfAutoescape(Expr
):
849 """Mark the wrapped expression as safe (wrap it as `Markup`) but
850 only if autoescaping is active.
852 .. versionadded:: 2.5
856 def as_const(self
, eval_ctx
=None):
857 eval_ctx
= get_eval_context(self
, eval_ctx
)
858 if eval_ctx
.volatile
:
860 expr
= self
.expr
.as_const(eval_ctx
)
861 if eval_ctx
.autoescape
:
866 class ContextReference(Expr
):
867 """Returns the current template context. It can be used like a
868 :class:`Name` node, with a ``'load'`` ctx and will return the
869 current :class:`~jinja2.runtime.Context` object.
871 Here an example that assigns the current template name to a
872 variable named `foo`::
874 Assign(Name('foo', ctx='store'),
875 Getattr(ContextReference(), 'name'))
879 class Continue(Stmt
):
880 """Continue a loop."""
888 """An artificial scope."""
892 class EvalContextModifier(Stmt
):
893 """Modifies the eval context. For each option that should be modified,
894 a :class:`Keyword` has to be added to the :attr:`options` list.
896 Example to change the `autoescape` setting::
898 EvalContextModifier(options=[Keyword('autoescape', Const(True))])
900 fields
= ('options',)
903 class ScopedEvalContextModifier(EvalContextModifier
):
904 """Modifies the eval context and reverts it later. Works exactly like
905 :class:`EvalContextModifier` but will only modify the
906 :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.
911 # make sure nobody creates custom nodes
912 def _failing_new(*args
, **kwargs
):
913 raise TypeError('can\'t create custom node types')
914 NodeType
.__new
__ = staticmethod(_failing_new
); del _failing_new