Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.compiler |
| 4 | ~~~~~~~~~~~~~~~ |
| 5 | |
| 6 | Compiles nodes into python code. |
| 7 | |
| 8 | :copyright: Copyright 2008 by Armin Ronacher. |
| 9 | :license: GNU GPL. |
| 10 | """ |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 11 | from time import time |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 12 | from copy import copy |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 13 | from random import randrange |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 14 | from keyword import iskeyword |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 15 | from cStringIO import StringIO |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 16 | from itertools import chain |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 17 | from jinja2 import nodes |
| 18 | from jinja2.visitor import NodeVisitor, NodeTransformer |
| 19 | from jinja2.exceptions import TemplateAssertionError |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 20 | from jinja2.runtime import concat |
Armin Ronacher | 5236d8c | 2008-04-17 11:23:16 +0200 | [diff] [blame] | 21 | from jinja2.utils import Markup |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | operators = { |
| 25 | 'eq': '==', |
| 26 | 'ne': '!=', |
| 27 | 'gt': '>', |
| 28 | 'gteq': '>=', |
| 29 | 'lt': '<', |
| 30 | 'lteq': '<=', |
| 31 | 'in': 'in', |
| 32 | 'notin': 'not in' |
| 33 | } |
| 34 | |
| 35 | |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 36 | try: |
| 37 | exec '(0 if 0 else 0)' |
| 38 | except SyntaxError: |
| 39 | have_condexpr = False |
| 40 | else: |
| 41 | have_condexpr = True |
| 42 | |
| 43 | |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 44 | def generate(node, environment, name, filename, stream=None): |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 45 | """Generate the python source for a node tree.""" |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 46 | generator = CodeGenerator(environment, name, filename, stream) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 47 | generator.visit(node) |
| 48 | if stream is None: |
| 49 | return generator.stream.getvalue() |
| 50 | |
| 51 | |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 52 | def has_safe_repr(value): |
| 53 | """Does the node have a safe representation?""" |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 54 | if value is None or value is NotImplemented or value is Ellipsis: |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 55 | return True |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 56 | if isinstance(value, (bool, int, long, float, complex, basestring, |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 57 | xrange, Markup)): |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 58 | return True |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 59 | if isinstance(value, (tuple, list, set, frozenset)): |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 60 | for item in value: |
| 61 | if not has_safe_repr(item): |
| 62 | return False |
| 63 | return True |
| 64 | elif isinstance(value, dict): |
| 65 | for key, value in value.iteritems(): |
| 66 | if not has_safe_repr(key): |
| 67 | return False |
| 68 | if not has_safe_repr(value): |
| 69 | return False |
| 70 | return True |
| 71 | return False |
| 72 | |
| 73 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 74 | class Identifiers(object): |
| 75 | """Tracks the status of identifiers in frames.""" |
| 76 | |
| 77 | def __init__(self): |
| 78 | # variables that are known to be declared (probably from outer |
| 79 | # frames or because they are special for the frame) |
| 80 | self.declared = set() |
| 81 | |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 82 | # undeclared variables from outer scopes |
| 83 | self.outer_undeclared = set() |
| 84 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 85 | # names that are accessed without being explicitly declared by |
| 86 | # this one or any of the outer scopes. Names can appear both in |
| 87 | # declared and undeclared. |
| 88 | self.undeclared = set() |
| 89 | |
| 90 | # names that are declared locally |
| 91 | self.declared_locally = set() |
| 92 | |
| 93 | # names that are declared by parameters |
| 94 | self.declared_parameter = set() |
| 95 | |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 96 | # filters/tests that are referenced |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 97 | self.filters = set() |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 98 | self.tests = set() |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 99 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 100 | def add_special(self, name): |
| 101 | """Register a special name like `loop`.""" |
| 102 | self.undeclared.discard(name) |
| 103 | self.declared.add(name) |
| 104 | |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 105 | def is_declared(self, name, local_only=False): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 106 | """Check if a name is declared in this or an outer scope.""" |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 107 | if name in self.declared_locally or name in self.declared_parameter: |
| 108 | return True |
| 109 | if local_only: |
| 110 | return False |
| 111 | return name in self.declared |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 112 | |
| 113 | def find_shadowed(self): |
| 114 | """Find all the shadowed names.""" |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 115 | return (self.declared | self.outer_undeclared) & \ |
| 116 | (self.declared_locally | self.declared_parameter) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class Frame(object): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 120 | """Holds compile time information for us.""" |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 121 | |
| 122 | def __init__(self, parent=None): |
| 123 | self.identifiers = Identifiers() |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 124 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 125 | # a toplevel frame is the root + soft frames such as if conditions. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 126 | self.toplevel = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 127 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 128 | # the root frame is basically just the outermost frame, so no if |
| 129 | # conditions. This information is used to optimize inheritance |
| 130 | # situations. |
| 131 | self.rootlevel = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 132 | |
| 133 | # inside some tags we are using a buffer rather than yield statements. |
| 134 | # this for example affects {% filter %} or {% macro %}. If a frame |
| 135 | # is buffered this variable points to the name of the list used as |
| 136 | # buffer. |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 137 | self.buffer = None |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 138 | |
| 139 | # if a frame has name_overrides, all read access to a name in this |
| 140 | # dict is redirected to a string expression. |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 141 | self.name_overrides = {} |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 142 | |
| 143 | # the name of the block we're in, otherwise None. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 144 | self.block = parent and parent.block or None |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 145 | |
| 146 | # the parent of this frame |
| 147 | self.parent = parent |
| 148 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 149 | if parent is not None: |
| 150 | self.identifiers.declared.update( |
| 151 | parent.identifiers.declared | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 152 | parent.identifiers.declared_locally | |
| 153 | parent.identifiers.declared_parameter |
| 154 | ) |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 155 | self.identifiers.outer_undeclared.update( |
| 156 | parent.identifiers.undeclared - |
| 157 | self.identifiers.declared |
| 158 | ) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 159 | self.buffer = parent.buffer |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 160 | self.name_overrides = parent.name_overrides.copy() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 161 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 162 | def copy(self): |
| 163 | """Create a copy of the current one.""" |
| 164 | rv = copy(self) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 165 | rv.identifiers = copy(self.identifiers) |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 166 | rv.name_overrides = self.name_overrides.copy() |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 167 | return rv |
| 168 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 169 | def inspect(self, nodes, with_depenencies=False, hard_scope=False): |
| 170 | """Walk the node and check for identifiers. If the scope is hard (eg: |
| 171 | enforce on a python level) overrides from outer scopes are tracked |
| 172 | differently. |
| 173 | |
| 174 | Per default filters and tests (dependencies) are not tracked. That's |
| 175 | the case because filters and tests are absolutely immutable and so we |
| 176 | can savely use them in closures too. The `Template` and `Block` |
| 177 | visitor visits the frame with dependencies to collect them. |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 178 | """ |
| 179 | visitor = FrameIdentifierVisitor(self.identifiers, hard_scope) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 180 | for node in nodes: |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 181 | visitor.visit(node, True, with_depenencies) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 182 | |
| 183 | def inner(self): |
| 184 | """Return an inner frame.""" |
| 185 | return Frame(self) |
| 186 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 187 | def soft(self): |
| 188 | """Return a soft frame. A soft frame may not be modified as |
| 189 | standalone thing as it shares the resources with the frame it |
| 190 | was created of, but it's not a rootlevel frame any longer. |
| 191 | """ |
| 192 | rv = copy(self) |
| 193 | rv.rootlevel = False |
| 194 | return rv |
| 195 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 196 | |
| 197 | class FrameIdentifierVisitor(NodeVisitor): |
| 198 | """A visitor for `Frame.inspect`.""" |
| 199 | |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 200 | def __init__(self, identifiers, hard_scope): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 201 | self.identifiers = identifiers |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 202 | self.hard_scope = hard_scope |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 203 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 204 | def visit_Name(self, node, visit_ident, visit_deps): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 205 | """All assignments to names go through this function.""" |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 206 | if visit_ident: |
| 207 | if node.ctx in ('store', 'param'): |
| 208 | self.identifiers.declared_locally.add(node.name) |
| 209 | elif node.ctx == 'load' and not \ |
| 210 | self.identifiers.is_declared(node.name, self.hard_scope): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 211 | self.identifiers.undeclared.add(node.name) |
| 212 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 213 | def visit_Filter(self, node, visit_ident, visit_deps): |
| 214 | if visit_deps: |
| 215 | self.generic_visit(node, visit_ident, True) |
| 216 | self.identifiers.filters.add(node.name) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 217 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 218 | def visit_Test(self, node, visit_ident, visit_deps): |
| 219 | if visit_deps: |
| 220 | self.generic_visit(node, visit_ident, True) |
| 221 | self.identifiers.tests.add(node.name) |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 222 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 223 | def visit_Macro(self, node, visit_ident, visit_deps): |
| 224 | if visit_ident: |
| 225 | self.identifiers.declared_locally.add(node.name) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 226 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 227 | def visit_Import(self, node, visit_ident, visit_deps): |
| 228 | if visit_ident: |
| 229 | self.generic_visit(node, True, visit_deps) |
| 230 | self.identifiers.declared_locally.add(node.target) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 231 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 232 | def visit_FromImport(self, node, visit_ident, visit_deps): |
| 233 | if visit_ident: |
| 234 | self.generic_visit(node, True, visit_deps) |
| 235 | for name in node.names: |
| 236 | if isinstance(name, tuple): |
| 237 | self.identifiers.declared_locally.add(name[1]) |
| 238 | else: |
| 239 | self.identifiers.declared_locally.add(name) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 240 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 241 | def visit_Assign(self, node, visit_ident, visit_deps): |
Armin Ronacher | ebe55aa | 2008-04-10 20:51:23 +0200 | [diff] [blame] | 242 | """Visit assignments in the correct order.""" |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 243 | self.visit(node.node, visit_ident, visit_deps) |
| 244 | self.visit(node.target, visit_ident, visit_deps) |
Armin Ronacher | ebe55aa | 2008-04-10 20:51:23 +0200 | [diff] [blame] | 245 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 246 | def visit_For(self, node, visit_ident, visit_deps): |
| 247 | """Visiting stops at for blocks. However the block sequence |
| 248 | is visited as part of the outer scope. |
| 249 | """ |
| 250 | if visit_ident: |
| 251 | self.visit(node.iter, True, visit_deps) |
| 252 | if visit_deps: |
| 253 | for child in node.iter_child_nodes(exclude=('iter',)): |
| 254 | self.visit(child, False, True) |
| 255 | |
| 256 | def ident_stop(self, node, visit_ident, visit_deps): |
| 257 | if visit_deps: |
| 258 | self.generic_visit(node, False, True) |
| 259 | visit_CallBlock = visit_FilterBlock = ident_stop |
| 260 | visit_Block = lambda s, n, a, b: None |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 261 | |
| 262 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 263 | class CompilerExit(Exception): |
| 264 | """Raised if the compiler encountered a situation where it just |
| 265 | doesn't make sense to further process the code. Any block that |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 266 | raises such an exception is not further processed. |
| 267 | """ |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 268 | |
| 269 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 270 | class CodeGenerator(NodeVisitor): |
| 271 | |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 272 | def __init__(self, environment, name, filename, stream=None): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 273 | if stream is None: |
| 274 | stream = StringIO() |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 275 | self.environment = environment |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 276 | self.name = name |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 277 | self.filename = filename |
| 278 | self.stream = stream |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 279 | |
| 280 | # a registry for all blocks. Because blocks are moved out |
| 281 | # into the global python scope they are registered here |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 282 | self.blocks = {} |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 283 | |
| 284 | # the number of extends statements so far |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 285 | self.extends_so_far = 0 |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 286 | |
| 287 | # some templates have a rootlevel extends. In this case we |
| 288 | # can safely assume that we're a child template and do some |
| 289 | # more optimizations. |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 290 | self.has_known_extends = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 291 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 292 | # the current line number |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 293 | self.code_lineno = 1 |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 294 | |
| 295 | # the debug information |
| 296 | self.debug_info = [] |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 297 | self._write_debug_info = None |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 298 | |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 299 | # the number of new lines before the next write() |
| 300 | self._new_lines = 0 |
| 301 | |
| 302 | # the line number of the last written statement |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 303 | self._last_line = 0 |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 304 | |
| 305 | # true if nothing was written so far. |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 306 | self._first_write = True |
| 307 | |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 308 | # used by the `temporary_identifier` method to get new |
| 309 | # unique, temporary identifier |
| 310 | self._last_identifier = 0 |
| 311 | |
| 312 | # the current indentation |
| 313 | self._indentation = 0 |
| 314 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 315 | def temporary_identifier(self): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 316 | """Get a new unique identifier.""" |
| 317 | self._last_identifier += 1 |
| 318 | return 't%d' % self._last_identifier |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 319 | |
| 320 | def indent(self): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 321 | """Indent by one.""" |
| 322 | self._indentation += 1 |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 323 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 324 | def outdent(self, step=1): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 325 | """Outdent by step.""" |
| 326 | self._indentation -= step |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 327 | |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 328 | def blockvisit(self, nodes, frame, indent=True, force_generator=True): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 329 | """Visit a list of nodes as block in a frame. Per default the |
| 330 | code is indented, but this can be disabled by setting the indent |
| 331 | parameter to False. If the current frame is no buffer a dummy |
| 332 | ``if 0: yield None`` is written automatically unless the |
| 333 | force_generator parameter is set to False. |
| 334 | """ |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 335 | if indent: |
| 336 | self.indent() |
| 337 | if frame.buffer is None and force_generator: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 338 | self.writeline('if 0: yield None') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 339 | try: |
| 340 | for node in nodes: |
| 341 | self.visit(node, frame) |
| 342 | except CompilerExit: |
| 343 | pass |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 344 | if indent: |
| 345 | self.outdent() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 346 | |
| 347 | def write(self, x): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 348 | """Write a string into the output stream.""" |
| 349 | if self._new_lines: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 350 | if not self._first_write: |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 351 | self.stream.write('\n' * self._new_lines) |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 352 | self.code_lineno += self._new_lines |
| 353 | if self._write_debug_info is not None: |
| 354 | self.debug_info.append((self._write_debug_info, |
| 355 | self.code_lineno)) |
| 356 | self._write_debug_info = None |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 357 | self._first_write = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 358 | self.stream.write(' ' * self._indentation) |
| 359 | self._new_lines = 0 |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 360 | self.stream.write(x) |
| 361 | |
| 362 | def writeline(self, x, node=None, extra=0): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 363 | """Combination of newline and write.""" |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 364 | self.newline(node, extra) |
| 365 | self.write(x) |
| 366 | |
| 367 | def newline(self, node=None, extra=0): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 368 | """Add one or more newlines before the next write.""" |
| 369 | self._new_lines = max(self._new_lines, 1 + extra) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 370 | if node is not None and node.lineno != self._last_line: |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 371 | self._write_debug_info = node.lineno |
| 372 | self._last_line = node.lineno |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 373 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 374 | def signature(self, node, frame, have_comma=True, extra_kwargs=None): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 375 | """Writes a function call to the stream for the current node. |
| 376 | Per default it will write a leading comma but this can be |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 377 | disabled by setting have_comma to False. The extra keyword |
| 378 | arguments may not include python keywords otherwise a syntax |
| 379 | error could occour. The extra keyword arguments should be given |
| 380 | as python dict. |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 381 | """ |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 382 | have_comma = have_comma and [True] or [] |
| 383 | def touch_comma(): |
| 384 | if have_comma: |
| 385 | self.write(', ') |
| 386 | else: |
| 387 | have_comma.append(True) |
| 388 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 389 | # if any of the given keyword arguments is a python keyword |
| 390 | # we have to make sure that no invalid call is created. |
| 391 | kwarg_workaround = False |
| 392 | for kwarg in chain((x.key for x in node.kwargs), extra_kwargs or ()): |
| 393 | if iskeyword(kwarg): |
| 394 | kwarg_workaround = True |
| 395 | break |
| 396 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 397 | for arg in node.args: |
| 398 | touch_comma() |
| 399 | self.visit(arg, frame) |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 400 | |
| 401 | if not kwarg_workaround: |
| 402 | for kwarg in node.kwargs: |
| 403 | touch_comma() |
| 404 | self.visit(kwarg, frame) |
| 405 | if extra_kwargs is not None: |
| 406 | for key, value in extra_kwargs.iteritems(): |
| 407 | touch_comma() |
| 408 | self.write('%s=%s' % (key, value)) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 409 | if node.dyn_args: |
| 410 | touch_comma() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 411 | self.write('*') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 412 | self.visit(node.dyn_args, frame) |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 413 | |
| 414 | if kwarg_workaround: |
| 415 | touch_comma() |
| 416 | if node.dyn_kwargs is not None: |
| 417 | self.write('**dict({') |
| 418 | else: |
| 419 | self.write('**{') |
| 420 | for kwarg in node.kwargs: |
| 421 | self.write('%r: ' % kwarg.key) |
| 422 | self.visit(kwarg.value, frame) |
| 423 | self.write(', ') |
| 424 | if extra_kwargs is not None: |
| 425 | for key, value in extra_kwargs.iteritems(): |
| 426 | touch_comma() |
| 427 | self.write('%r: %s, ' % (key, value)) |
| 428 | if node.dyn_kwargs is not None: |
| 429 | self.write('}, **') |
| 430 | self.visit(node.dyn_kwargs, frame) |
| 431 | self.write(')') |
| 432 | else: |
| 433 | self.write('}') |
| 434 | |
| 435 | elif node.dyn_kwargs is not None: |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 436 | touch_comma() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 437 | self.write('**') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 438 | self.visit(node.dyn_kwargs, frame) |
| 439 | |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 440 | def pull_locals(self, frame, indent=True): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 441 | """Pull all the references identifiers into the local scope. |
| 442 | This affects regular names, filters and tests. If indent is |
| 443 | set to False, no automatic indentation will take place. |
| 444 | """ |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 445 | if indent: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 446 | self.indent() |
| 447 | for name in frame.identifiers.undeclared: |
| 448 | self.writeline('l_%s = context[%r]' % (name, name)) |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 449 | for name in frame.identifiers.filters: |
| 450 | self.writeline('f_%s = environment.filters[%r]' % (name, name)) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 451 | for name in frame.identifiers.tests: |
| 452 | self.writeline('t_%s = environment.tests[%r]' % (name, name)) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 453 | if indent: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 454 | self.outdent() |
| 455 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 456 | def collect_shadowed(self, frame): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 457 | """This function returns all the shadowed variables in a dict |
| 458 | in the form name: alias and will write the required assignments |
| 459 | into the current scope. No indentation takes place. |
| 460 | """ |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 461 | # make sure we "backup" overridden, local identifiers |
| 462 | # TODO: we should probably optimize this and check if the |
| 463 | # identifier is in use afterwards. |
| 464 | aliases = {} |
| 465 | for name in frame.identifiers.find_shadowed(): |
| 466 | aliases[name] = ident = self.temporary_identifier() |
| 467 | self.writeline('%s = l_%s' % (ident, name)) |
| 468 | return aliases |
| 469 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 470 | def function_scoping(self, node, frame): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 471 | """In Jinja a few statements require the help of anonymous |
| 472 | functions. Those are currently macros and call blocks and in |
| 473 | the future also recursive loops. As there is currently |
| 474 | technical limitation that doesn't allow reading and writing a |
| 475 | variable in a scope where the initial value is coming from an |
| 476 | outer scope, this function tries to fall back with a common |
| 477 | error message. Additionally the frame passed is modified so |
| 478 | that the argumetns are collected and callers are looked up. |
| 479 | |
| 480 | This will return the modified frame. |
| 481 | """ |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 482 | func_frame = frame.inner() |
| 483 | func_frame.inspect(node.iter_child_nodes(), hard_scope=True) |
| 484 | |
| 485 | # variables that are undeclared (accessed before declaration) and |
| 486 | # declared locally *and* part of an outside scope raise a template |
| 487 | # assertion error. Reason: we can't generate reasonable code from |
| 488 | # it without aliasing all the variables. XXX: alias them ^^ |
| 489 | overriden_closure_vars = ( |
| 490 | func_frame.identifiers.undeclared & |
| 491 | func_frame.identifiers.declared & |
| 492 | (func_frame.identifiers.declared_locally | |
| 493 | func_frame.identifiers.declared_parameter) |
| 494 | ) |
| 495 | if overriden_closure_vars: |
| 496 | vars = ', '.join(sorted(overriden_closure_vars)) |
| 497 | raise TemplateAssertionError('It\'s not possible to set and ' |
| 498 | 'access variables derived from ' |
| 499 | 'an outer scope! (affects: %s' % |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 500 | vars, node.lineno, self.name) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 501 | |
| 502 | # remove variables from a closure from the frame's undeclared |
| 503 | # identifiers. |
| 504 | func_frame.identifiers.undeclared -= ( |
| 505 | func_frame.identifiers.undeclared & |
| 506 | func_frame.identifiers.declared |
| 507 | ) |
| 508 | |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 509 | func_frame.accesses_kwargs = False |
| 510 | func_frame.accesses_varargs = False |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 511 | func_frame.accesses_caller = False |
| 512 | func_frame.arguments = args = ['l_' + x.name for x in node.args] |
| 513 | |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 514 | if 'caller' in func_frame.identifiers.undeclared: |
| 515 | func_frame.accesses_caller = True |
| 516 | func_frame.identifiers.add_special('caller') |
| 517 | args.append('l_caller') |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 518 | if 'kwargs' in func_frame.identifiers.undeclared: |
| 519 | func_frame.accesses_kwargs = True |
| 520 | func_frame.identifiers.add_special('kwargs') |
| 521 | args.append('l_kwargs') |
| 522 | if 'varargs' in func_frame.identifiers.undeclared: |
| 523 | func_frame.accesses_varargs = True |
| 524 | func_frame.identifiers.add_special('varargs') |
| 525 | args.append('l_varargs') |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 526 | return func_frame |
| 527 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 528 | # -- Visitors |
| 529 | |
| 530 | def visit_Template(self, node, frame=None): |
| 531 | assert frame is None, 'no root frame allowed' |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 532 | from jinja2.runtime import __all__ as exported |
| 533 | self.writeline('from jinja2.runtime import ' + ', '.join(exported)) |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 534 | self.writeline('name = %r' % self.name) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 535 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 536 | # do we have an extends tag at all? If not, we can save some |
| 537 | # overhead by just not processing any inheritance code. |
| 538 | have_extends = node.find(nodes.Extends) is not None |
| 539 | |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 540 | # find all blocks |
| 541 | for block in node.find_all(nodes.Block): |
| 542 | if block.name in self.blocks: |
| 543 | raise TemplateAssertionError('block %r defined twice' % |
| 544 | block.name, block.lineno, |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 545 | self.name) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 546 | self.blocks[block.name] = block |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 547 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 548 | # generate the root render function. |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 549 | self.writeline('def root(context, environment=environment):', extra=1) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 550 | if have_extends: |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 551 | self.indent() |
| 552 | self.writeline('parent_template = None') |
| 553 | self.outdent() |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 554 | |
| 555 | # process the root |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 556 | frame = Frame() |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 557 | frame.inspect(node.body, with_depenencies=True) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 558 | frame.toplevel = frame.rootlevel = True |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 559 | self.indent() |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 560 | self.pull_locals(frame, indent=False) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 561 | self.blockvisit(node.body, frame, indent=False) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 562 | self.outdent() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 563 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 564 | # make sure that the parent root is called. |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 565 | if have_extends: |
| 566 | if not self.has_known_extends: |
| 567 | self.indent() |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 568 | self.writeline('if parent_template is not None:') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 569 | self.indent() |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 570 | self.writeline('for event in parent_template.' |
| 571 | 'root_render_func(context):') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 572 | self.indent() |
| 573 | self.writeline('yield event') |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 574 | self.outdent(2 + (not self.has_known_extends)) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 575 | |
| 576 | # at this point we now have the blocks collected and can visit them too. |
| 577 | for name, block in self.blocks.iteritems(): |
| 578 | block_frame = Frame() |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 579 | block_frame.inspect(block.body, with_depenencies=True) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 580 | block_frame.block = name |
Armin Ronacher | 62f8a29 | 2008-04-13 23:18:05 +0200 | [diff] [blame] | 581 | block_frame.identifiers.add_special('super') |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 582 | block_frame.name_overrides['super'] = 'context.super(%r, ' \ |
| 583 | 'block_%s)' % (name, name) |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 584 | self.writeline('def block_%s(context, environment=environment):' |
| 585 | % name, block, 1) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 586 | self.pull_locals(block_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 587 | self.blockvisit(block.body, block_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 588 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 589 | self.writeline('blocks = {%s}' % ', '.join('%r: block_%s' % (x, x) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 590 | for x in self.blocks), |
| 591 | extra=1) |
| 592 | |
| 593 | # add a function that returns the debug info |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 594 | self.writeline('debug_info = %r' % '&'.join('%s=%s' % x for x |
| 595 | in self.debug_info)) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 596 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 597 | def visit_Block(self, node, frame): |
| 598 | """Call a block and register it for the template.""" |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 599 | level = 1 |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 600 | if frame.toplevel: |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 601 | # if we know that we are a child template, there is no need to |
| 602 | # check if we are one |
| 603 | if self.has_known_extends: |
| 604 | return |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 605 | if self.extends_so_far > 0: |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 606 | self.writeline('if parent_template is None:') |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 607 | self.indent() |
| 608 | level += 1 |
| 609 | self.writeline('for event in context.blocks[%r][-1](context):' % node.name) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 610 | self.indent() |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 611 | if frame.buffer is None: |
| 612 | self.writeline('yield event') |
| 613 | else: |
| 614 | self.writeline('%s.append(event)' % frame.buffer) |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 615 | self.outdent(level) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 616 | |
| 617 | def visit_Extends(self, node, frame): |
| 618 | """Calls the extender.""" |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 619 | if not frame.toplevel: |
| 620 | raise TemplateAssertionError('cannot use extend from a non ' |
| 621 | 'top-level scope', node.lineno, |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 622 | self.name) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 623 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 624 | # if the number of extends statements in general is zero so |
| 625 | # far, we don't have to add a check if something extended |
| 626 | # the template before this one. |
| 627 | if self.extends_so_far > 0: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 628 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 629 | # if we have a known extends we just add a template runtime |
| 630 | # error into the generated code. We could catch that at compile |
| 631 | # time too, but i welcome it not to confuse users by throwing the |
| 632 | # same error at different times just "because we can". |
| 633 | if not self.has_known_extends: |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 634 | self.writeline('if parent_template is not None:') |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 635 | self.indent() |
| 636 | self.writeline('raise TemplateRuntimeError(%r)' % |
| 637 | 'extended multiple times') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 638 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 639 | # if we have a known extends already we don't need that code here |
| 640 | # as we know that the template execution will end here. |
| 641 | if self.has_known_extends: |
| 642 | raise CompilerExit() |
| 643 | self.outdent() |
| 644 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 645 | self.writeline('parent_template = environment.get_template(', node, 1) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 646 | self.visit(node.template, frame) |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 647 | self.write(', %r)' % self.name) |
| 648 | self.writeline('for name, parent_block in parent_template.' |
| 649 | 'blocks.iteritems():') |
| 650 | self.indent() |
| 651 | self.writeline('context.blocks.setdefault(name, []).' |
| 652 | 'insert(0, parent_block)') |
| 653 | self.outdent() |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 654 | |
| 655 | # if this extends statement was in the root level we can take |
| 656 | # advantage of that information and simplify the generated code |
| 657 | # in the top level from this point onwards |
| 658 | self.has_known_extends = True |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 659 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 660 | # and now we have one more |
| 661 | self.extends_so_far += 1 |
| 662 | |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 663 | def visit_Include(self, node, frame): |
| 664 | """Handles includes.""" |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 665 | self.writeline('included_template = environment.get_template(', node) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 666 | self.visit(node.template, frame) |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 667 | self.write(', %r)' % self.name) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 668 | self.writeline('for event in included_template.root_render_func(' |
| 669 | 'included_template.new_context(context.get_root())):') |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 670 | self.indent() |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 671 | if frame.buffer is None: |
| 672 | self.writeline('yield event') |
| 673 | else: |
| 674 | self.writeline('%s.append(event)' % frame.buffer) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 675 | self.outdent() |
| 676 | |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 677 | def visit_Import(self, node, frame): |
| 678 | """Visit regular imports.""" |
| 679 | self.writeline('l_%s = ' % node.target, node) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 680 | if frame.toplevel: |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 681 | self.write('context.vars[%r] = ' % node.target) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 682 | self.write('environment.get_template(') |
| 683 | self.visit(node.template, frame) |
| 684 | self.write(', %r).include(context)' % self.name) |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 685 | if frame.toplevel: |
| 686 | self.writeline('context.exported_vars.discard(%r)' % node.target) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 687 | |
| 688 | def visit_FromImport(self, node, frame): |
| 689 | """Visit named imports.""" |
| 690 | self.newline(node) |
| 691 | self.write('included_template = environment.get_template(') |
| 692 | self.visit(node.template, frame) |
| 693 | self.write(', %r).include(context)' % self.name) |
| 694 | for name in node.names: |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 695 | if isinstance(name, tuple): |
| 696 | name, alias = name |
| 697 | else: |
| 698 | alias = name |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 699 | self.writeline('l_%s = getattr(included_template, ' |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 700 | '%r, missing)' % (alias, name)) |
| 701 | self.writeline('if l_%s is missing:' % alias) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 702 | self.indent() |
| 703 | self.writeline('l_%s = environment.undefined(%r %% ' |
| 704 | 'included_template.name)' % |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 705 | (alias, 'the template %r does not export ' |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 706 | 'the requested name ' + repr(name))) |
| 707 | self.outdent() |
| 708 | if frame.toplevel: |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 709 | self.writeline('context.vars[%r] = l_%s' % (alias, alias)) |
| 710 | self.writeline('context.exported_vars.discard(%r)' % alias) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 711 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 712 | def visit_For(self, node, frame): |
| 713 | loop_frame = frame.inner() |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 714 | loop_frame.inspect(node.iter_child_nodes(exclude=('iter',))) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 715 | extended_loop = bool(node.else_) or \ |
| 716 | 'loop' in loop_frame.identifiers.undeclared |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 717 | if extended_loop: |
| 718 | loop_frame.identifiers.add_special('loop') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 719 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 720 | aliases = self.collect_shadowed(loop_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 721 | self.pull_locals(loop_frame, indent=False) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 722 | if node.else_: |
| 723 | self.writeline('l_loop = None') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 724 | |
| 725 | self.newline(node) |
| 726 | self.writeline('for ') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 727 | self.visit(node.target, loop_frame) |
Armin Ronacher | 180a1bd | 2008-04-09 12:14:24 +0200 | [diff] [blame] | 728 | self.write(extended_loop and ', l_loop in LoopContext(' or ' in ') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 729 | |
| 730 | # the expression pointing to the parent loop. We make the |
| 731 | # undefined a bit more debug friendly at the same time. |
| 732 | parent_loop = 'loop' in aliases and aliases['loop'] \ |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 733 | or "environment.undefined(%r)" % "'loop' is undefined. " \ |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 734 | 'the filter section of a loop as well as the ' \ |
| 735 | 'else block doesn\'t have access to the special ' \ |
| 736 | "'loop' variable of the current loop. Because " \ |
| 737 | 'there is no parent loop it\'s undefined.' |
| 738 | |
| 739 | # if we have an extened loop and a node test, we filter in the |
| 740 | # "outer frame". |
| 741 | if extended_loop and node.test is not None: |
| 742 | self.write('(') |
| 743 | self.visit(node.target, loop_frame) |
| 744 | self.write(' for ') |
| 745 | self.visit(node.target, loop_frame) |
| 746 | self.write(' in ') |
| 747 | self.visit(node.iter, loop_frame) |
| 748 | self.write(' if (') |
| 749 | test_frame = loop_frame.copy() |
| 750 | test_frame.name_overrides['loop'] = parent_loop |
| 751 | self.visit(node.test, test_frame) |
| 752 | self.write('))') |
| 753 | |
| 754 | else: |
| 755 | self.visit(node.iter, loop_frame) |
| 756 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 757 | self.write(extended_loop and '):' or ':') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 758 | |
| 759 | # tests in not extended loops become a continue |
| 760 | if not extended_loop and node.test is not None: |
| 761 | self.indent() |
| 762 | self.writeline('if ') |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 763 | self.visit(node.test, loop_frame) |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 764 | self.write(':') |
| 765 | self.indent() |
| 766 | self.writeline('continue') |
| 767 | self.outdent(2) |
| 768 | |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 769 | self.blockvisit(node.body, loop_frame, force_generator=True) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 770 | |
| 771 | if node.else_: |
| 772 | self.writeline('if l_loop is None:') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 773 | self.indent() |
| 774 | self.writeline('l_loop = ' + parent_loop) |
| 775 | self.outdent() |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 776 | self.blockvisit(node.else_, loop_frame, force_generator=False) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 777 | |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 778 | # reset the aliases if there are any. |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 779 | for name, alias in aliases.iteritems(): |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 780 | self.writeline('l_%s = %s' % (name, alias)) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 781 | |
| 782 | def visit_If(self, node, frame): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 783 | if_frame = frame.soft() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 784 | self.writeline('if ', node) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 785 | self.visit(node.test, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 786 | self.write(':') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 787 | self.blockvisit(node.body, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 788 | if node.else_: |
| 789 | self.writeline('else:') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 790 | self.blockvisit(node.else_, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 791 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 792 | def visit_Macro(self, node, frame): |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 793 | macro_frame = self.function_scoping(node, frame) |
| 794 | args = macro_frame.arguments |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 795 | self.writeline('def macro(%s):' % ', '.join(args), node) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 796 | macro_frame.buffer = buf = self.temporary_identifier() |
| 797 | self.indent() |
| 798 | self.pull_locals(macro_frame, indent=False) |
| 799 | self.writeline('%s = []' % buf) |
| 800 | self.blockvisit(node.body, macro_frame, indent=False) |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 801 | self.writeline("return Markup(concat(%s))" % buf) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 802 | self.outdent() |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 803 | self.newline() |
| 804 | if frame.toplevel: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 805 | self.write('context.exported_vars.add(%r)' % node.name) |
| 806 | self.writeline('context.vars[%r] = ' % node.name) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 807 | arg_tuple = ', '.join(repr(x.name) for x in node.args) |
| 808 | if len(node.args) == 1: |
| 809 | arg_tuple += ',' |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 810 | self.write('l_%s = Macro(environment, macro, %r, (%s), (' % |
| 811 | (node.name, node.name, arg_tuple)) |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 812 | for arg in node.defaults: |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 813 | self.visit(arg, macro_frame) |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 814 | self.write(', ') |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 815 | self.write('), %s, %s, %s)' % ( |
| 816 | macro_frame.accesses_kwargs and '1' or '0', |
| 817 | macro_frame.accesses_varargs and '1' or '0', |
Armin Ronacher | 00d5d21 | 2008-04-13 01:10:18 +0200 | [diff] [blame] | 818 | macro_frame.accesses_caller and '1' or '0' |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 819 | )) |
| 820 | |
| 821 | def visit_CallBlock(self, node, frame): |
| 822 | call_frame = self.function_scoping(node, frame) |
| 823 | args = call_frame.arguments |
| 824 | self.writeline('def call(%s):' % ', '.join(args), node) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 825 | call_frame.buffer = buf = self.temporary_identifier() |
| 826 | self.indent() |
| 827 | self.pull_locals(call_frame, indent=False) |
| 828 | self.writeline('%s = []' % buf) |
| 829 | self.blockvisit(node.body, call_frame, indent=False) |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 830 | self.writeline("return Markup(concat(%s))" % buf) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 831 | self.outdent() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 832 | arg_tuple = ', '.join(repr(x.name) for x in node.args) |
| 833 | if len(node.args) == 1: |
| 834 | arg_tuple += ',' |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 835 | self.writeline('caller = Macro(environment, call, None, (%s), (' % |
| 836 | arg_tuple) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 837 | for arg in node.defaults: |
| 838 | self.visit(arg) |
| 839 | self.write(', ') |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 840 | self.write('), %s, %s, 0)' % ( |
| 841 | call_frame.accesses_kwargs and '1' or '0', |
| 842 | call_frame.accesses_varargs and '1' or '0' |
| 843 | )) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 844 | if frame.buffer is None: |
| 845 | self.writeline('yield ', node) |
| 846 | else: |
| 847 | self.writeline('%s.append(' % frame.buffer, node) |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 848 | self.visit_Call(node.call, call_frame, |
| 849 | extra_kwargs={'caller': 'caller'}) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 850 | if frame.buffer is not None: |
| 851 | self.write(')') |
| 852 | |
| 853 | def visit_FilterBlock(self, node, frame): |
| 854 | filter_frame = frame.inner() |
| 855 | filter_frame.inspect(node.iter_child_nodes()) |
| 856 | |
| 857 | aliases = self.collect_shadowed(filter_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 858 | self.pull_locals(filter_frame, indent=False) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 859 | filter_frame.buffer = buf = self.temporary_identifier() |
| 860 | |
| 861 | self.writeline('%s = []' % buf, node) |
| 862 | for child in node.body: |
| 863 | self.visit(child, filter_frame) |
| 864 | |
| 865 | if frame.buffer is None: |
| 866 | self.writeline('yield ', node) |
| 867 | else: |
| 868 | self.writeline('%s.append(' % frame.buffer, node) |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 869 | self.visit_Filter(node.filter, filter_frame, 'concat(%s)' % buf) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 870 | if frame.buffer is not None: |
| 871 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 872 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 873 | def visit_ExprStmt(self, node, frame): |
| 874 | self.newline(node) |
Armin Ronacher | 6ce170c | 2008-04-25 12:32:36 +0200 | [diff] [blame] | 875 | self.visit(node.node, frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 876 | |
| 877 | def visit_Output(self, node, frame): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 878 | # if we have a known extends statement, we don't output anything |
Armin Ronacher | 7a52df8 | 2008-04-11 13:58:22 +0200 | [diff] [blame] | 879 | if self.has_known_extends and frame.toplevel: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 880 | return |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 881 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 882 | self.newline(node) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 883 | if self.environment.finalize is unicode: |
| 884 | finalizer = 'unicode' |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 885 | have_finalizer = False |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 886 | else: |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 887 | finalizer = 'environment.finalize' |
| 888 | have_finalizer = True |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 889 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 890 | # if we are in the toplevel scope and there was already an extends |
| 891 | # statement we have to add a check that disables our yield(s) here |
| 892 | # so that they don't appear in the output. |
| 893 | outdent_later = False |
| 894 | if frame.toplevel and self.extends_so_far != 0: |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 895 | self.writeline('if parent_template is None:') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 896 | self.indent() |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 897 | outdent_later = True |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 898 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 899 | # try to evaluate as many chunks as possible into a static |
| 900 | # string at compile time. |
| 901 | body = [] |
| 902 | for child in node.nodes: |
| 903 | try: |
| 904 | const = unicode(child.as_const()) |
| 905 | except: |
| 906 | body.append(child) |
| 907 | continue |
| 908 | if body and isinstance(body[-1], list): |
| 909 | body[-1].append(const) |
| 910 | else: |
| 911 | body.append([const]) |
| 912 | |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 913 | # if we have less than 3 nodes or less than 6 and a buffer we |
| 914 | # yield or extend |
| 915 | if len(body) < 3 or (frame.buffer is not None and len(body) < 6): |
| 916 | if frame.buffer is not None: |
| 917 | self.writeline('%s.extend((' % frame.buffer) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 918 | for item in body: |
| 919 | if isinstance(item, list): |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 920 | val = repr(concat(item)) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 921 | if frame.buffer is None: |
| 922 | self.writeline('yield ' + val) |
| 923 | else: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 924 | self.write(val + ', ') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 925 | else: |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 926 | if frame.buffer is None: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 927 | self.writeline('yield ') |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 928 | self.write(finalizer + '(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 929 | self.visit(item, frame) |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 930 | self.write(')') |
| 931 | if frame.buffer is not None: |
| 932 | self.write(', ') |
| 933 | if frame.buffer is not None: |
| 934 | self.write('))') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 935 | |
| 936 | # otherwise we create a format string as this is faster in that case |
| 937 | else: |
| 938 | format = [] |
| 939 | arguments = [] |
| 940 | for item in body: |
| 941 | if isinstance(item, list): |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 942 | format.append(concat(item).replace('%', '%%')) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 943 | else: |
| 944 | format.append('%s') |
| 945 | arguments.append(item) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 946 | if frame.buffer is None: |
| 947 | self.writeline('yield ') |
| 948 | else: |
| 949 | self.writeline('%s.append(' % frame.buffer) |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 950 | self.write(repr(concat(format)) + ' % (') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 951 | idx = -1 |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 952 | self.indent() |
| 953 | for argument in arguments: |
| 954 | self.newline(argument) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 955 | if have_finalizer: |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 956 | self.write(finalizer + '(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 957 | self.visit(argument, frame) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 958 | if have_finalizer: |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 959 | self.write(')') |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 960 | self.write(',') |
| 961 | self.outdent() |
| 962 | self.writeline(')') |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 963 | if frame.buffer is not None: |
| 964 | self.write(')') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 965 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 966 | if outdent_later: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 967 | self.outdent() |
| 968 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 969 | def visit_Assign(self, node, frame): |
| 970 | self.newline(node) |
| 971 | # toplevel assignments however go into the local namespace and |
| 972 | # the current template's context. We create a copy of the frame |
| 973 | # here and add a set so that the Name visitor can add the assigned |
| 974 | # names here. |
| 975 | if frame.toplevel: |
| 976 | assignment_frame = frame.copy() |
| 977 | assignment_frame.assigned_names = set() |
| 978 | else: |
| 979 | assignment_frame = frame |
| 980 | self.visit(node.target, assignment_frame) |
| 981 | self.write(' = ') |
| 982 | self.visit(node.node, frame) |
Armin Ronacher | 9706fab | 2008-04-08 18:49:56 +0200 | [diff] [blame] | 983 | |
| 984 | # make sure toplevel assignments are added to the context. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 985 | if frame.toplevel: |
| 986 | for name in assignment_frame.assigned_names: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame^] | 987 | self.writeline('context.vars[%r] = l_%s' % (name, name)) |
| 988 | self.writeline('context.exported_vars.add(%r)' % name) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 989 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 990 | def visit_Name(self, node, frame): |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 991 | if node.ctx == 'store': |
| 992 | if frame.toplevel: |
| 993 | frame.assigned_names.add(node.name) |
| 994 | frame.name_overrides.pop(node.name, None) |
| 995 | elif node.ctx == 'load': |
| 996 | if node.name in frame.name_overrides: |
| 997 | self.write(frame.name_overrides[node.name]) |
| 998 | return |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 999 | self.write('l_' + node.name) |
| 1000 | |
| 1001 | def visit_Const(self, node, frame): |
| 1002 | val = node.value |
| 1003 | if isinstance(val, float): |
| 1004 | # XXX: add checks for infinity and nan |
| 1005 | self.write(str(val)) |
| 1006 | else: |
| 1007 | self.write(repr(val)) |
| 1008 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1009 | def visit_Tuple(self, node, frame): |
| 1010 | self.write('(') |
| 1011 | idx = -1 |
| 1012 | for idx, item in enumerate(node.items): |
| 1013 | if idx: |
| 1014 | self.write(', ') |
| 1015 | self.visit(item, frame) |
| 1016 | self.write(idx == 0 and ',)' or ')') |
| 1017 | |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 1018 | def visit_List(self, node, frame): |
| 1019 | self.write('[') |
| 1020 | for idx, item in enumerate(node.items): |
| 1021 | if idx: |
| 1022 | self.write(', ') |
| 1023 | self.visit(item, frame) |
| 1024 | self.write(']') |
| 1025 | |
| 1026 | def visit_Dict(self, node, frame): |
| 1027 | self.write('{') |
| 1028 | for idx, item in enumerate(node.items): |
| 1029 | if idx: |
| 1030 | self.write(', ') |
| 1031 | self.visit(item.key, frame) |
| 1032 | self.write(': ') |
| 1033 | self.visit(item.value, frame) |
| 1034 | self.write('}') |
| 1035 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1036 | def binop(operator): |
| 1037 | def visitor(self, node, frame): |
| 1038 | self.write('(') |
| 1039 | self.visit(node.left, frame) |
| 1040 | self.write(' %s ' % operator) |
| 1041 | self.visit(node.right, frame) |
| 1042 | self.write(')') |
| 1043 | return visitor |
| 1044 | |
| 1045 | def uaop(operator): |
| 1046 | def visitor(self, node, frame): |
| 1047 | self.write('(' + operator) |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 1048 | self.visit(node.node, frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1049 | self.write(')') |
| 1050 | return visitor |
| 1051 | |
| 1052 | visit_Add = binop('+') |
| 1053 | visit_Sub = binop('-') |
| 1054 | visit_Mul = binop('*') |
| 1055 | visit_Div = binop('/') |
| 1056 | visit_FloorDiv = binop('//') |
| 1057 | visit_Pow = binop('**') |
| 1058 | visit_Mod = binop('%') |
| 1059 | visit_And = binop('and') |
| 1060 | visit_Or = binop('or') |
| 1061 | visit_Pos = uaop('+') |
| 1062 | visit_Neg = uaop('-') |
| 1063 | visit_Not = uaop('not ') |
| 1064 | del binop, uaop |
| 1065 | |
| 1066 | def visit_Compare(self, node, frame): |
| 1067 | self.visit(node.expr, frame) |
| 1068 | for op in node.ops: |
| 1069 | self.visit(op, frame) |
| 1070 | |
| 1071 | def visit_Operand(self, node, frame): |
| 1072 | self.write(' %s ' % operators[node.op]) |
| 1073 | self.visit(node.expr, frame) |
| 1074 | |
| 1075 | def visit_Subscript(self, node, frame): |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1076 | if isinstance(node.arg, nodes.Slice): |
| 1077 | self.visit(node.node, frame) |
| 1078 | self.write('[') |
| 1079 | self.visit(node.arg, frame) |
| 1080 | self.write(']') |
| 1081 | return |
| 1082 | try: |
| 1083 | const = node.arg.as_const() |
| 1084 | have_const = True |
| 1085 | except nodes.Impossible: |
| 1086 | have_const = False |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1087 | self.write('environment.subscribe(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1088 | self.visit(node.node, frame) |
| 1089 | self.write(', ') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1090 | if have_const: |
| 1091 | self.write(repr(const)) |
| 1092 | else: |
| 1093 | self.visit(node.arg, frame) |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 1094 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1095 | |
| 1096 | def visit_Slice(self, node, frame): |
| 1097 | if node.start is not None: |
| 1098 | self.visit(node.start, frame) |
| 1099 | self.write(':') |
| 1100 | if node.stop is not None: |
| 1101 | self.visit(node.stop, frame) |
| 1102 | if node.step is not None: |
| 1103 | self.write(':') |
| 1104 | self.visit(node.step, frame) |
| 1105 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 1106 | def visit_Filter(self, node, frame, initial=None): |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 1107 | self.write('f_%s(' % node.name) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 1108 | func = self.environment.filters.get(node.name) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 1109 | if func is None: |
| 1110 | raise TemplateAssertionError('no filter named %r' % node.name, |
| 1111 | node.lineno, self.filename) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 1112 | if getattr(func, 'contextfilter', False): |
| 1113 | self.write('context, ') |
Armin Ronacher | 9a027f4 | 2008-04-17 11:13:40 +0200 | [diff] [blame] | 1114 | elif getattr(func, 'environmentfilter', False): |
| 1115 | self.write('environment, ') |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 1116 | if isinstance(node.node, nodes.Filter): |
| 1117 | self.visit_Filter(node.node, frame, initial) |
| 1118 | elif node.node is None: |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 1119 | self.write(initial) |
| 1120 | else: |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 1121 | self.visit(node.node, frame) |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 1122 | self.signature(node, frame) |
| 1123 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1124 | |
| 1125 | def visit_Test(self, node, frame): |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 1126 | self.write('t_%s(' % node.name) |
Armin Ronacher | 0611e49 | 2008-04-25 23:44:14 +0200 | [diff] [blame] | 1127 | if node.name not in self.environment.tests: |
| 1128 | raise TemplateAssertionError('no test named %r' % node.name, |
| 1129 | node.lineno, self.filename) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1130 | self.visit(node.node, frame) |
| 1131 | self.signature(node, frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1132 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1133 | |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 1134 | def visit_CondExpr(self, node, frame): |
| 1135 | if not have_condexpr: |
| 1136 | self.write('((') |
| 1137 | self.visit(node.test, frame) |
| 1138 | self.write(') and (') |
| 1139 | self.visit(node.expr1, frame) |
| 1140 | self.write(',) or (') |
| 1141 | self.visit(node.expr2, frame) |
| 1142 | self.write(',))[0]') |
| 1143 | else: |
| 1144 | self.write('(') |
| 1145 | self.visit(node.expr1, frame) |
| 1146 | self.write(' if ') |
| 1147 | self.visit(node.test, frame) |
| 1148 | self.write(' else ') |
| 1149 | self.visit(node.expr2, frame) |
| 1150 | self.write(')') |
| 1151 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 1152 | def visit_Call(self, node, frame, extra_kwargs=None): |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1153 | if self.environment.sandboxed: |
| 1154 | self.write('environment.call(') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1155 | self.visit(node.node, frame) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1156 | self.write(self.environment.sandboxed and ', ' or '(') |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 1157 | self.signature(node, frame, False, extra_kwargs) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1158 | self.write(')') |
| 1159 | |
| 1160 | def visit_Keyword(self, node, frame): |
Armin Ronacher | 2e9396b | 2008-04-16 14:21:57 +0200 | [diff] [blame] | 1161 | self.write(node.key + '=') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1162 | self.visit(node.value, frame) |