blob: faa8b4151853b0004d7b5ded3cfa6c8e7dcb4f95 [file] [log] [blame]
Armin Ronachere791c2a2008-04-07 18:39:54 +02001# -*- 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 Ronacher32a910f2008-04-26 23:21:03 +020011from time import time
Armin Ronacher8efc5222008-04-08 14:47:40 +020012from copy import copy
Armin Ronachere791c2a2008-04-07 18:39:54 +020013from random import randrange
Armin Ronacher2feed1d2008-04-26 16:26:52 +020014from keyword import iskeyword
Armin Ronachere791c2a2008-04-07 18:39:54 +020015from cStringIO import StringIO
Armin Ronacher2feed1d2008-04-26 16:26:52 +020016from itertools import chain
Armin Ronachere791c2a2008-04-07 18:39:54 +020017from jinja2 import nodes
18from jinja2.visitor import NodeVisitor, NodeTransformer
19from jinja2.exceptions import TemplateAssertionError
Armin Ronacher32a910f2008-04-26 23:21:03 +020020from jinja2.runtime import concat
Armin Ronacher5236d8c2008-04-17 11:23:16 +020021from jinja2.utils import Markup
Armin Ronachere791c2a2008-04-07 18:39:54 +020022
23
24operators = {
25 'eq': '==',
26 'ne': '!=',
27 'gt': '>',
28 'gteq': '>=',
29 'lt': '<',
30 'lteq': '<=',
31 'in': 'in',
32 'notin': 'not in'
33}
34
Armin Ronacher3d8b7842008-04-13 13:16:50 +020035try:
36 exec '(0 if 0 else 0)'
37except SyntaxError:
38 have_condexpr = False
39else:
40 have_condexpr = True
41
42
Armin Ronacher8e8d0712008-04-16 23:10:49 +020043def generate(node, environment, name, filename, stream=None):
Armin Ronacherbcb7c532008-04-11 16:30:34 +020044 """Generate the python source for a node tree."""
Armin Ronacher8e8d0712008-04-16 23:10:49 +020045 generator = CodeGenerator(environment, name, filename, stream)
Armin Ronachere791c2a2008-04-07 18:39:54 +020046 generator.visit(node)
47 if stream is None:
48 return generator.stream.getvalue()
49
50
Armin Ronacher4dfc9752008-04-09 15:03:29 +020051def has_safe_repr(value):
52 """Does the node have a safe representation?"""
Armin Ronacherd55ab532008-04-09 16:13:39 +020053 if value is None or value is NotImplemented or value is Ellipsis:
Armin Ronacher4dfc9752008-04-09 15:03:29 +020054 return True
Armin Ronacherd55ab532008-04-09 16:13:39 +020055 if isinstance(value, (bool, int, long, float, complex, basestring,
Armin Ronacher32a910f2008-04-26 23:21:03 +020056 xrange, Markup)):
Armin Ronacher4dfc9752008-04-09 15:03:29 +020057 return True
Armin Ronacherd55ab532008-04-09 16:13:39 +020058 if isinstance(value, (tuple, list, set, frozenset)):
Armin Ronacher4dfc9752008-04-09 15:03:29 +020059 for item in value:
60 if not has_safe_repr(item):
61 return False
62 return True
63 elif isinstance(value, dict):
64 for key, value in value.iteritems():
65 if not has_safe_repr(key):
66 return False
67 if not has_safe_repr(value):
68 return False
69 return True
70 return False
71
72
Armin Ronacherc9705c22008-04-27 21:28:03 +020073def find_undeclared(nodes, names):
74 """Check if the names passed are accessed undeclared. The return value
75 is a set of all the undeclared names from the sequence of names found.
76 """
77 visitor = UndeclaredNameVisitor(names)
78 try:
79 for node in nodes:
80 visitor.visit(node)
81 except VisitorExit:
82 pass
83 return visitor.undeclared
84
85
Armin Ronachere791c2a2008-04-07 18:39:54 +020086class Identifiers(object):
87 """Tracks the status of identifiers in frames."""
88
89 def __init__(self):
90 # variables that are known to be declared (probably from outer
91 # frames or because they are special for the frame)
92 self.declared = set()
93
Armin Ronacher10f3ba22008-04-18 11:30:37 +020094 # undeclared variables from outer scopes
95 self.outer_undeclared = set()
96
Armin Ronachere791c2a2008-04-07 18:39:54 +020097 # names that are accessed without being explicitly declared by
98 # this one or any of the outer scopes. Names can appear both in
99 # declared and undeclared.
100 self.undeclared = set()
101
102 # names that are declared locally
103 self.declared_locally = set()
104
105 # names that are declared by parameters
106 self.declared_parameter = set()
107
108 def add_special(self, name):
109 """Register a special name like `loop`."""
110 self.undeclared.discard(name)
111 self.declared.add(name)
112
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200113 def is_declared(self, name, local_only=False):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200114 """Check if a name is declared in this or an outer scope."""
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200115 if name in self.declared_locally or name in self.declared_parameter:
116 return True
117 if local_only:
118 return False
119 return name in self.declared
Armin Ronachere791c2a2008-04-07 18:39:54 +0200120
121 def find_shadowed(self):
122 """Find all the shadowed names."""
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200123 return (self.declared | self.outer_undeclared) & \
124 (self.declared_locally | self.declared_parameter)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200125
126
127class Frame(object):
Armin Ronacher75cfb862008-04-11 13:47:22 +0200128 """Holds compile time information for us."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200129
130 def __init__(self, parent=None):
131 self.identifiers = Identifiers()
Armin Ronacherfed44b52008-04-13 19:42:53 +0200132
Armin Ronacher75cfb862008-04-11 13:47:22 +0200133 # a toplevel frame is the root + soft frames such as if conditions.
Armin Ronacher8efc5222008-04-08 14:47:40 +0200134 self.toplevel = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200135
Armin Ronacher75cfb862008-04-11 13:47:22 +0200136 # the root frame is basically just the outermost frame, so no if
137 # conditions. This information is used to optimize inheritance
138 # situations.
139 self.rootlevel = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200140
141 # inside some tags we are using a buffer rather than yield statements.
142 # this for example affects {% filter %} or {% macro %}. If a frame
143 # is buffered this variable points to the name of the list used as
144 # buffer.
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200145 self.buffer = None
Armin Ronacherfed44b52008-04-13 19:42:53 +0200146
Armin Ronacherfed44b52008-04-13 19:42:53 +0200147 # the name of the block we're in, otherwise None.
Armin Ronacher8efc5222008-04-08 14:47:40 +0200148 self.block = parent and parent.block or None
Armin Ronacherfed44b52008-04-13 19:42:53 +0200149
150 # the parent of this frame
151 self.parent = parent
152
Armin Ronachere791c2a2008-04-07 18:39:54 +0200153 if parent is not None:
154 self.identifiers.declared.update(
155 parent.identifiers.declared |
Armin Ronachere791c2a2008-04-07 18:39:54 +0200156 parent.identifiers.declared_locally |
157 parent.identifiers.declared_parameter
158 )
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200159 self.identifiers.outer_undeclared.update(
160 parent.identifiers.undeclared -
161 self.identifiers.declared
162 )
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200163 self.buffer = parent.buffer
Armin Ronachere791c2a2008-04-07 18:39:54 +0200164
Armin Ronacher8efc5222008-04-08 14:47:40 +0200165 def copy(self):
166 """Create a copy of the current one."""
167 rv = copy(self)
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200168 rv.identifiers = copy(self.identifiers)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200169 return rv
170
Armin Ronacherc9705c22008-04-27 21:28:03 +0200171 def inspect(self, nodes, hard_scope=False):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200172 """Walk the node and check for identifiers. If the scope is hard (eg:
173 enforce on a python level) overrides from outer scopes are tracked
174 differently.
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200175 """
176 visitor = FrameIdentifierVisitor(self.identifiers, hard_scope)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200177 for node in nodes:
Armin Ronacherc9705c22008-04-27 21:28:03 +0200178 visitor.visit(node)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200179
180 def inner(self):
181 """Return an inner frame."""
182 return Frame(self)
183
Armin Ronacher75cfb862008-04-11 13:47:22 +0200184 def soft(self):
185 """Return a soft frame. A soft frame may not be modified as
186 standalone thing as it shares the resources with the frame it
187 was created of, but it's not a rootlevel frame any longer.
188 """
189 rv = copy(self)
190 rv.rootlevel = False
191 return rv
192
Armin Ronachere791c2a2008-04-07 18:39:54 +0200193
Armin Ronacherc9705c22008-04-27 21:28:03 +0200194class VisitorExit(RuntimeError):
195 """Exception used by the `UndeclaredNameVisitor` to signal a stop."""
196
197
198class DependencyFinderVisitor(NodeVisitor):
199 """A visitor that collects filter and test calls."""
200
201 def __init__(self):
202 self.filters = set()
203 self.tests = set()
204
205 def visit_Filter(self, node):
206 self.generic_visit(node)
207 self.filters.add(node.name)
208
209 def visit_Test(self, node):
210 self.generic_visit(node)
211 self.tests.add(node.name)
212
213 def visit_Block(self, node):
214 """Stop visiting at blocks."""
215
216
217class UndeclaredNameVisitor(NodeVisitor):
218 """A visitor that checks if a name is accessed without being
219 declared. This is different from the frame visitor as it will
220 not stop at closure frames.
221 """
222
223 def __init__(self, names):
224 self.names = set(names)
225 self.undeclared = set()
226
227 def visit_Name(self, node):
228 if node.ctx == 'load' and node.name in self.names:
229 self.undeclared.add(node.name)
230 if self.undeclared == self.names:
231 raise VisitorExit()
232 else:
233 self.names.discard(node.name)
234
235 def visit_Block(self, node):
236 """Stop visiting a blocks."""
237
238
Armin Ronachere791c2a2008-04-07 18:39:54 +0200239class FrameIdentifierVisitor(NodeVisitor):
240 """A visitor for `Frame.inspect`."""
241
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200242 def __init__(self, identifiers, hard_scope):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200243 self.identifiers = identifiers
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200244 self.hard_scope = hard_scope
Armin Ronachere791c2a2008-04-07 18:39:54 +0200245
Armin Ronacherc9705c22008-04-27 21:28:03 +0200246 def visit_Name(self, node):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200247 """All assignments to names go through this function."""
Armin Ronacherc9705c22008-04-27 21:28:03 +0200248 if node.ctx in ('store', 'param'):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200249 self.identifiers.declared_locally.add(node.name)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200250 elif node.ctx == 'load' and not \
251 self.identifiers.is_declared(node.name, self.hard_scope):
252 self.identifiers.undeclared.add(node.name)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200253
Armin Ronacherc9705c22008-04-27 21:28:03 +0200254 def visit_Macro(self, node):
255 self.generic_visit(node)
256 self.identifiers.declared_locally.add(node.name)
Armin Ronacher0611e492008-04-25 23:44:14 +0200257
Armin Ronacherc9705c22008-04-27 21:28:03 +0200258 def visit_Import(self, node):
259 self.generic_visit(node)
260 self.identifiers.declared_locally.add(node.target)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200261
Armin Ronacherc9705c22008-04-27 21:28:03 +0200262 def visit_FromImport(self, node):
263 self.generic_visit(node)
264 for name in node.names:
265 if isinstance(name, tuple):
266 self.identifiers.declared_locally.add(name[1])
267 else:
268 self.identifiers.declared_locally.add(name)
269
270 def visit_Assign(self, node):
Armin Ronacherebe55aa2008-04-10 20:51:23 +0200271 """Visit assignments in the correct order."""
Armin Ronacherc9705c22008-04-27 21:28:03 +0200272 self.visit(node.node)
273 self.visit(node.target)
Armin Ronacherebe55aa2008-04-10 20:51:23 +0200274
Armin Ronacherc9705c22008-04-27 21:28:03 +0200275 def visit_For(self, node):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200276 """Visiting stops at for blocks. However the block sequence
277 is visited as part of the outer scope.
278 """
Armin Ronacherc9705c22008-04-27 21:28:03 +0200279 self.visit(node.iter)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200280
Armin Ronacherc9705c22008-04-27 21:28:03 +0200281 def visit_CallBlock(self, node):
282 for child in node.iter_child_nodes(exclude=('body',)):
283 self.visit(child)
284
285 def visit_FilterBlock(self, node):
286 self.visit(node.filter)
287
288 def visit_Block(self, node):
289 """Stop visiting at blocks."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200290
291
Armin Ronacher75cfb862008-04-11 13:47:22 +0200292class CompilerExit(Exception):
293 """Raised if the compiler encountered a situation where it just
294 doesn't make sense to further process the code. Any block that
Armin Ronacher0611e492008-04-25 23:44:14 +0200295 raises such an exception is not further processed.
296 """
Armin Ronacher75cfb862008-04-11 13:47:22 +0200297
298
Armin Ronachere791c2a2008-04-07 18:39:54 +0200299class CodeGenerator(NodeVisitor):
300
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200301 def __init__(self, environment, name, filename, stream=None):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200302 if stream is None:
303 stream = StringIO()
Christoph Hack65642a52008-04-08 14:46:56 +0200304 self.environment = environment
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200305 self.name = name
Armin Ronachere791c2a2008-04-07 18:39:54 +0200306 self.filename = filename
307 self.stream = stream
Armin Ronacherfed44b52008-04-13 19:42:53 +0200308
309 # a registry for all blocks. Because blocks are moved out
310 # into the global python scope they are registered here
Armin Ronachere791c2a2008-04-07 18:39:54 +0200311 self.blocks = {}
Armin Ronacherfed44b52008-04-13 19:42:53 +0200312
313 # the number of extends statements so far
Armin Ronacher7fb38972008-04-11 13:54:28 +0200314 self.extends_so_far = 0
Armin Ronacherfed44b52008-04-13 19:42:53 +0200315
316 # some templates have a rootlevel extends. In this case we
317 # can safely assume that we're a child template and do some
318 # more optimizations.
Armin Ronacher75cfb862008-04-11 13:47:22 +0200319 self.has_known_extends = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200320
Armin Ronacherba3757b2008-04-16 19:43:16 +0200321 # the current line number
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200322 self.code_lineno = 1
Armin Ronacherba3757b2008-04-16 19:43:16 +0200323
324 # the debug information
325 self.debug_info = []
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200326 self._write_debug_info = None
Armin Ronacherba3757b2008-04-16 19:43:16 +0200327
Armin Ronacherfed44b52008-04-13 19:42:53 +0200328 # the number of new lines before the next write()
329 self._new_lines = 0
330
331 # the line number of the last written statement
Armin Ronachere791c2a2008-04-07 18:39:54 +0200332 self._last_line = 0
Armin Ronacherfed44b52008-04-13 19:42:53 +0200333
334 # true if nothing was written so far.
Armin Ronachere791c2a2008-04-07 18:39:54 +0200335 self._first_write = True
336
Armin Ronacherfed44b52008-04-13 19:42:53 +0200337 # used by the `temporary_identifier` method to get new
338 # unique, temporary identifier
339 self._last_identifier = 0
340
341 # the current indentation
342 self._indentation = 0
343
Armin Ronachere791c2a2008-04-07 18:39:54 +0200344 def temporary_identifier(self):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200345 """Get a new unique identifier."""
346 self._last_identifier += 1
347 return 't%d' % self._last_identifier
Armin Ronachere791c2a2008-04-07 18:39:54 +0200348
349 def indent(self):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200350 """Indent by one."""
351 self._indentation += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +0200352
Armin Ronacher8efc5222008-04-08 14:47:40 +0200353 def outdent(self, step=1):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200354 """Outdent by step."""
355 self._indentation -= step
Armin Ronachere791c2a2008-04-07 18:39:54 +0200356
Armin Ronacherc9705c22008-04-27 21:28:03 +0200357 def blockvisit(self, nodes, frame, force_generator=True):
358 """Visit a list of nodes as block in a frame. If the current frame
359 is no buffer a dummy ``if 0: yield None`` is written automatically
360 unless the force_generator parameter is set to False.
Armin Ronacherfed44b52008-04-13 19:42:53 +0200361 """
Armin Ronacher625215e2008-04-13 16:31:08 +0200362 if frame.buffer is None and force_generator:
Armin Ronachere791c2a2008-04-07 18:39:54 +0200363 self.writeline('if 0: yield None')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200364 try:
365 for node in nodes:
366 self.visit(node, frame)
367 except CompilerExit:
368 pass
Armin Ronachere791c2a2008-04-07 18:39:54 +0200369
370 def write(self, x):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200371 """Write a string into the output stream."""
372 if self._new_lines:
Armin Ronachere791c2a2008-04-07 18:39:54 +0200373 if not self._first_write:
Armin Ronacherfed44b52008-04-13 19:42:53 +0200374 self.stream.write('\n' * self._new_lines)
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200375 self.code_lineno += self._new_lines
376 if self._write_debug_info is not None:
377 self.debug_info.append((self._write_debug_info,
378 self.code_lineno))
379 self._write_debug_info = None
Armin Ronachere791c2a2008-04-07 18:39:54 +0200380 self._first_write = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200381 self.stream.write(' ' * self._indentation)
382 self._new_lines = 0
Armin Ronachere791c2a2008-04-07 18:39:54 +0200383 self.stream.write(x)
384
385 def writeline(self, x, node=None, extra=0):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200386 """Combination of newline and write."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200387 self.newline(node, extra)
388 self.write(x)
389
390 def newline(self, node=None, extra=0):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200391 """Add one or more newlines before the next write."""
392 self._new_lines = max(self._new_lines, 1 + extra)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200393 if node is not None and node.lineno != self._last_line:
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200394 self._write_debug_info = node.lineno
395 self._last_line = node.lineno
Armin Ronachere791c2a2008-04-07 18:39:54 +0200396
Armin Ronacher71082072008-04-12 14:19:36 +0200397 def signature(self, node, frame, have_comma=True, extra_kwargs=None):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200398 """Writes a function call to the stream for the current node.
399 Per default it will write a leading comma but this can be
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200400 disabled by setting have_comma to False. The extra keyword
401 arguments may not include python keywords otherwise a syntax
402 error could occour. The extra keyword arguments should be given
403 as python dict.
Armin Ronacherfed44b52008-04-13 19:42:53 +0200404 """
Armin Ronacher8efc5222008-04-08 14:47:40 +0200405 have_comma = have_comma and [True] or []
406 def touch_comma():
407 if have_comma:
408 self.write(', ')
409 else:
410 have_comma.append(True)
411
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200412 # if any of the given keyword arguments is a python keyword
413 # we have to make sure that no invalid call is created.
414 kwarg_workaround = False
415 for kwarg in chain((x.key for x in node.kwargs), extra_kwargs or ()):
416 if iskeyword(kwarg):
417 kwarg_workaround = True
418 break
419
Armin Ronacher8efc5222008-04-08 14:47:40 +0200420 for arg in node.args:
421 touch_comma()
422 self.visit(arg, frame)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200423
424 if not kwarg_workaround:
425 for kwarg in node.kwargs:
426 touch_comma()
427 self.visit(kwarg, frame)
428 if extra_kwargs is not None:
429 for key, value in extra_kwargs.iteritems():
430 touch_comma()
431 self.write('%s=%s' % (key, value))
Armin Ronacher8efc5222008-04-08 14:47:40 +0200432 if node.dyn_args:
433 touch_comma()
Armin Ronacher71082072008-04-12 14:19:36 +0200434 self.write('*')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200435 self.visit(node.dyn_args, frame)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200436
437 if kwarg_workaround:
438 touch_comma()
439 if node.dyn_kwargs is not None:
440 self.write('**dict({')
441 else:
442 self.write('**{')
443 for kwarg in node.kwargs:
444 self.write('%r: ' % kwarg.key)
445 self.visit(kwarg.value, frame)
446 self.write(', ')
447 if extra_kwargs is not None:
448 for key, value in extra_kwargs.iteritems():
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200449 self.write('%r: %s, ' % (key, value))
450 if node.dyn_kwargs is not None:
451 self.write('}, **')
452 self.visit(node.dyn_kwargs, frame)
453 self.write(')')
454 else:
455 self.write('}')
456
457 elif node.dyn_kwargs is not None:
Armin Ronacher8efc5222008-04-08 14:47:40 +0200458 touch_comma()
Armin Ronacher71082072008-04-12 14:19:36 +0200459 self.write('**')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200460 self.visit(node.dyn_kwargs, frame)
461
Armin Ronacherc9705c22008-04-27 21:28:03 +0200462 def pull_locals(self, frame):
463 """Pull all the references identifiers into the local scope."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200464 for name in frame.identifiers.undeclared:
465 self.writeline('l_%s = context[%r]' % (name, name))
Armin Ronacherc9705c22008-04-27 21:28:03 +0200466
467 def pull_dependencies(self, nodes):
468 """Pull all the dependencies."""
469 visitor = DependencyFinderVisitor()
470 for node in nodes:
471 visitor.visit(node)
472 for name in visitor.filters:
Armin Ronacherd4c64f72008-04-11 17:15:29 +0200473 self.writeline('f_%s = environment.filters[%r]' % (name, name))
Armin Ronacherc9705c22008-04-27 21:28:03 +0200474 for name in visitor.tests:
Armin Ronacherf059ec12008-04-11 22:21:00 +0200475 self.writeline('t_%s = environment.tests[%r]' % (name, name))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200476
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200477 def collect_shadowed(self, frame):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200478 """This function returns all the shadowed variables in a dict
479 in the form name: alias and will write the required assignments
480 into the current scope. No indentation takes place.
481 """
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200482 # make sure we "backup" overridden, local identifiers
483 # TODO: we should probably optimize this and check if the
484 # identifier is in use afterwards.
485 aliases = {}
486 for name in frame.identifiers.find_shadowed():
487 aliases[name] = ident = self.temporary_identifier()
488 self.writeline('%s = l_%s' % (ident, name))
489 return aliases
490
Armin Ronacherc9705c22008-04-27 21:28:03 +0200491 def function_scoping(self, node, frame, children=None):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200492 """In Jinja a few statements require the help of anonymous
493 functions. Those are currently macros and call blocks and in
494 the future also recursive loops. As there is currently
495 technical limitation that doesn't allow reading and writing a
496 variable in a scope where the initial value is coming from an
497 outer scope, this function tries to fall back with a common
498 error message. Additionally the frame passed is modified so
499 that the argumetns are collected and callers are looked up.
500
501 This will return the modified frame.
502 """
Armin Ronacherc9705c22008-04-27 21:28:03 +0200503 # we have to iterate twice over it, make sure that works
504 if children is None:
505 children = node.iter_child_nodes()
506 children = list(children)
Armin Ronacher71082072008-04-12 14:19:36 +0200507 func_frame = frame.inner()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200508 func_frame.inspect(children, hard_scope=True)
Armin Ronacher71082072008-04-12 14:19:36 +0200509
510 # variables that are undeclared (accessed before declaration) and
511 # declared locally *and* part of an outside scope raise a template
512 # assertion error. Reason: we can't generate reasonable code from
513 # it without aliasing all the variables. XXX: alias them ^^
514 overriden_closure_vars = (
515 func_frame.identifiers.undeclared &
516 func_frame.identifiers.declared &
517 (func_frame.identifiers.declared_locally |
518 func_frame.identifiers.declared_parameter)
519 )
520 if overriden_closure_vars:
521 vars = ', '.join(sorted(overriden_closure_vars))
522 raise TemplateAssertionError('It\'s not possible to set and '
523 'access variables derived from '
524 'an outer scope! (affects: %s' %
Armin Ronacher68f77672008-04-17 11:50:39 +0200525 vars, node.lineno, self.name)
Armin Ronacher71082072008-04-12 14:19:36 +0200526
527 # remove variables from a closure from the frame's undeclared
528 # identifiers.
529 func_frame.identifiers.undeclared -= (
530 func_frame.identifiers.undeclared &
531 func_frame.identifiers.declared
532 )
533
Armin Ronacher963f97d2008-04-25 11:44:59 +0200534 func_frame.accesses_kwargs = False
535 func_frame.accesses_varargs = False
Armin Ronacher71082072008-04-12 14:19:36 +0200536 func_frame.accesses_caller = False
537 func_frame.arguments = args = ['l_' + x.name for x in node.args]
538
Armin Ronacherc9705c22008-04-27 21:28:03 +0200539 undeclared = find_undeclared(children, ('caller', 'kwargs', 'varargs'))
540
541 if 'caller' in undeclared:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200542 func_frame.accesses_caller = True
543 func_frame.identifiers.add_special('caller')
544 args.append('l_caller')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200545 if 'kwargs' in undeclared:
Armin Ronacher963f97d2008-04-25 11:44:59 +0200546 func_frame.accesses_kwargs = True
547 func_frame.identifiers.add_special('kwargs')
548 args.append('l_kwargs')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200549 if 'varargs' in undeclared:
Armin Ronacher963f97d2008-04-25 11:44:59 +0200550 func_frame.accesses_varargs = True
551 func_frame.identifiers.add_special('varargs')
552 args.append('l_varargs')
Armin Ronacher71082072008-04-12 14:19:36 +0200553 return func_frame
554
Armin Ronachere791c2a2008-04-07 18:39:54 +0200555 # -- Visitors
556
557 def visit_Template(self, node, frame=None):
558 assert frame is None, 'no root frame allowed'
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200559 from jinja2.runtime import __all__ as exported
Armin Ronacher709f6e52008-04-28 18:18:16 +0200560 self.writeline('from __future__ import division')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200561 self.writeline('from jinja2.runtime import ' + ', '.join(exported))
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200562 self.writeline('name = %r' % self.name)
Armin Ronacher8edbe492008-04-10 20:43:43 +0200563
Armin Ronacher75cfb862008-04-11 13:47:22 +0200564 # do we have an extends tag at all? If not, we can save some
565 # overhead by just not processing any inheritance code.
566 have_extends = node.find(nodes.Extends) is not None
567
Armin Ronacher8edbe492008-04-10 20:43:43 +0200568 # find all blocks
569 for block in node.find_all(nodes.Block):
570 if block.name in self.blocks:
571 raise TemplateAssertionError('block %r defined twice' %
572 block.name, block.lineno,
Armin Ronacher68f77672008-04-17 11:50:39 +0200573 self.name)
Armin Ronacher8edbe492008-04-10 20:43:43 +0200574 self.blocks[block.name] = block
Armin Ronachere791c2a2008-04-07 18:39:54 +0200575
Armin Ronacher8efc5222008-04-08 14:47:40 +0200576 # generate the root render function.
Armin Ronacher32a910f2008-04-26 23:21:03 +0200577 self.writeline('def root(context, environment=environment):', extra=1)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200578
579 # process the root
Armin Ronachere791c2a2008-04-07 18:39:54 +0200580 frame = Frame()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200581 frame.inspect(node.body)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200582 frame.toplevel = frame.rootlevel = True
Armin Ronacherf059ec12008-04-11 22:21:00 +0200583 self.indent()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200584 if have_extends:
585 self.writeline('parent_template = None')
586 self.pull_locals(frame)
587 self.pull_dependencies(node.body)
588 if 'self' in find_undeclared(node.body, ('self',)):
589 frame.identifiers.add_special('self')
590 self.writeline('l_self = TemplateReference(context)')
591 self.blockvisit(node.body, frame)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200592 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200593
Armin Ronacher8efc5222008-04-08 14:47:40 +0200594 # make sure that the parent root is called.
Armin Ronacher75cfb862008-04-11 13:47:22 +0200595 if have_extends:
596 if not self.has_known_extends:
597 self.indent()
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200598 self.writeline('if parent_template is not None:')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200599 self.indent()
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200600 self.writeline('for event in parent_template.'
601 'root_render_func(context):')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200602 self.indent()
603 self.writeline('yield event')
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200604 self.outdent(2 + (not self.has_known_extends))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200605
606 # at this point we now have the blocks collected and can visit them too.
607 for name, block in self.blocks.iteritems():
608 block_frame = Frame()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200609 block_frame.inspect(block.body)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200610 block_frame.block = name
Armin Ronacherd4c64f72008-04-11 17:15:29 +0200611 self.writeline('def block_%s(context, environment=environment):'
612 % name, block, 1)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200613 self.indent()
614 undeclared = find_undeclared(block.body, ('self', 'super'))
615 if 'self' in undeclared:
616 block_frame.identifiers.add_special('self')
617 self.writeline('l_self = TemplateReference(context)')
618 if 'super' in undeclared:
619 block_frame.identifiers.add_special('super')
620 self.writeline('l_super = context.super(%r, '
621 'block_%s)' % (name, name))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200622 self.pull_locals(block_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200623 self.pull_dependencies(block.body)
Armin Ronacher625215e2008-04-13 16:31:08 +0200624 self.blockvisit(block.body, block_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200625 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200626
Armin Ronacher75cfb862008-04-11 13:47:22 +0200627 self.writeline('blocks = {%s}' % ', '.join('%r: block_%s' % (x, x)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200628 for x in self.blocks),
629 extra=1)
630
631 # add a function that returns the debug info
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200632 self.writeline('debug_info = %r' % '&'.join('%s=%s' % x for x
633 in self.debug_info))
Armin Ronacher75cfb862008-04-11 13:47:22 +0200634
Armin Ronachere791c2a2008-04-07 18:39:54 +0200635 def visit_Block(self, node, frame):
636 """Call a block and register it for the template."""
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200637 level = 1
Armin Ronacher75cfb862008-04-11 13:47:22 +0200638 if frame.toplevel:
Armin Ronacherbcb7c532008-04-11 16:30:34 +0200639 # if we know that we are a child template, there is no need to
640 # check if we are one
641 if self.has_known_extends:
642 return
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200643 if self.extends_so_far > 0:
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200644 self.writeline('if parent_template is None:')
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200645 self.indent()
646 level += 1
Armin Ronacherc9705c22008-04-27 21:28:03 +0200647 self.writeline('for event in context.blocks[%r][-1](context):' %
648 node.name, node)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200649 self.indent()
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200650 if frame.buffer is None:
651 self.writeline('yield event')
652 else:
653 self.writeline('%s.append(event)' % frame.buffer)
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200654 self.outdent(level)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200655
656 def visit_Extends(self, node, frame):
657 """Calls the extender."""
Armin Ronacher8efc5222008-04-08 14:47:40 +0200658 if not frame.toplevel:
659 raise TemplateAssertionError('cannot use extend from a non '
660 'top-level scope', node.lineno,
Armin Ronacher68f77672008-04-17 11:50:39 +0200661 self.name)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200662
Armin Ronacher7fb38972008-04-11 13:54:28 +0200663 # if the number of extends statements in general is zero so
664 # far, we don't have to add a check if something extended
665 # the template before this one.
666 if self.extends_so_far > 0:
Armin Ronacher75cfb862008-04-11 13:47:22 +0200667
Armin Ronacher7fb38972008-04-11 13:54:28 +0200668 # if we have a known extends we just add a template runtime
669 # error into the generated code. We could catch that at compile
670 # time too, but i welcome it not to confuse users by throwing the
671 # same error at different times just "because we can".
672 if not self.has_known_extends:
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200673 self.writeline('if parent_template is not None:')
Armin Ronacher7fb38972008-04-11 13:54:28 +0200674 self.indent()
675 self.writeline('raise TemplateRuntimeError(%r)' %
676 'extended multiple times')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200677
Armin Ronacher7fb38972008-04-11 13:54:28 +0200678 # if we have a known extends already we don't need that code here
679 # as we know that the template execution will end here.
680 if self.has_known_extends:
681 raise CompilerExit()
682 self.outdent()
683
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200684 self.writeline('parent_template = environment.get_template(', node, 1)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200685 self.visit(node.template, frame)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200686 self.write(', %r)' % self.name)
687 self.writeline('for name, parent_block in parent_template.'
688 'blocks.iteritems():')
689 self.indent()
690 self.writeline('context.blocks.setdefault(name, []).'
691 'insert(0, parent_block)')
692 self.outdent()
Armin Ronacher75cfb862008-04-11 13:47:22 +0200693
694 # if this extends statement was in the root level we can take
695 # advantage of that information and simplify the generated code
696 # in the top level from this point onwards
697 self.has_known_extends = True
Armin Ronachere791c2a2008-04-07 18:39:54 +0200698
Armin Ronacher7fb38972008-04-11 13:54:28 +0200699 # and now we have one more
700 self.extends_so_far += 1
701
Armin Ronacherf059ec12008-04-11 22:21:00 +0200702 def visit_Include(self, node, frame):
703 """Handles includes."""
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200704 self.writeline('included_template = environment.get_template(', node)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200705 self.visit(node.template, frame)
Armin Ronacher963f97d2008-04-25 11:44:59 +0200706 self.write(', %r)' % self.name)
Armin Ronacher0611e492008-04-25 23:44:14 +0200707 self.writeline('for event in included_template.root_render_func('
Armin Ronacherc9705c22008-04-27 21:28:03 +0200708 'included_template.new_context(context.parent, True)):')
Armin Ronacherf059ec12008-04-11 22:21:00 +0200709 self.indent()
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200710 if frame.buffer is None:
711 self.writeline('yield event')
712 else:
713 self.writeline('%s.append(event)' % frame.buffer)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200714 self.outdent()
715
Armin Ronacher0611e492008-04-25 23:44:14 +0200716 def visit_Import(self, node, frame):
717 """Visit regular imports."""
718 self.writeline('l_%s = ' % node.target, node)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200719 if frame.toplevel:
Armin Ronacher53042292008-04-26 18:30:19 +0200720 self.write('context.vars[%r] = ' % node.target)
Armin Ronacher0611e492008-04-25 23:44:14 +0200721 self.write('environment.get_template(')
722 self.visit(node.template, frame)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200723 self.write(', %r).module' % self.name)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200724 if frame.toplevel and not node.target.startswith('__'):
Armin Ronacher53042292008-04-26 18:30:19 +0200725 self.writeline('context.exported_vars.discard(%r)' % node.target)
Armin Ronacher0611e492008-04-25 23:44:14 +0200726
727 def visit_FromImport(self, node, frame):
728 """Visit named imports."""
729 self.newline(node)
730 self.write('included_template = environment.get_template(')
731 self.visit(node.template, frame)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200732 self.write(', %r).module' % self.name)
Armin Ronacher0611e492008-04-25 23:44:14 +0200733 for name in node.names:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200734 if isinstance(name, tuple):
735 name, alias = name
736 else:
737 alias = name
Armin Ronacher0611e492008-04-25 23:44:14 +0200738 self.writeline('l_%s = getattr(included_template, '
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200739 '%r, missing)' % (alias, name))
740 self.writeline('if l_%s is missing:' % alias)
Armin Ronacher0611e492008-04-25 23:44:14 +0200741 self.indent()
742 self.writeline('l_%s = environment.undefined(%r %% '
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200743 'included_template.name, '
744 'name=included_template.name)' %
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200745 (alias, 'the template %r does not export '
Armin Ronacher0611e492008-04-25 23:44:14 +0200746 'the requested name ' + repr(name)))
747 self.outdent()
748 if frame.toplevel:
Armin Ronacher53042292008-04-26 18:30:19 +0200749 self.writeline('context.vars[%r] = l_%s' % (alias, alias))
Armin Ronacherc9705c22008-04-27 21:28:03 +0200750 if not alias.startswith('__'):
751 self.writeline('context.exported_vars.discard(%r)' % alias)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200752
Armin Ronachere791c2a2008-04-07 18:39:54 +0200753 def visit_For(self, node, frame):
754 loop_frame = frame.inner()
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200755 loop_frame.inspect(node.iter_child_nodes(exclude=('iter',)))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200756 extended_loop = bool(node.else_) or \
757 'loop' in loop_frame.identifiers.undeclared
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200758 if extended_loop:
759 loop_frame.identifiers.add_special('loop')
Armin Ronachere791c2a2008-04-07 18:39:54 +0200760
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200761 aliases = self.collect_shadowed(loop_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200762 self.pull_locals(loop_frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200763 if node.else_:
764 self.writeline('l_loop = None')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200765
766 self.newline(node)
767 self.writeline('for ')
Armin Ronachere791c2a2008-04-07 18:39:54 +0200768 self.visit(node.target, loop_frame)
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200769 self.write(extended_loop and ', l_loop in LoopContext(' or ' in ')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200770
771 # the expression pointing to the parent loop. We make the
772 # undefined a bit more debug friendly at the same time.
773 parent_loop = 'loop' in aliases and aliases['loop'] \
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200774 or "environment.undefined(%r, name='loop')" % "'loop' " \
775 'is undefined. "the filter section of a loop as well ' \
776 'as the else block doesn\'t have access to the ' \
777 "special 'loop' variable of the current loop. " \
778 "Because there is no parent loop it's undefined."
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200779
780 # if we have an extened loop and a node test, we filter in the
781 # "outer frame".
782 if extended_loop and node.test is not None:
783 self.write('(')
784 self.visit(node.target, loop_frame)
785 self.write(' for ')
786 self.visit(node.target, loop_frame)
787 self.write(' in ')
788 self.visit(node.iter, loop_frame)
789 self.write(' if (')
790 test_frame = loop_frame.copy()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200791 self.writeline('l_loop = ' + parent_loop)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200792 self.visit(node.test, test_frame)
793 self.write('))')
794
795 else:
796 self.visit(node.iter, loop_frame)
797
Armin Ronachere791c2a2008-04-07 18:39:54 +0200798 self.write(extended_loop and '):' or ':')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200799
800 # tests in not extended loops become a continue
801 if not extended_loop and node.test is not None:
802 self.indent()
803 self.writeline('if ')
Armin Ronacher32a910f2008-04-26 23:21:03 +0200804 self.visit(node.test, loop_frame)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200805 self.write(':')
806 self.indent()
807 self.writeline('continue')
808 self.outdent(2)
809
Armin Ronacherc9705c22008-04-27 21:28:03 +0200810 self.indent()
Armin Ronacherbe4ae242008-04-18 09:49:08 +0200811 self.blockvisit(node.body, loop_frame, force_generator=True)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200812 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200813
814 if node.else_:
815 self.writeline('if l_loop is None:')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200816 self.indent()
817 self.writeline('l_loop = ' + parent_loop)
Armin Ronacher625215e2008-04-13 16:31:08 +0200818 self.blockvisit(node.else_, loop_frame, force_generator=False)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200819 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200820
Armin Ronacherd4c64f72008-04-11 17:15:29 +0200821 # reset the aliases if there are any.
Armin Ronachere791c2a2008-04-07 18:39:54 +0200822 for name, alias in aliases.iteritems():
Armin Ronacher8efc5222008-04-08 14:47:40 +0200823 self.writeline('l_%s = %s' % (name, alias))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200824
825 def visit_If(self, node, frame):
Armin Ronacher75cfb862008-04-11 13:47:22 +0200826 if_frame = frame.soft()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200827 self.writeline('if ', node)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200828 self.visit(node.test, if_frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200829 self.write(':')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200830 self.indent()
Armin Ronacher75cfb862008-04-11 13:47:22 +0200831 self.blockvisit(node.body, if_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200832 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200833 if node.else_:
834 self.writeline('else:')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200835 self.indent()
Armin Ronacher75cfb862008-04-11 13:47:22 +0200836 self.blockvisit(node.else_, if_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200837 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200838
Armin Ronacher8efc5222008-04-08 14:47:40 +0200839 def visit_Macro(self, node, frame):
Armin Ronacher71082072008-04-12 14:19:36 +0200840 macro_frame = self.function_scoping(node, frame)
841 args = macro_frame.arguments
Armin Ronacher8efc5222008-04-08 14:47:40 +0200842 self.writeline('def macro(%s):' % ', '.join(args), node)
Armin Ronacher625215e2008-04-13 16:31:08 +0200843 macro_frame.buffer = buf = self.temporary_identifier()
844 self.indent()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200845 self.pull_locals(macro_frame)
Armin Ronacher625215e2008-04-13 16:31:08 +0200846 self.writeline('%s = []' % buf)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200847 self.blockvisit(node.body, macro_frame)
Armin Ronacherd1342312008-04-28 12:20:12 +0200848 if self.environment.autoescape:
849 self.writeline('return Markup(concat(%s))' % buf)
850 else:
851 self.writeline("return concat(%s)" % buf)
Armin Ronacher625215e2008-04-13 16:31:08 +0200852 self.outdent()
Armin Ronacher8efc5222008-04-08 14:47:40 +0200853 self.newline()
854 if frame.toplevel:
Armin Ronacherc9705c22008-04-27 21:28:03 +0200855 if not node.name.startswith('__'):
856 self.write('context.exported_vars.add(%r)' % node.name)
Armin Ronacher32a910f2008-04-26 23:21:03 +0200857 self.writeline('context.vars[%r] = ' % node.name)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200858 arg_tuple = ', '.join(repr(x.name) for x in node.args)
859 if len(node.args) == 1:
860 arg_tuple += ','
Armin Ronacherc63243e2008-04-14 22:53:58 +0200861 self.write('l_%s = Macro(environment, macro, %r, (%s), (' %
862 (node.name, node.name, arg_tuple))
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200863 for arg in node.defaults:
Armin Ronacher625215e2008-04-13 16:31:08 +0200864 self.visit(arg, macro_frame)
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200865 self.write(', ')
Armin Ronacher963f97d2008-04-25 11:44:59 +0200866 self.write('), %s, %s, %s)' % (
867 macro_frame.accesses_kwargs and '1' or '0',
868 macro_frame.accesses_varargs and '1' or '0',
Armin Ronacher00d5d212008-04-13 01:10:18 +0200869 macro_frame.accesses_caller and '1' or '0'
Armin Ronacher71082072008-04-12 14:19:36 +0200870 ))
871
872 def visit_CallBlock(self, node, frame):
Armin Ronacherc9705c22008-04-27 21:28:03 +0200873 call_frame = self.function_scoping(node, frame, node.iter_child_nodes
874 (exclude=('call',)))
Armin Ronacher71082072008-04-12 14:19:36 +0200875 args = call_frame.arguments
876 self.writeline('def call(%s):' % ', '.join(args), node)
Armin Ronacher625215e2008-04-13 16:31:08 +0200877 call_frame.buffer = buf = self.temporary_identifier()
878 self.indent()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200879 self.pull_locals(call_frame)
Armin Ronacher625215e2008-04-13 16:31:08 +0200880 self.writeline('%s = []' % buf)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200881 self.blockvisit(node.body, call_frame)
Armin Ronacherd1342312008-04-28 12:20:12 +0200882 if self.environment.autoescape:
883 self.writeline("return Markup(concat(%s))" % buf)
884 else:
885 self.writeline('return concat(%s)' % buf)
Armin Ronacher625215e2008-04-13 16:31:08 +0200886 self.outdent()
Armin Ronacher71082072008-04-12 14:19:36 +0200887 arg_tuple = ', '.join(repr(x.name) for x in node.args)
888 if len(node.args) == 1:
889 arg_tuple += ','
Armin Ronacherc63243e2008-04-14 22:53:58 +0200890 self.writeline('caller = Macro(environment, call, None, (%s), (' %
891 arg_tuple)
Armin Ronacher71082072008-04-12 14:19:36 +0200892 for arg in node.defaults:
Armin Ronacherc9705c22008-04-27 21:28:03 +0200893 self.visit(arg, call_frame)
Armin Ronacher71082072008-04-12 14:19:36 +0200894 self.write(', ')
Armin Ronacher963f97d2008-04-25 11:44:59 +0200895 self.write('), %s, %s, 0)' % (
896 call_frame.accesses_kwargs and '1' or '0',
897 call_frame.accesses_varargs and '1' or '0'
898 ))
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200899 if frame.buffer is None:
900 self.writeline('yield ', node)
901 else:
902 self.writeline('%s.append(' % frame.buffer, node)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200903 self.visit_Call(node.call, call_frame,
904 extra_kwargs={'caller': 'caller'})
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200905 if frame.buffer is not None:
906 self.write(')')
907
908 def visit_FilterBlock(self, node, frame):
909 filter_frame = frame.inner()
910 filter_frame.inspect(node.iter_child_nodes())
911
912 aliases = self.collect_shadowed(filter_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200913 self.pull_locals(filter_frame)
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200914 filter_frame.buffer = buf = self.temporary_identifier()
915
916 self.writeline('%s = []' % buf, node)
917 for child in node.body:
918 self.visit(child, filter_frame)
919
920 if frame.buffer is None:
921 self.writeline('yield ', node)
922 else:
923 self.writeline('%s.append(' % frame.buffer, node)
Armin Ronacherde6bf712008-04-26 01:44:14 +0200924 self.visit_Filter(node.filter, filter_frame, 'concat(%s)' % buf)
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200925 if frame.buffer is not None:
926 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200927
Armin Ronachere791c2a2008-04-07 18:39:54 +0200928 def visit_ExprStmt(self, node, frame):
929 self.newline(node)
Armin Ronacher6ce170c2008-04-25 12:32:36 +0200930 self.visit(node.node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200931
932 def visit_Output(self, node, frame):
Armin Ronacher75cfb862008-04-11 13:47:22 +0200933 # if we have a known extends statement, we don't output anything
Armin Ronacher7a52df82008-04-11 13:58:22 +0200934 if self.has_known_extends and frame.toplevel:
Armin Ronacher75cfb862008-04-11 13:47:22 +0200935 return
Armin Ronachere791c2a2008-04-07 18:39:54 +0200936
Armin Ronacher75cfb862008-04-11 13:47:22 +0200937 self.newline(node)
Armin Ronacher8edbe492008-04-10 20:43:43 +0200938
Armin Ronacher7fb38972008-04-11 13:54:28 +0200939 # if we are in the toplevel scope and there was already an extends
940 # statement we have to add a check that disables our yield(s) here
941 # so that they don't appear in the output.
942 outdent_later = False
943 if frame.toplevel and self.extends_so_far != 0:
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200944 self.writeline('if parent_template is None:')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200945 self.indent()
Armin Ronacher7fb38972008-04-11 13:54:28 +0200946 outdent_later = True
Armin Ronacher75cfb862008-04-11 13:47:22 +0200947
Armin Ronachere791c2a2008-04-07 18:39:54 +0200948 # try to evaluate as many chunks as possible into a static
949 # string at compile time.
950 body = []
951 for child in node.nodes:
952 try:
953 const = unicode(child.as_const())
954 except:
955 body.append(child)
956 continue
957 if body and isinstance(body[-1], list):
958 body[-1].append(const)
959 else:
960 body.append([const])
961
Armin Ronacher32a910f2008-04-26 23:21:03 +0200962 # if we have less than 3 nodes or less than 6 and a buffer we
963 # yield or extend
964 if len(body) < 3 or (frame.buffer is not None and len(body) < 6):
965 if frame.buffer is not None:
966 self.writeline('%s.extend((' % frame.buffer)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200967 for item in body:
968 if isinstance(item, list):
Armin Ronacherde6bf712008-04-26 01:44:14 +0200969 val = repr(concat(item))
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200970 if frame.buffer is None:
971 self.writeline('yield ' + val)
972 else:
Armin Ronacher32a910f2008-04-26 23:21:03 +0200973 self.write(val + ', ')
Armin Ronachere791c2a2008-04-07 18:39:54 +0200974 else:
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200975 if frame.buffer is None:
Armin Ronacher32a910f2008-04-26 23:21:03 +0200976 self.writeline('yield ')
Armin Ronacherd1342312008-04-28 12:20:12 +0200977 close = 1
978 if self.environment.autoescape:
979 self.write('escape(')
980 else:
981 self.write('unicode(')
982 if self.environment.finalize is not None:
983 self.write('environment.finalize(')
984 close += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +0200985 self.visit(item, frame)
Armin Ronacherd1342312008-04-28 12:20:12 +0200986 self.write(')' * close)
Armin Ronacher32a910f2008-04-26 23:21:03 +0200987 if frame.buffer is not None:
988 self.write(', ')
989 if frame.buffer is not None:
990 self.write('))')
Armin Ronachere791c2a2008-04-07 18:39:54 +0200991
992 # otherwise we create a format string as this is faster in that case
993 else:
994 format = []
995 arguments = []
996 for item in body:
997 if isinstance(item, list):
Armin Ronacherde6bf712008-04-26 01:44:14 +0200998 format.append(concat(item).replace('%', '%%'))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200999 else:
1000 format.append('%s')
1001 arguments.append(item)
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001002 if frame.buffer is None:
1003 self.writeline('yield ')
1004 else:
1005 self.writeline('%s.append(' % frame.buffer)
Armin Ronacherde6bf712008-04-26 01:44:14 +02001006 self.write(repr(concat(format)) + ' % (')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001007 idx = -1
Armin Ronacher8e8d0712008-04-16 23:10:49 +02001008 self.indent()
1009 for argument in arguments:
1010 self.newline(argument)
Armin Ronacherd1342312008-04-28 12:20:12 +02001011 close = 0
1012 if self.environment.autoescape:
1013 self.write('escape(')
1014 close += 1
1015 if self.environment.finalize is not None:
1016 self.write('environment.finalize(')
1017 close += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +02001018 self.visit(argument, frame)
Armin Ronacherd1342312008-04-28 12:20:12 +02001019 self.write(')' * close + ',')
Armin Ronacher8e8d0712008-04-16 23:10:49 +02001020 self.outdent()
1021 self.writeline(')')
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001022 if frame.buffer is not None:
1023 self.write(')')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001024
Armin Ronacher7fb38972008-04-11 13:54:28 +02001025 if outdent_later:
Armin Ronacher75cfb862008-04-11 13:47:22 +02001026 self.outdent()
1027
Armin Ronacher8efc5222008-04-08 14:47:40 +02001028 def visit_Assign(self, node, frame):
1029 self.newline(node)
1030 # toplevel assignments however go into the local namespace and
1031 # the current template's context. We create a copy of the frame
1032 # here and add a set so that the Name visitor can add the assigned
1033 # names here.
1034 if frame.toplevel:
1035 assignment_frame = frame.copy()
1036 assignment_frame.assigned_names = set()
1037 else:
1038 assignment_frame = frame
1039 self.visit(node.target, assignment_frame)
1040 self.write(' = ')
1041 self.visit(node.node, frame)
Armin Ronacher9706fab2008-04-08 18:49:56 +02001042
1043 # make sure toplevel assignments are added to the context.
Armin Ronacher8efc5222008-04-08 14:47:40 +02001044 if frame.toplevel:
1045 for name in assignment_frame.assigned_names:
Armin Ronacher32a910f2008-04-26 23:21:03 +02001046 self.writeline('context.vars[%r] = l_%s' % (name, name))
Armin Ronacherc9705c22008-04-27 21:28:03 +02001047 if not name.startswith('__'):
1048 self.writeline('context.exported_vars.add(%r)' % name)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001049
Armin Ronachere791c2a2008-04-07 18:39:54 +02001050 def visit_Name(self, node, frame):
Armin Ronacherc9705c22008-04-27 21:28:03 +02001051 if node.ctx == 'store' and frame.toplevel:
1052 frame.assigned_names.add(node.name)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001053 self.write('l_' + node.name)
1054
Armin Ronacherd84ec462008-04-29 13:43:16 +02001055 def visit_MarkSafe(self, node, frame):
1056 self.write('Markup(')
1057 self.visit(node.expr, frame)
1058 self.write(')')
1059
Armin Ronachere791c2a2008-04-07 18:39:54 +02001060 def visit_Const(self, node, frame):
1061 val = node.value
1062 if isinstance(val, float):
1063 # XXX: add checks for infinity and nan
1064 self.write(str(val))
1065 else:
1066 self.write(repr(val))
1067
Armin Ronacher8efc5222008-04-08 14:47:40 +02001068 def visit_Tuple(self, node, frame):
1069 self.write('(')
1070 idx = -1
1071 for idx, item in enumerate(node.items):
1072 if idx:
1073 self.write(', ')
1074 self.visit(item, frame)
1075 self.write(idx == 0 and ',)' or ')')
1076
Armin Ronacher8edbe492008-04-10 20:43:43 +02001077 def visit_List(self, node, frame):
1078 self.write('[')
1079 for idx, item in enumerate(node.items):
1080 if idx:
1081 self.write(', ')
1082 self.visit(item, frame)
1083 self.write(']')
1084
1085 def visit_Dict(self, node, frame):
1086 self.write('{')
1087 for idx, item in enumerate(node.items):
1088 if idx:
1089 self.write(', ')
1090 self.visit(item.key, frame)
1091 self.write(': ')
1092 self.visit(item.value, frame)
1093 self.write('}')
1094
Armin Ronachere791c2a2008-04-07 18:39:54 +02001095 def binop(operator):
1096 def visitor(self, node, frame):
1097 self.write('(')
1098 self.visit(node.left, frame)
1099 self.write(' %s ' % operator)
1100 self.visit(node.right, frame)
1101 self.write(')')
1102 return visitor
1103
1104 def uaop(operator):
1105 def visitor(self, node, frame):
1106 self.write('(' + operator)
Armin Ronacher9a822052008-04-17 18:44:07 +02001107 self.visit(node.node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001108 self.write(')')
1109 return visitor
1110
1111 visit_Add = binop('+')
1112 visit_Sub = binop('-')
1113 visit_Mul = binop('*')
1114 visit_Div = binop('/')
1115 visit_FloorDiv = binop('//')
1116 visit_Pow = binop('**')
1117 visit_Mod = binop('%')
1118 visit_And = binop('and')
1119 visit_Or = binop('or')
1120 visit_Pos = uaop('+')
1121 visit_Neg = uaop('-')
1122 visit_Not = uaop('not ')
1123 del binop, uaop
1124
Armin Ronacherd1342312008-04-28 12:20:12 +02001125 def visit_Concat(self, node, frame):
Armin Ronacher709f6e52008-04-28 18:18:16 +02001126 self.write('%s((' % self.environment.autoescape and
1127 'markup_join' or 'unicode_join')
Armin Ronacherd1342312008-04-28 12:20:12 +02001128 for arg in node.nodes:
1129 self.visit(arg, frame)
1130 self.write(', ')
1131 self.write('))')
1132
Armin Ronachere791c2a2008-04-07 18:39:54 +02001133 def visit_Compare(self, node, frame):
1134 self.visit(node.expr, frame)
1135 for op in node.ops:
1136 self.visit(op, frame)
1137
1138 def visit_Operand(self, node, frame):
1139 self.write(' %s ' % operators[node.op])
1140 self.visit(node.expr, frame)
1141
1142 def visit_Subscript(self, node, frame):
Armin Ronacher8efc5222008-04-08 14:47:40 +02001143 if isinstance(node.arg, nodes.Slice):
1144 self.visit(node.node, frame)
1145 self.write('[')
1146 self.visit(node.arg, frame)
1147 self.write(']')
1148 return
1149 try:
1150 const = node.arg.as_const()
1151 have_const = True
1152 except nodes.Impossible:
1153 have_const = False
Armin Ronacherc63243e2008-04-14 22:53:58 +02001154 self.write('environment.subscribe(')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001155 self.visit(node.node, frame)
1156 self.write(', ')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001157 if have_const:
1158 self.write(repr(const))
1159 else:
1160 self.visit(node.arg, frame)
Armin Ronacher4dfc9752008-04-09 15:03:29 +02001161 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001162
1163 def visit_Slice(self, node, frame):
1164 if node.start is not None:
1165 self.visit(node.start, frame)
1166 self.write(':')
1167 if node.stop is not None:
1168 self.visit(node.stop, frame)
1169 if node.step is not None:
1170 self.write(':')
1171 self.visit(node.step, frame)
1172
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001173 def visit_Filter(self, node, frame, initial=None):
Armin Ronacherd4c64f72008-04-11 17:15:29 +02001174 self.write('f_%s(' % node.name)
Christoph Hack80909862008-04-14 01:35:10 +02001175 func = self.environment.filters.get(node.name)
Armin Ronacher0611e492008-04-25 23:44:14 +02001176 if func is None:
1177 raise TemplateAssertionError('no filter named %r' % node.name,
1178 node.lineno, self.filename)
Christoph Hack80909862008-04-14 01:35:10 +02001179 if getattr(func, 'contextfilter', False):
1180 self.write('context, ')
Armin Ronacher9a027f42008-04-17 11:13:40 +02001181 elif getattr(func, 'environmentfilter', False):
1182 self.write('environment, ')
Christoph Hack80909862008-04-14 01:35:10 +02001183 if isinstance(node.node, nodes.Filter):
1184 self.visit_Filter(node.node, frame, initial)
1185 elif node.node is None:
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001186 self.write(initial)
1187 else:
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001188 self.visit(node.node, frame)
Armin Ronacherd55ab532008-04-09 16:13:39 +02001189 self.signature(node, frame)
1190 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001191
1192 def visit_Test(self, node, frame):
Armin Ronacherf059ec12008-04-11 22:21:00 +02001193 self.write('t_%s(' % node.name)
Armin Ronacher0611e492008-04-25 23:44:14 +02001194 if node.name not in self.environment.tests:
1195 raise TemplateAssertionError('no test named %r' % node.name,
1196 node.lineno, self.filename)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001197 self.visit(node.node, frame)
1198 self.signature(node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001199 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001200
Armin Ronacher3d8b7842008-04-13 13:16:50 +02001201 def visit_CondExpr(self, node, frame):
1202 if not have_condexpr:
1203 self.write('((')
1204 self.visit(node.test, frame)
1205 self.write(') and (')
1206 self.visit(node.expr1, frame)
1207 self.write(',) or (')
1208 self.visit(node.expr2, frame)
1209 self.write(',))[0]')
1210 else:
1211 self.write('(')
1212 self.visit(node.expr1, frame)
1213 self.write(' if ')
1214 self.visit(node.test, frame)
1215 self.write(' else ')
1216 self.visit(node.expr2, frame)
1217 self.write(')')
1218
Armin Ronacher71082072008-04-12 14:19:36 +02001219 def visit_Call(self, node, frame, extra_kwargs=None):
Armin Ronacherc63243e2008-04-14 22:53:58 +02001220 if self.environment.sandboxed:
1221 self.write('environment.call(')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001222 self.visit(node.node, frame)
Armin Ronacherc63243e2008-04-14 22:53:58 +02001223 self.write(self.environment.sandboxed and ', ' or '(')
Armin Ronacher71082072008-04-12 14:19:36 +02001224 self.signature(node, frame, False, extra_kwargs)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001225 self.write(')')
1226
1227 def visit_Keyword(self, node, frame):
Armin Ronacher2e9396b2008-04-16 14:21:57 +02001228 self.write(node.key + '=')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001229 self.visit(node.value, frame)