1b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# -*- coding: utf-8 -*- 2b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)""" 3b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) jinja2.nodes 4b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) ~~~~~~~~~~~~ 5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) This module implements additional nodes derived from the ast base node. 7b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) It also provides some node tree helper functions like `in_lineno` and 9b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) `get_nodes` used by the parser and translator in order to normalize 10b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) python and jinja nodes. 11b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 12b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :copyright: (c) 2010 by the Jinja Team. 13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :license: BSD, see LICENSE for more details. 14b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)""" 15b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)import operator 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)from collections import deque 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)from jinja2.utils import Markup 1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)from jinja2._compat import next, izip, with_metaclass, text_type, \ 2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) method_type, function_type 21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 22b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#: the types we support for context functions 2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)_context_function_types = (function_type, method_type) 25b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 26b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)_binop_to_func = { 28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '*': operator.mul, 29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '/': operator.truediv, 30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '//': operator.floordiv, 31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '**': operator.pow, 32b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '%': operator.mod, 33b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '+': operator.add, 34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '-': operator.sub 35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)} 36b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 37b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)_uaop_to_func = { 38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'not': operator.not_, 39b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '+': operator.pos, 40b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '-': operator.neg 41b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)} 42b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 43b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)_cmpop_to_func = { 44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'eq': operator.eq, 45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'ne': operator.ne, 46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'gt': operator.gt, 47b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'gteq': operator.ge, 48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'lt': operator.lt, 49b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'lteq': operator.le, 50b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'in': lambda a, b: a in b, 51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'notin': lambda a, b: a not in b 52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)} 53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 54b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 55b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Impossible(Exception): 56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Raised if the node could not perform a requested action.""" 57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 58b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 59b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class NodeType(type): 60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A metaclass for nodes that handles the field and attribute 61b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) inheritance. fields and attributes from the parent class are 62b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) automatically forwarded to the child.""" 63b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 64b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __new__(cls, name, bases, d): 65b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for attr in 'fields', 'attributes': 66b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) storage = [] 67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) storage.extend(getattr(bases[0], attr, ())) 68b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) storage.extend(d.get(attr, ())) 69b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) assert len(bases) == 1, 'multiple inheritance not allowed' 70b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) assert len(storage) == len(set(storage)), 'layout conflict' 71b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) d[attr] = tuple(storage) 72b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) d.setdefault('abstract', False) 73b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return type.__new__(cls, name, bases, d) 74b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 75b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 76b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class EvalContext(object): 77b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Holds evaluation time information. Custom attributes can be attached 78b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) to it in extensions. 79b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 80b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 81b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __init__(self, environment, template_name=None): 82b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.environment = environment 83b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if callable(environment.autoescape): 84b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.autoescape = environment.autoescape(template_name) 85b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) else: 86b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.autoescape = environment.autoescape 87b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.volatile = False 88b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 89b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def save(self): 90b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.__dict__.copy() 91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 92b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def revert(self, old): 93b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.__dict__.clear() 94b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.__dict__.update(old) 95b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 96b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)def get_eval_context(node, ctx): 98b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if ctx is None: 99b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if node.environment is None: 100b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise RuntimeError('if no eval context is passed, the ' 101b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'node must have an attached ' 102b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'environment.') 103b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return EvalContext(node.environment) 104b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return ctx 105b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 106b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class Node(with_metaclass(NodeType, object)): 108b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Baseclass for all Jinja2 nodes. There are a number of nodes available 10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) of different types. There are four major types: 110b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 111b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - :class:`Stmt`: statements 112b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - :class:`Expr`: expressions 113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - :class:`Helper`: helper nodes 114b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - :class:`Template`: the outermost wrapper node 115b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 116b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) All nodes have fields and attributes. Fields may be other nodes, lists, 117b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) or arbitrary values. Fields are passed to the constructor as regular 118b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) positional arguments, attributes as keyword arguments. Each node has 119b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) two attributes: `lineno` (the line number of the node) and `environment`. 120b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) The `environment` attribute is set at the end of the parsing process for 121b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) all nodes automatically. 122b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 123b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = () 124b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) attributes = ('lineno', 'environment') 125b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 126b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 127b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __init__(self, *fields, **attributes): 128b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.abstract: 129b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('abstract nodes are not instanciable') 130b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if fields: 131b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if len(fields) != len(self.fields): 132b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if not self.fields: 133b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('%r takes 0 arguments' % 134b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.__class__.__name__) 135b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('%r takes 0 or %d argument%s' % ( 136b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.__class__.__name__, 137b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) len(self.fields), 138b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) len(self.fields) != 1 and 's' or '' 139b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) )) 140b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for name, arg in izip(self.fields, fields): 141b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) setattr(self, name, arg) 142b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for attr in self.attributes: 143b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) setattr(self, attr, attributes.pop(attr, None)) 144b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if attributes: 145b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('unknown attribute %r' % 14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) next(iter(attributes))) 147b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 148b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def iter_fields(self, exclude=None, only=None): 149b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """This method iterates over all fields that are defined and yields 150b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) ``(key, value)`` tuples. Per default all fields are returned, but 151b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) it's possible to limit that to some fields by providing the `only` 152b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) parameter or to exclude some using the `exclude` parameter. Both 153b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) should be sets or tuples of field names. 154b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 155b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for name in self.fields: 156b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if (exclude is only is None) or \ 157b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) (exclude is not None and name not in exclude) or \ 158b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) (only is not None and name in only): 159b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 160b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yield name, getattr(self, name) 161b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except AttributeError: 162b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) pass 163b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 164b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def iter_child_nodes(self, exclude=None, only=None): 165b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Iterates over all direct child nodes of the node. This iterates 166b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) over all fields and yields the values of they are nodes. If the value 167b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) of a field is a list all the nodes in that list are returned. 168b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 169b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for field, item in self.iter_fields(exclude, only): 170b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if isinstance(item, list): 171b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for n in item: 172b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if isinstance(n, Node): 173b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yield n 174b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) elif isinstance(item, Node): 175b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yield item 176b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 177b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def find(self, node_type): 178b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Find the first node of a given type. If no such node exists the 179b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return value is `None`. 180b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 181b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for result in self.find_all(node_type): 182b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return result 183b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 184b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def find_all(self, node_type): 185b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Find all the nodes of a given type. If the type is a tuple, 186b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) the check is performed for any of the tuple items. 187b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 188b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for child in self.iter_child_nodes(): 189b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if isinstance(child, node_type): 190b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yield child 191b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for result in child.find_all(node_type): 192b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yield result 193b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 194b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def set_ctx(self, ctx): 195b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Reset the context of a node and all child nodes. Per default the 196b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) parser will all generate nodes that have a 'load' context as it's the 197b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) most common one. This method is used in the parser to set assignment 198b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) targets and other nodes to a store context. 199b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 200b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo = deque([self]) 201b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) while todo: 202b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node = todo.popleft() 203b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if 'ctx' in node.fields: 204b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node.ctx = ctx 205b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo.extend(node.iter_child_nodes()) 206b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self 207b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 208b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def set_lineno(self, lineno, override=False): 209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Set the line numbers of the node and children.""" 210b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo = deque([self]) 211b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) while todo: 212b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node = todo.popleft() 213b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if 'lineno' in node.attributes: 214b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if node.lineno is None or override: 215b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node.lineno = lineno 216b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo.extend(node.iter_child_nodes()) 217b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self 218b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 219b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def set_environment(self, environment): 220b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Set the environment for all nodes.""" 221b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo = deque([self]) 222b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) while todo: 223b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node = todo.popleft() 224b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node.environment = environment 225b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) todo.extend(node.iter_child_nodes()) 226b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self 227b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 228b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __eq__(self, other): 229b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return type(self) is type(other) and \ 230b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) tuple(self.iter_fields()) == tuple(other.iter_fields()) 231b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 232b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __ne__(self, other): 233b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return not self.__eq__(other) 234b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 23558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) # Restore Python 2 hashing behavior on Python 3 23658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) __hash__ = object.__hash__ 23758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 238b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __repr__(self): 239b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return '%s(%s)' % ( 240b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.__class__.__name__, 241b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) ', '.join('%s=%r' % (arg, getattr(self, arg, None)) for 242b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) arg in self.fields) 243b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) ) 244b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 245b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 246b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Stmt(Node): 247b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Base node for all statements.""" 248b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 249b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 250b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 251b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Helper(Node): 252b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Nodes that exist in a specific context only.""" 253b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 254b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 255b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 256b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Template(Node): 257b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Node that represents a template. This must be the outermost node that 258b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) is passed to the compiler. 259b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 260b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('body',) 261b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 262b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 263b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Output(Stmt): 264b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A node that holds multiple expressions which are then printed out. 265b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) This is used both for the `print` statement and the regular template data. 266b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 267b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('nodes',) 268b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 269b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 270b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Extends(Stmt): 271b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Represents an extends statement.""" 272b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('template',) 273b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 274b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 275b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class For(Stmt): 276b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """The for loop. `target` is the target for the iteration (usually a 277b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`Name` or :class:`Tuple`), `iter` the iterable. `body` is a list 278b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) of nodes that are used as loop-body, and `else_` a list of nodes for the 279b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) `else` block. If no else node exists it has to be an empty list. 280b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 281b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) For filtered nodes an expression can be stored as `test`, otherwise `None`. 282b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 283b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('target', 'iter', 'body', 'else_', 'test', 'recursive') 284b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 285b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 286b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class If(Stmt): 287b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """If `test` is true, `body` is rendered, else `else_`.""" 288b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('test', 'body', 'else_') 289b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 290b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 291b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Macro(Stmt): 292b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A macro definition. `name` is the name of the macro, `args` a list of 293b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) arguments and `defaults` a list of defaults if there are any. `body` is 294b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) a list of nodes for the macro body. 295b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 296b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('name', 'args', 'defaults', 'body') 297b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 298b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 299b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class CallBlock(Stmt): 300b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Like a macro without a name but a call instead. `call` is called with 301b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) the unnamed macro as `caller` argument this node holds. 302b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 303b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('call', 'args', 'defaults', 'body') 304b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 305b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 306b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class FilterBlock(Stmt): 307b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Node for filter sections.""" 308b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('body', 'filter') 309b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 310b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 311b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Block(Stmt): 312b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A node that represents a block.""" 313b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('name', 'body', 'scoped') 314b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 315b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 316b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Include(Stmt): 317b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A node that represents the include tag.""" 318b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('template', 'with_context', 'ignore_missing') 319b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 320b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 321b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Import(Stmt): 322b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A node that represents the import tag.""" 323b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('template', 'target', 'with_context') 324b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 325b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 326b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class FromImport(Stmt): 327b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A node that represents the from import tag. It's important to not 328b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) pass unsafe names to the name attribute. The compiler translates the 329b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) attribute lookups directly into getattr calls and does *not* use the 330b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) subscript callback of the interface. As exported variables may not 331b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) start with double underscores (which the parser asserts) this is not a 332b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) problem for regular Jinja code, but if this node is used in an extension 333b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) extra care must be taken. 334b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 335b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) The list of names may contain tuples if aliases are wanted. 336b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 337b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('template', 'names', 'with_context') 338b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 339b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 340b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class ExprStmt(Stmt): 341b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A statement that evaluates an expression and discards the result.""" 342b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node',) 343b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 344b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 345b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Assign(Stmt): 346b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Assigns an expression to a target.""" 347b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('target', 'node') 348b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 349b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 350b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Expr(Node): 351b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Baseclass for all expressions.""" 352b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 353b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 354b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 355b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Return the value of the expression as constant or raise 356b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :exc:`Impossible` if this was not possible. 357b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 358b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) An :class:`EvalContext` can be provided, if none is given 359b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) a default context is created which requires the nodes to have 360b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) an attached environment. 361b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 362b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) .. versionchanged:: 2.4 363b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) the `eval_ctx` parameter was added. 364b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 365b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 366b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 367b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def can_assign(self): 368b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Check if it's possible to assign something to this node.""" 369b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return False 370b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 371b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 372b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class BinExpr(Expr): 373b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Baseclass for all binary expressions.""" 374b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('left', 'right') 375b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = None 376b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 377b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 378b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 379b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 380b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # intercepted operators cannot be folded at compile time 381b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.environment.sandboxed and \ 382b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.operator in self.environment.intercepted_binops: 383b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 384b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) f = _binop_to_func[self.operator] 385b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 386b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx)) 387b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 388b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 389b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 390b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 391b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class UnaryExpr(Expr): 392b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Baseclass for all unary expressions.""" 393b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node',) 394b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = None 395b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 396b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 397b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 398b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 399b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # intercepted operators cannot be folded at compile time 400b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.environment.sandboxed and \ 401b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.operator in self.environment.intercepted_unops: 402b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 403b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) f = _uaop_to_func[self.operator] 404b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 405b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return f(self.node.as_const(eval_ctx)) 406b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 407b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 408b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 409b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 410b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Name(Expr): 411b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Looks up a name or stores a value in a name. 412b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) The `ctx` of the node can be one of the following values: 413b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 414b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - `store`: store a value in the name 415b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - `load`: load that name 416b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) - `param`: like `store` but if the name was defined as function parameter. 417b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 418b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('name', 'ctx') 419b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 420b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def can_assign(self): 421b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.name not in ('true', 'false', 'none', 422b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 'True', 'False', 'None') 423b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 424b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 425b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Literal(Expr): 426b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Baseclass for literals.""" 427b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) abstract = True 428b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 429b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 430b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Const(Literal): 431b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """All constant values. The parser will return this node for simple 432b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) constants such as ``42`` or ``"foo"`` but it can be used to store more 433b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) complex values such as lists too. Only constants with a safe 434b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) representation (objects where ``eval(repr(x)) == x`` is true). 435b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 436b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('value',) 437b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 438b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 439b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.value 440b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 441b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) @classmethod 442b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def from_untrusted(cls, value, lineno=None, environment=None): 443b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Return a const object if the value is representable as 444b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) constant value in the generated code, otherwise it will raise 445b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) an `Impossible` exception. 446b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 44758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) from .compiler import has_safe_repr 448b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if not has_safe_repr(value): 449b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 450b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return cls(value, lineno=lineno, environment=environment) 451b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 452b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 453b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class TemplateData(Literal): 454b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A constant template string.""" 455b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('data',) 456b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 457b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 458b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 459b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.volatile: 460b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 461b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.autoescape: 462b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return Markup(self.data) 463b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.data 464b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 465b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 466b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Tuple(Literal): 467b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """For loop unpacking and some other things like multiple arguments 468b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for subscripts. Like for :class:`Name` `ctx` specifies if the tuple 469b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) is used for loading the names or storing. 470b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 471b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('items', 'ctx') 472b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 473b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 474b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 475b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return tuple(x.as_const(eval_ctx) for x in self.items) 476b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 477b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def can_assign(self): 478b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for item in self.items: 479b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if not item.can_assign(): 480b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return False 481b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return True 482b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 483b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 484b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class List(Literal): 485b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Any list literal such as ``[1, 2, 3]``""" 486b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('items',) 487b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 488b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 489b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 490b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return [x.as_const(eval_ctx) for x in self.items] 491b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 492b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 493b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Dict(Literal): 494b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Any dict literal such as ``{1: 2, 3: 4}``. The items must be a list of 495b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`Pair` nodes. 496b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 497b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('items',) 498b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 499b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 500b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 501b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return dict(x.as_const(eval_ctx) for x in self.items) 502b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 503b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 504b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Pair(Helper): 505b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A key, value pair for dicts.""" 506b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('key', 'value') 507b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 508b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 509b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 510b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx) 511b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 512b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 513b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Keyword(Helper): 514b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A key, value pair for keyword arguments where key is a string.""" 515b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('key', 'value') 516b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 517b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 518b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 519b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.key, self.value.as_const(eval_ctx) 520b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 521b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 522b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class CondExpr(Expr): 523b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """A conditional expression (inline if expression). (``{{ 524b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) foo if bar else baz }}``) 525b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 526b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('test', 'expr1', 'expr2') 527b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 528b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 529b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 530b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.test.as_const(eval_ctx): 531b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.expr1.as_const(eval_ctx) 532b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 533b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # if we evaluate to an undefined object, we better do that at runtime 534b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.expr2 is None: 535b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 536b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 537b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.expr2.as_const(eval_ctx) 538b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 539b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 540b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Filter(Expr): 541b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """This node applies a filter on an expression. `name` is the name of 542b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) the filter, the rest of the fields are the same as for :class:`Call`. 543b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 544b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) If the `node` of a filter is `None` the contents of the last buffer are 545b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) filtered. Buffers are created by macros and filter blocks. 546b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 547b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs') 548b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 549b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 550b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 551b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.volatile or self.node is None: 552b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 553b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # we have to be careful here because we call filter_ below. 554b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # if this variable would be called filter, 2to3 would wrap the 555b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # call in a list beause it is assuming we are talking about the 556b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # builtin filter function here which no longer returns a list in 557b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # python 3. because of that, do not rename filter_ to filter! 558b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) filter_ = self.environment.filters.get(self.name) 559b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if filter_ is None or getattr(filter_, 'contextfilter', False): 560b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 561b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) obj = self.node.as_const(eval_ctx) 562b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args = [x.as_const(eval_ctx) for x in self.args] 563b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if getattr(filter_, 'evalcontextfilter', False): 564b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.insert(0, eval_ctx) 565b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) elif getattr(filter_, 'environmentfilter', False): 566b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.insert(0, self.environment) 567b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) kwargs = dict(x.as_const(eval_ctx) for x in self.kwargs) 568b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.dyn_args is not None: 569b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 570b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.extend(self.dyn_args.as_const(eval_ctx)) 571b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 572b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 573b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.dyn_kwargs is not None: 574b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 575b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) kwargs.update(self.dyn_kwargs.as_const(eval_ctx)) 576b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 577b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 578b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 579b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return filter_(obj, *args, **kwargs) 580b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 581b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 582b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 583b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 584b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Test(Expr): 585b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Applies a test on an expression. `name` is the name of the test, the 586b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) rest of the fields are the same as for :class:`Call`. 587b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 588b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs') 589b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 590b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 591b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Call(Expr): 592b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Calls an expression. `args` is a list of arguments, `kwargs` a list 593b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args` 594b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) and `dyn_kwargs` has to be either `None` or a node that is used as 595b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) node for dynamic positional (``*args``) or keyword (``**kwargs``) 596b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) arguments. 597b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 598b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs') 599b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 600b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 601b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 602b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.volatile: 603b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 604b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) obj = self.node.as_const(eval_ctx) 605b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 606b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) # don't evaluate context functions 607b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args = [x.as_const(eval_ctx) for x in self.args] 608b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if isinstance(obj, _context_function_types): 609b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if getattr(obj, 'contextfunction', False): 610b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 611b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) elif getattr(obj, 'evalcontextfunction', False): 612b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.insert(0, eval_ctx) 613b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) elif getattr(obj, 'environmentfunction', False): 614b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.insert(0, self.environment) 615b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 616b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) kwargs = dict(x.as_const(eval_ctx) for x in self.kwargs) 617b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.dyn_args is not None: 618b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 619b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) args.extend(self.dyn_args.as_const(eval_ctx)) 620b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 621b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 622b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.dyn_kwargs is not None: 623b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 624b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) kwargs.update(self.dyn_kwargs.as_const(eval_ctx)) 625b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 626b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 627b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 628b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return obj(*args, **kwargs) 629b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 630b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 631b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 632b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 633b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Getitem(Expr): 634b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Get an attribute or item from an expression and prefer the item.""" 635b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node', 'arg', 'ctx') 636b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 637b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 638b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 639b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.ctx != 'load': 640b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 641b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 642b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.environment.getitem(self.node.as_const(eval_ctx), 643b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.arg.as_const(eval_ctx)) 644b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 645b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 646b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 647b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def can_assign(self): 648b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return False 649b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 650b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 651b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Getattr(Expr): 652b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Get an attribute or item from an expression that is a ascii-only 653b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) bytestring and prefer the attribute. 654b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 655b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('node', 'attr', 'ctx') 656b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 657b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 658b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if self.ctx != 'load': 659b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 660b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 661b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 662b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.environment.getattr(self.node.as_const(eval_ctx), 663b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) self.attr) 664b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 665b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 666b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 667b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def can_assign(self): 668b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return False 669b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 670b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 671b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Slice(Expr): 672b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Represents a slice object. This must only be used as argument for 673b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`Subscript`. 674b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 675b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('start', 'stop', 'step') 676b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 677b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 678b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 679b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def const(obj): 680b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if obj is None: 681b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return None 682b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return obj.as_const(eval_ctx) 683b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return slice(const(self.start), const(self.stop), const(self.step)) 684b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 685b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 686b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Concat(Expr): 687b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Concatenates the list of expressions provided after converting them to 688b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) unicode. 689b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 690b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('nodes',) 691b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 692b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 693b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 69458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return ''.join(text_type(x.as_const(eval_ctx)) for x in self.nodes) 695b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 696b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 697b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Compare(Expr): 698b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Compares an expression with some other expressions. `ops` must be a 699b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) list of :class:`Operand`\s. 700b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 701b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('expr', 'ops') 702b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 703b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 704b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 705b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) result = value = self.expr.as_const(eval_ctx) 706b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) try: 707b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) for op in self.ops: 708b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) new_value = op.expr.as_const(eval_ctx) 709b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) result = _cmpop_to_func[op.op](value, new_value) 710b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) value = new_value 711b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) except Exception: 712b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 713b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return result 714b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 715b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 716b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Operand(Helper): 717b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Holds an operator and an expression.""" 718b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('op', 'expr') 719b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 720b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)if __debug__: 721b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) Operand.__doc__ += '\nThe following operators are available: ' + \ 722b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) ', '.join(sorted('``%s``' % x for x in set(_binop_to_func) | 723b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) set(_uaop_to_func) | set(_cmpop_to_func))) 724b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 725b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 726b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Mul(BinExpr): 727b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Multiplies the left with the right node.""" 728b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '*' 729b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 730b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 731b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Div(BinExpr): 732b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Divides the left by the right node.""" 733b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '/' 734b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 735b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 736b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class FloorDiv(BinExpr): 737b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Divides the left by the right node and truncates conver the 738b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) result into an integer by truncating. 739b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 740b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '//' 741b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 742b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 743b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Add(BinExpr): 744b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Add the left to the right node.""" 745b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '+' 746b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 747b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 748b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Sub(BinExpr): 749b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Substract the right from the left node.""" 750b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '-' 751b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 752b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 753b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Mod(BinExpr): 754b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Left modulo right.""" 755b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '%' 756b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 757b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 758b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Pow(BinExpr): 759b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Left to the power of right.""" 760b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '**' 761b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 762b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 763b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class And(BinExpr): 764b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Short circuited AND.""" 765b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = 'and' 766b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 767b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 768b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 769b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx) 770b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 771b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 772b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Or(BinExpr): 773b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Short circuited OR.""" 774b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = 'or' 775b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 776b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 777b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 778b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx) 779b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 780b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 781b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Not(UnaryExpr): 782b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Negate the expression.""" 783b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = 'not' 784b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 785b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 786b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Neg(UnaryExpr): 787b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Make the expression negative.""" 788b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '-' 789b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 790b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 791b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Pos(UnaryExpr): 792b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Make the expression positive (noop for most expressions)""" 793b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) operator = '+' 794b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 795b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 796b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# Helpers for extensions 797b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 798b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 799b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class EnvironmentAttribute(Expr): 800b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Loads an attribute from the environment object. This is useful for 801b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) extensions that want to call a callback stored on the environment. 802b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 803b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('name',) 804b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 805b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 806b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class ExtensionAttribute(Expr): 807b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Returns the attribute of an extension bound to the environment. 808b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) The identifier is the identifier of the :class:`Extension`. 809b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 810b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) This node is usually constructed by calling the 811b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :meth:`~jinja2.ext.Extension.attr` method on an extension. 812b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 813b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('identifier', 'name') 814b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 815b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 816b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class ImportedName(Expr): 817b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """If created with an import name the import name is returned on node 818b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) access. For example ``ImportedName('cgi.escape')`` returns the `escape` 819b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) function from the cgi module on evaluation. Imports are optimized by the 820b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) compiler so there is no need to assign them to local variables. 821b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 822b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('importname',) 823b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 824b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 825b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class InternalName(Expr): 826b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """An internal name in the compiler. You cannot create these nodes 827b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) yourself but the parser provides a 828b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :meth:`~jinja2.parser.Parser.free_identifier` method that creates 829b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) a new identifier for you. This identifier is not available from the 830b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) template and is not threated specially by the compiler. 831b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 832b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('name',) 833b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 834b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def __init__(self): 835b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('Can\'t create internal names. Use the ' 836b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) '`free_identifier` method on a parser.') 837b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 838b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 839b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class MarkSafe(Expr): 840b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Mark the wrapped expression as safe (wrap it as `Markup`).""" 841b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('expr',) 842b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 843b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 844b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 845b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return Markup(self.expr.as_const(eval_ctx)) 846b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 847b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 848b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class MarkSafeIfAutoescape(Expr): 849b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Mark the wrapped expression as safe (wrap it as `Markup`) but 850b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) only if autoescaping is active. 851b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 852b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) .. versionadded:: 2.5 853b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 854b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('expr',) 855b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 856b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) def as_const(self, eval_ctx=None): 857b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) eval_ctx = get_eval_context(self, eval_ctx) 858b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.volatile: 859b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise Impossible() 860b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) expr = self.expr.as_const(eval_ctx) 861b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) if eval_ctx.autoescape: 862b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return Markup(expr) 863b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return expr 864b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 865b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 866b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class ContextReference(Expr): 867b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Returns the current template context. It can be used like a 868b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`Name` node, with a ``'load'`` ctx and will return the 869b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) current :class:`~jinja2.runtime.Context` object. 870b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 871b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) Here an example that assigns the current template name to a 872b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) variable named `foo`:: 873b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 874b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) Assign(Name('foo', ctx='store'), 875b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) Getattr(ContextReference(), 'name')) 876b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 877b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 878b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 879b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Continue(Stmt): 880b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Continue a loop.""" 881b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 882b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 883b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Break(Stmt): 884b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Break a loop.""" 885b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 886b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 887b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class Scope(Stmt): 888b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """An artificial scope.""" 889b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('body',) 890b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 891b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 892b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class EvalContextModifier(Stmt): 893b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Modifies the eval context. For each option that should be modified, 894b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) a :class:`Keyword` has to be added to the :attr:`options` list. 895b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 896b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) Example to change the `autoescape` setting:: 897b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 898b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EvalContextModifier(options=[Keyword('autoescape', Const(True))]) 899b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 900b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('options',) 901b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 902b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 903b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class ScopedEvalContextModifier(EvalContextModifier): 904b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """Modifies the eval context and reverts it later. Works exactly like 905b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`EvalContextModifier` but will only modify the 906b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`. 907b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) """ 908b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) fields = ('body',) 909b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 910b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 911b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# make sure nobody creates custom nodes 912b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)def _failing_new(*args, **kwargs): 913b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) raise TypeError('can\'t create custom node types') 914b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)NodeType.__new__ = staticmethod(_failing_new); del _failing_new 915