blob: 6bd9428b6deee626087a52ef6d72f4f325ee6eec [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.
Armin Ronacherd7764372008-07-15 00:11:14 +02009 :license: BSD.
Armin Ronachere791c2a2008-04-07 18:39:54 +020010"""
Armin Ronachere791c2a2008-04-07 18:39:54 +020011from cStringIO import StringIO
Armin Ronacherd1ff8582008-05-11 00:30:43 +020012from itertools import chain
Armin Ronachere791c2a2008-04-07 18:39:54 +020013from jinja2 import nodes
14from jinja2.visitor import NodeVisitor, NodeTransformer
15from jinja2.exceptions import TemplateAssertionError
Armin Ronacher9a0078d2008-08-13 18:24:17 +020016from jinja2.utils import Markup, concat, escape, is_python_keyword
Armin Ronachere791c2a2008-04-07 18:39:54 +020017
18
19operators = {
20 'eq': '==',
21 'ne': '!=',
22 'gt': '>',
23 'gteq': '>=',
24 'lt': '<',
25 'lteq': '<=',
26 'in': 'in',
27 'notin': 'not in'
28}
29
Armin Ronacher3d8b7842008-04-13 13:16:50 +020030try:
31 exec '(0 if 0 else 0)'
32except SyntaxError:
33 have_condexpr = False
34else:
35 have_condexpr = True
36
37
Armin Ronacher8e8d0712008-04-16 23:10:49 +020038def generate(node, environment, name, filename, stream=None):
Armin Ronacherbcb7c532008-04-11 16:30:34 +020039 """Generate the python source for a node tree."""
Armin Ronacher023b5e92008-05-08 11:03:10 +020040 if not isinstance(node, nodes.Template):
41 raise TypeError('Can\'t compile non template nodes')
Armin Ronacher8e8d0712008-04-16 23:10:49 +020042 generator = CodeGenerator(environment, name, filename, stream)
Armin Ronachere791c2a2008-04-07 18:39:54 +020043 generator.visit(node)
44 if stream is None:
45 return generator.stream.getvalue()
46
47
Armin Ronacher4dfc9752008-04-09 15:03:29 +020048def has_safe_repr(value):
49 """Does the node have a safe representation?"""
Armin Ronacherd55ab532008-04-09 16:13:39 +020050 if value is None or value is NotImplemented or value is Ellipsis:
Armin Ronacher4dfc9752008-04-09 15:03:29 +020051 return True
Armin Ronacherd55ab532008-04-09 16:13:39 +020052 if isinstance(value, (bool, int, long, float, complex, basestring,
Armin Ronacher32a910f2008-04-26 23:21:03 +020053 xrange, Markup)):
Armin Ronacher4dfc9752008-04-09 15:03:29 +020054 return True
Armin Ronacherd55ab532008-04-09 16:13:39 +020055 if isinstance(value, (tuple, list, set, frozenset)):
Armin Ronacher4dfc9752008-04-09 15:03:29 +020056 for item in value:
57 if not has_safe_repr(item):
58 return False
59 return True
60 elif isinstance(value, dict):
61 for key, value in value.iteritems():
62 if not has_safe_repr(key):
63 return False
64 if not has_safe_repr(value):
65 return False
66 return True
67 return False
68
69
Armin Ronacherc9705c22008-04-27 21:28:03 +020070def find_undeclared(nodes, names):
71 """Check if the names passed are accessed undeclared. The return value
72 is a set of all the undeclared names from the sequence of names found.
73 """
74 visitor = UndeclaredNameVisitor(names)
75 try:
76 for node in nodes:
77 visitor.visit(node)
78 except VisitorExit:
79 pass
80 return visitor.undeclared
81
82
Armin Ronachere791c2a2008-04-07 18:39:54 +020083class Identifiers(object):
84 """Tracks the status of identifiers in frames."""
85
86 def __init__(self):
87 # variables that are known to be declared (probably from outer
88 # frames or because they are special for the frame)
89 self.declared = set()
90
Armin Ronacher10f3ba22008-04-18 11:30:37 +020091 # undeclared variables from outer scopes
92 self.outer_undeclared = set()
93
Armin Ronachere791c2a2008-04-07 18:39:54 +020094 # names that are accessed without being explicitly declared by
95 # this one or any of the outer scopes. Names can appear both in
96 # declared and undeclared.
97 self.undeclared = set()
98
99 # names that are declared locally
100 self.declared_locally = set()
101
102 # names that are declared by parameters
103 self.declared_parameter = set()
104
105 def add_special(self, name):
106 """Register a special name like `loop`."""
107 self.undeclared.discard(name)
108 self.declared.add(name)
109
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200110 def is_declared(self, name, local_only=False):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200111 """Check if a name is declared in this or an outer scope."""
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200112 if name in self.declared_locally or name in self.declared_parameter:
113 return True
114 if local_only:
115 return False
116 return name in self.declared
Armin Ronachere791c2a2008-04-07 18:39:54 +0200117
Armin Ronacherff53c782008-08-13 18:55:50 +0200118 def find_shadowed(self, extra=()):
119 """Find all the shadowed names. extra is an iterable of variables
120 that may be defined with `add_special` which may occour scoped.
121 """
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200122 return (self.declared | self.outer_undeclared) & \
Armin Ronacherff53c782008-08-13 18:55:50 +0200123 (self.declared_locally | self.declared_parameter) | \
124 set(x for x in extra if self.is_declared(x))
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 |
Armin Ronacherb3a1fcf2008-05-15 11:04:14 +0200157 parent.identifiers.declared_parameter |
158 parent.identifiers.undeclared
Armin Ronachere791c2a2008-04-07 18:39:54 +0200159 )
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200160 self.identifiers.outer_undeclared.update(
161 parent.identifiers.undeclared -
162 self.identifiers.declared
163 )
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200164 self.buffer = parent.buffer
Armin Ronachere791c2a2008-04-07 18:39:54 +0200165
Armin Ronacher8efc5222008-04-08 14:47:40 +0200166 def copy(self):
167 """Create a copy of the current one."""
Armin Ronacher9a0078d2008-08-13 18:24:17 +0200168 rv = object.__new__(self.__class__)
169 rv.__dict__.update(self.__dict__)
170 rv.identifiers = object.__new__(self.identifiers.__class__)
171 rv.identifiers.__dict__.update(self.identifiers.__dict__)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200172 return rv
173
Armin Ronacherc9705c22008-04-27 21:28:03 +0200174 def inspect(self, nodes, hard_scope=False):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200175 """Walk the node and check for identifiers. If the scope is hard (eg:
176 enforce on a python level) overrides from outer scopes are tracked
177 differently.
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200178 """
179 visitor = FrameIdentifierVisitor(self.identifiers, hard_scope)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200180 for node in nodes:
Armin Ronacherc9705c22008-04-27 21:28:03 +0200181 visitor.visit(node)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200182
183 def inner(self):
184 """Return an inner frame."""
185 return Frame(self)
186
Armin Ronacher75cfb862008-04-11 13:47:22 +0200187 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 """
Armin Ronacher9a0078d2008-08-13 18:24:17 +0200192 rv = self.copy()
Armin Ronacher75cfb862008-04-11 13:47:22 +0200193 rv.rootlevel = False
194 return rv
195
Armin Ronacher9a0078d2008-08-13 18:24:17 +0200196 __copy__ = copy
197
Armin Ronachere791c2a2008-04-07 18:39:54 +0200198
Armin Ronacherc9705c22008-04-27 21:28:03 +0200199class VisitorExit(RuntimeError):
200 """Exception used by the `UndeclaredNameVisitor` to signal a stop."""
201
202
203class DependencyFinderVisitor(NodeVisitor):
204 """A visitor that collects filter and test calls."""
205
206 def __init__(self):
207 self.filters = set()
208 self.tests = set()
209
210 def visit_Filter(self, node):
211 self.generic_visit(node)
212 self.filters.add(node.name)
213
214 def visit_Test(self, node):
215 self.generic_visit(node)
216 self.tests.add(node.name)
217
218 def visit_Block(self, node):
219 """Stop visiting at blocks."""
220
221
222class UndeclaredNameVisitor(NodeVisitor):
223 """A visitor that checks if a name is accessed without being
224 declared. This is different from the frame visitor as it will
225 not stop at closure frames.
226 """
227
228 def __init__(self, names):
229 self.names = set(names)
230 self.undeclared = set()
231
232 def visit_Name(self, node):
233 if node.ctx == 'load' and node.name in self.names:
234 self.undeclared.add(node.name)
235 if self.undeclared == self.names:
236 raise VisitorExit()
237 else:
238 self.names.discard(node.name)
239
240 def visit_Block(self, node):
241 """Stop visiting a blocks."""
242
243
Armin Ronachere791c2a2008-04-07 18:39:54 +0200244class FrameIdentifierVisitor(NodeVisitor):
245 """A visitor for `Frame.inspect`."""
246
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200247 def __init__(self, identifiers, hard_scope):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200248 self.identifiers = identifiers
Armin Ronacher4f62a9f2008-04-08 18:09:13 +0200249 self.hard_scope = hard_scope
Armin Ronachere791c2a2008-04-07 18:39:54 +0200250
Armin Ronacherc9705c22008-04-27 21:28:03 +0200251 def visit_Name(self, node):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200252 """All assignments to names go through this function."""
Armin Ronachere9411b42008-05-15 16:22:07 +0200253 if node.ctx == 'store':
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200254 self.identifiers.declared_locally.add(node.name)
Armin Ronachere9411b42008-05-15 16:22:07 +0200255 elif node.ctx == 'param':
256 self.identifiers.declared_parameter.add(node.name)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200257 elif node.ctx == 'load' and not \
258 self.identifiers.is_declared(node.name, self.hard_scope):
259 self.identifiers.undeclared.add(node.name)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200260
Armin Ronacherc9705c22008-04-27 21:28:03 +0200261 def visit_Macro(self, node):
Armin Ronacherc9705c22008-04-27 21:28:03 +0200262 self.identifiers.declared_locally.add(node.name)
Armin Ronacher0611e492008-04-25 23:44:14 +0200263
Armin Ronacherc9705c22008-04-27 21:28:03 +0200264 def visit_Import(self, node):
265 self.generic_visit(node)
266 self.identifiers.declared_locally.add(node.target)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200267
Armin Ronacherc9705c22008-04-27 21:28:03 +0200268 def visit_FromImport(self, node):
269 self.generic_visit(node)
270 for name in node.names:
271 if isinstance(name, tuple):
272 self.identifiers.declared_locally.add(name[1])
273 else:
274 self.identifiers.declared_locally.add(name)
275
276 def visit_Assign(self, node):
Armin Ronacherebe55aa2008-04-10 20:51:23 +0200277 """Visit assignments in the correct order."""
Armin Ronacherc9705c22008-04-27 21:28:03 +0200278 self.visit(node.node)
279 self.visit(node.target)
Armin Ronacherebe55aa2008-04-10 20:51:23 +0200280
Armin Ronacherc9705c22008-04-27 21:28:03 +0200281 def visit_For(self, node):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200282 """Visiting stops at for blocks. However the block sequence
283 is visited as part of the outer scope.
284 """
Armin Ronacherc9705c22008-04-27 21:28:03 +0200285 self.visit(node.iter)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200286
Armin Ronacherc9705c22008-04-27 21:28:03 +0200287 def visit_CallBlock(self, node):
288 for child in node.iter_child_nodes(exclude=('body',)):
289 self.visit(child)
290
291 def visit_FilterBlock(self, node):
292 self.visit(node.filter)
293
294 def visit_Block(self, node):
295 """Stop visiting at blocks."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200296
297
Armin Ronacher75cfb862008-04-11 13:47:22 +0200298class CompilerExit(Exception):
299 """Raised if the compiler encountered a situation where it just
300 doesn't make sense to further process the code. Any block that
Armin Ronacher0611e492008-04-25 23:44:14 +0200301 raises such an exception is not further processed.
302 """
Armin Ronacher75cfb862008-04-11 13:47:22 +0200303
304
Armin Ronachere791c2a2008-04-07 18:39:54 +0200305class CodeGenerator(NodeVisitor):
306
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200307 def __init__(self, environment, name, filename, stream=None):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200308 if stream is None:
309 stream = StringIO()
Christoph Hack65642a52008-04-08 14:46:56 +0200310 self.environment = environment
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200311 self.name = name
Armin Ronachere791c2a2008-04-07 18:39:54 +0200312 self.filename = filename
313 self.stream = stream
Armin Ronacherfed44b52008-04-13 19:42:53 +0200314
Armin Ronacher023b5e92008-05-08 11:03:10 +0200315 # aliases for imports
316 self.import_aliases = {}
317
Armin Ronacherfed44b52008-04-13 19:42:53 +0200318 # a registry for all blocks. Because blocks are moved out
319 # into the global python scope they are registered here
Armin Ronachere791c2a2008-04-07 18:39:54 +0200320 self.blocks = {}
Armin Ronacherfed44b52008-04-13 19:42:53 +0200321
322 # the number of extends statements so far
Armin Ronacher7fb38972008-04-11 13:54:28 +0200323 self.extends_so_far = 0
Armin Ronacherfed44b52008-04-13 19:42:53 +0200324
325 # some templates have a rootlevel extends. In this case we
326 # can safely assume that we're a child template and do some
327 # more optimizations.
Armin Ronacher75cfb862008-04-11 13:47:22 +0200328 self.has_known_extends = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200329
Armin Ronacherba3757b2008-04-16 19:43:16 +0200330 # the current line number
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200331 self.code_lineno = 1
Armin Ronacherba3757b2008-04-16 19:43:16 +0200332
Armin Ronacherb9e78752008-05-10 23:36:28 +0200333 # registry of all filters and tests (global, not block local)
334 self.tests = {}
335 self.filters = {}
336
Armin Ronacherba3757b2008-04-16 19:43:16 +0200337 # the debug information
338 self.debug_info = []
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200339 self._write_debug_info = None
Armin Ronacherba3757b2008-04-16 19:43:16 +0200340
Armin Ronacherfed44b52008-04-13 19:42:53 +0200341 # the number of new lines before the next write()
342 self._new_lines = 0
343
344 # the line number of the last written statement
Armin Ronachere791c2a2008-04-07 18:39:54 +0200345 self._last_line = 0
Armin Ronacherfed44b52008-04-13 19:42:53 +0200346
347 # true if nothing was written so far.
Armin Ronachere791c2a2008-04-07 18:39:54 +0200348 self._first_write = True
349
Armin Ronacherfed44b52008-04-13 19:42:53 +0200350 # used by the `temporary_identifier` method to get new
351 # unique, temporary identifier
352 self._last_identifier = 0
353
354 # the current indentation
355 self._indentation = 0
356
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200357 # -- Various compilation helpers
358
Armin Ronachere2244882008-05-19 09:25:57 +0200359 def fail(self, msg, lineno):
360 """Fail with a `TemplateAssertionError`."""
361 raise TemplateAssertionError(msg, lineno, self.name, self.filename)
362
Armin Ronachere791c2a2008-04-07 18:39:54 +0200363 def temporary_identifier(self):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200364 """Get a new unique identifier."""
365 self._last_identifier += 1
Armin Ronacher8a1d27f2008-05-19 08:37:19 +0200366 return 't_%d' % self._last_identifier
Armin Ronachere791c2a2008-04-07 18:39:54 +0200367
Armin Ronachered1e0d42008-05-18 20:25:28 +0200368 def buffer(self, frame):
369 """Enable buffering for the frame from that point onwards."""
Armin Ronachere2244882008-05-19 09:25:57 +0200370 frame.buffer = self.temporary_identifier()
371 self.writeline('%s = []' % frame.buffer)
Armin Ronachered1e0d42008-05-18 20:25:28 +0200372
373 def return_buffer_contents(self, frame):
374 """Return the buffer contents of the frame."""
375 if self.environment.autoescape:
376 self.writeline('return Markup(concat(%s))' % frame.buffer)
377 else:
378 self.writeline('return concat(%s)' % frame.buffer)
379
Armin Ronachere791c2a2008-04-07 18:39:54 +0200380 def indent(self):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200381 """Indent by one."""
382 self._indentation += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +0200383
Armin Ronacher8efc5222008-04-08 14:47:40 +0200384 def outdent(self, step=1):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200385 """Outdent by step."""
386 self._indentation -= step
Armin Ronachere791c2a2008-04-07 18:39:54 +0200387
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200388 def start_write(self, frame, node=None):
389 """Yield or write into the frame buffer."""
390 if frame.buffer is None:
391 self.writeline('yield ', node)
392 else:
393 self.writeline('%s.append(' % frame.buffer, node)
394
395 def end_write(self, frame):
396 """End the writing process started by `start_write`."""
397 if frame.buffer is not None:
398 self.write(')')
399
400 def simple_write(self, s, frame, node=None):
401 """Simple shortcut for start_write + write + end_write."""
402 self.start_write(frame, node)
403 self.write(s)
404 self.end_write(frame)
405
Armin Ronacherc9705c22008-04-27 21:28:03 +0200406 def blockvisit(self, nodes, frame, force_generator=True):
407 """Visit a list of nodes as block in a frame. If the current frame
408 is no buffer a dummy ``if 0: yield None`` is written automatically
409 unless the force_generator parameter is set to False.
Armin Ronacherfed44b52008-04-13 19:42:53 +0200410 """
Armin Ronacher625215e2008-04-13 16:31:08 +0200411 if frame.buffer is None and force_generator:
Armin Ronachere791c2a2008-04-07 18:39:54 +0200412 self.writeline('if 0: yield None')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200413 try:
414 for node in nodes:
415 self.visit(node, frame)
416 except CompilerExit:
417 pass
Armin Ronachere791c2a2008-04-07 18:39:54 +0200418
419 def write(self, x):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200420 """Write a string into the output stream."""
421 if self._new_lines:
Armin Ronachere791c2a2008-04-07 18:39:54 +0200422 if not self._first_write:
Armin Ronacherfed44b52008-04-13 19:42:53 +0200423 self.stream.write('\n' * self._new_lines)
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200424 self.code_lineno += self._new_lines
425 if self._write_debug_info is not None:
426 self.debug_info.append((self._write_debug_info,
427 self.code_lineno))
428 self._write_debug_info = None
Armin Ronachere791c2a2008-04-07 18:39:54 +0200429 self._first_write = False
Armin Ronacherfed44b52008-04-13 19:42:53 +0200430 self.stream.write(' ' * self._indentation)
431 self._new_lines = 0
Armin Ronachere791c2a2008-04-07 18:39:54 +0200432 self.stream.write(x)
433
434 def writeline(self, x, node=None, extra=0):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200435 """Combination of newline and write."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200436 self.newline(node, extra)
437 self.write(x)
438
439 def newline(self, node=None, extra=0):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200440 """Add one or more newlines before the next write."""
441 self._new_lines = max(self._new_lines, 1 + extra)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200442 if node is not None and node.lineno != self._last_line:
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200443 self._write_debug_info = node.lineno
444 self._last_line = node.lineno
Armin Ronachere791c2a2008-04-07 18:39:54 +0200445
Armin Ronacherfd310492008-05-25 00:16:51 +0200446 def signature(self, node, frame, extra_kwargs=None):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200447 """Writes a function call to the stream for the current node.
Armin Ronacherfd310492008-05-25 00:16:51 +0200448 A leading comma is added automatically. The extra keyword
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200449 arguments may not include python keywords otherwise a syntax
450 error could occour. The extra keyword arguments should be given
451 as python dict.
Armin Ronacherfed44b52008-04-13 19:42:53 +0200452 """
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200453 # if any of the given keyword arguments is a python keyword
454 # we have to make sure that no invalid call is created.
455 kwarg_workaround = False
456 for kwarg in chain((x.key for x in node.kwargs), extra_kwargs or ()):
Armin Ronacher9a0078d2008-08-13 18:24:17 +0200457 if is_python_keyword(kwarg):
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200458 kwarg_workaround = True
459 break
460
Armin Ronacher8efc5222008-04-08 14:47:40 +0200461 for arg in node.args:
Armin Ronacherfd310492008-05-25 00:16:51 +0200462 self.write(', ')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200463 self.visit(arg, frame)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200464
465 if not kwarg_workaround:
466 for kwarg in node.kwargs:
Armin Ronacherfd310492008-05-25 00:16:51 +0200467 self.write(', ')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200468 self.visit(kwarg, frame)
469 if extra_kwargs is not None:
470 for key, value in extra_kwargs.iteritems():
Armin Ronacherfd310492008-05-25 00:16:51 +0200471 self.write(', %s=%s' % (key, value))
Armin Ronacher8efc5222008-04-08 14:47:40 +0200472 if node.dyn_args:
Armin Ronacherfd310492008-05-25 00:16:51 +0200473 self.write(', *')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200474 self.visit(node.dyn_args, frame)
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200475
476 if kwarg_workaround:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200477 if node.dyn_kwargs is not None:
Armin Ronacherfd310492008-05-25 00:16:51 +0200478 self.write(', **dict({')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200479 else:
Armin Ronacherfd310492008-05-25 00:16:51 +0200480 self.write(', **{')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200481 for kwarg in node.kwargs:
482 self.write('%r: ' % kwarg.key)
483 self.visit(kwarg.value, frame)
484 self.write(', ')
485 if extra_kwargs is not None:
486 for key, value in extra_kwargs.iteritems():
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200487 self.write('%r: %s, ' % (key, value))
488 if node.dyn_kwargs is not None:
489 self.write('}, **')
490 self.visit(node.dyn_kwargs, frame)
491 self.write(')')
492 else:
493 self.write('}')
494
495 elif node.dyn_kwargs is not None:
Armin Ronacherfd310492008-05-25 00:16:51 +0200496 self.write(', **')
Armin Ronacher8efc5222008-04-08 14:47:40 +0200497 self.visit(node.dyn_kwargs, frame)
498
Armin Ronacherc9705c22008-04-27 21:28:03 +0200499 def pull_locals(self, frame):
500 """Pull all the references identifiers into the local scope."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200501 for name in frame.identifiers.undeclared:
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200502 self.writeline('l_%s = context.resolve(%r)' % (name, name))
Armin Ronacherc9705c22008-04-27 21:28:03 +0200503
504 def pull_dependencies(self, nodes):
505 """Pull all the dependencies."""
506 visitor = DependencyFinderVisitor()
507 for node in nodes:
508 visitor.visit(node)
Armin Ronacherb9e78752008-05-10 23:36:28 +0200509 for dependency in 'filters', 'tests':
510 mapping = getattr(self, dependency)
511 for name in getattr(visitor, dependency):
512 if name not in mapping:
513 mapping[name] = self.temporary_identifier()
514 self.writeline('%s = environment.%s[%r]' %
515 (mapping[name], dependency, name))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200516
Armin Ronacherff53c782008-08-13 18:55:50 +0200517 def collect_shadowed(self, frame, extra_vars=()):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200518 """This function returns all the shadowed variables in a dict
519 in the form name: alias and will write the required assignments
520 into the current scope. No indentation takes place.
Armin Ronacherff53c782008-08-13 18:55:50 +0200521
522 `extra_vars` is passed to `Identifiers.find_shadowed`.
Armin Ronacherfed44b52008-04-13 19:42:53 +0200523 """
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200524 aliases = {}
Armin Ronacherff53c782008-08-13 18:55:50 +0200525 for name in frame.identifiers.find_shadowed(extra_vars):
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200526 aliases[name] = ident = self.temporary_identifier()
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200527 self.writeline('%s = l_%s' % (ident, name))
Armin Ronacherfa865fb2008-04-12 22:11:53 +0200528 return aliases
529
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200530 def restore_shadowed(self, aliases):
531 """Restore all aliases."""
532 for name, alias in aliases.iteritems():
533 self.writeline('l_%s = %s' % (name, alias))
534
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200535 def function_scoping(self, node, frame, children=None,
536 find_special=True):
Armin Ronacherfed44b52008-04-13 19:42:53 +0200537 """In Jinja a few statements require the help of anonymous
538 functions. Those are currently macros and call blocks and in
539 the future also recursive loops. As there is currently
540 technical limitation that doesn't allow reading and writing a
541 variable in a scope where the initial value is coming from an
542 outer scope, this function tries to fall back with a common
543 error message. Additionally the frame passed is modified so
544 that the argumetns are collected and callers are looked up.
545
546 This will return the modified frame.
547 """
Armin Ronacherc9705c22008-04-27 21:28:03 +0200548 # we have to iterate twice over it, make sure that works
549 if children is None:
550 children = node.iter_child_nodes()
551 children = list(children)
Armin Ronacher71082072008-04-12 14:19:36 +0200552 func_frame = frame.inner()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200553 func_frame.inspect(children, hard_scope=True)
Armin Ronacher71082072008-04-12 14:19:36 +0200554
555 # variables that are undeclared (accessed before declaration) and
556 # declared locally *and* part of an outside scope raise a template
557 # assertion error. Reason: we can't generate reasonable code from
558 # it without aliasing all the variables. XXX: alias them ^^
559 overriden_closure_vars = (
560 func_frame.identifiers.undeclared &
561 func_frame.identifiers.declared &
562 (func_frame.identifiers.declared_locally |
563 func_frame.identifiers.declared_parameter)
564 )
565 if overriden_closure_vars:
Armin Ronachere2244882008-05-19 09:25:57 +0200566 self.fail('It\'s not possible to set and access variables '
567 'derived from an outer scope! (affects: %s' %
568 ', '.join(sorted(overriden_closure_vars)), node.lineno)
Armin Ronacher71082072008-04-12 14:19:36 +0200569
570 # remove variables from a closure from the frame's undeclared
571 # identifiers.
572 func_frame.identifiers.undeclared -= (
573 func_frame.identifiers.undeclared &
574 func_frame.identifiers.declared
575 )
576
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200577 # no special variables for this scope, abort early
578 if not find_special:
579 return func_frame
580
Armin Ronacher963f97d2008-04-25 11:44:59 +0200581 func_frame.accesses_kwargs = False
582 func_frame.accesses_varargs = False
Armin Ronacher71082072008-04-12 14:19:36 +0200583 func_frame.accesses_caller = False
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200584 func_frame.arguments = args = ['l_' + x.name for x in node.args]
Armin Ronacher71082072008-04-12 14:19:36 +0200585
Armin Ronacherc9705c22008-04-27 21:28:03 +0200586 undeclared = find_undeclared(children, ('caller', 'kwargs', 'varargs'))
587
588 if 'caller' in undeclared:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200589 func_frame.accesses_caller = True
590 func_frame.identifiers.add_special('caller')
591 args.append('l_caller')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200592 if 'kwargs' in undeclared:
Armin Ronacher963f97d2008-04-25 11:44:59 +0200593 func_frame.accesses_kwargs = True
594 func_frame.identifiers.add_special('kwargs')
595 args.append('l_kwargs')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200596 if 'varargs' in undeclared:
Armin Ronacher963f97d2008-04-25 11:44:59 +0200597 func_frame.accesses_varargs = True
598 func_frame.identifiers.add_special('varargs')
599 args.append('l_varargs')
Armin Ronacher71082072008-04-12 14:19:36 +0200600 return func_frame
601
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200602 def macro_body(self, node, frame, children=None):
603 """Dump the function def of a macro or call block."""
604 frame = self.function_scoping(node, frame, children)
605 args = frame.arguments
606 self.writeline('def macro(%s):' % ', '.join(args), node)
607 self.indent()
608 self.buffer(frame)
609 self.pull_locals(frame)
610 self.blockvisit(node.body, frame)
611 self.return_buffer_contents(frame)
612 self.outdent()
613 return frame
614
615 def macro_def(self, node, frame):
616 """Dump the macro definition for the def created by macro_body."""
617 arg_tuple = ', '.join(repr(x.name) for x in node.args)
618 name = getattr(node, 'name', None)
619 if len(node.args) == 1:
620 arg_tuple += ','
621 self.write('Macro(environment, macro, %r, (%s), (' %
622 (name, arg_tuple))
623 for arg in node.defaults:
624 self.visit(arg, frame)
625 self.write(', ')
Armin Ronacher903d1682008-05-23 00:51:58 +0200626 self.write('), %r, %r, %r)' % (
627 bool(frame.accesses_kwargs),
628 bool(frame.accesses_varargs),
629 bool(frame.accesses_caller)
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200630 ))
631
Armin Ronacher547d0b62008-07-04 16:35:10 +0200632 def position(self, node):
633 """Return a human readable position for the node."""
634 rv = 'line %d' % node.lineno
635 if self.name is not None:
636 rv += ' in' + repr(self.name)
637 return rv
638
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200639 # -- Statement Visitors
Armin Ronachere791c2a2008-04-07 18:39:54 +0200640
641 def visit_Template(self, node, frame=None):
642 assert frame is None, 'no root frame allowed'
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200643 from jinja2.runtime import __all__ as exported
Armin Ronacher709f6e52008-04-28 18:18:16 +0200644 self.writeline('from __future__ import division')
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200645 self.writeline('from jinja2.runtime import ' + ', '.join(exported))
Armin Ronacher8edbe492008-04-10 20:43:43 +0200646
Armin Ronacher75cfb862008-04-11 13:47:22 +0200647 # do we have an extends tag at all? If not, we can save some
648 # overhead by just not processing any inheritance code.
649 have_extends = node.find(nodes.Extends) is not None
650
Armin Ronacher8edbe492008-04-10 20:43:43 +0200651 # find all blocks
652 for block in node.find_all(nodes.Block):
653 if block.name in self.blocks:
Armin Ronachere2244882008-05-19 09:25:57 +0200654 self.fail('block %r defined twice' % block.name, block.lineno)
Armin Ronacher8edbe492008-04-10 20:43:43 +0200655 self.blocks[block.name] = block
Armin Ronachere791c2a2008-04-07 18:39:54 +0200656
Armin Ronacher023b5e92008-05-08 11:03:10 +0200657 # find all imports and import them
658 for import_ in node.find_all(nodes.ImportedName):
659 if import_.importname not in self.import_aliases:
660 imp = import_.importname
661 self.import_aliases[imp] = alias = self.temporary_identifier()
662 if '.' in imp:
663 module, obj = imp.rsplit('.', 1)
664 self.writeline('from %s import %s as %s' %
665 (module, obj, alias))
666 else:
667 self.writeline('import %s as %s' % (imp, alias))
668
669 # add the load name
Armin Ronacherdc02b642008-05-15 22:47:27 +0200670 self.writeline('name = %r' % self.name)
Armin Ronacher023b5e92008-05-08 11:03:10 +0200671
Armin Ronacher8efc5222008-04-08 14:47:40 +0200672 # generate the root render function.
Armin Ronacher32a910f2008-04-26 23:21:03 +0200673 self.writeline('def root(context, environment=environment):', extra=1)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200674
675 # process the root
Armin Ronachere791c2a2008-04-07 18:39:54 +0200676 frame = Frame()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200677 frame.inspect(node.body)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200678 frame.toplevel = frame.rootlevel = True
Armin Ronacherf059ec12008-04-11 22:21:00 +0200679 self.indent()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200680 if have_extends:
681 self.writeline('parent_template = None')
Armin Ronacherc9705c22008-04-27 21:28:03 +0200682 if 'self' in find_undeclared(node.body, ('self',)):
683 frame.identifiers.add_special('self')
684 self.writeline('l_self = TemplateReference(context)')
Armin Ronacher6df604e2008-05-23 22:18:38 +0200685 self.pull_locals(frame)
686 self.pull_dependencies(node.body)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200687 self.blockvisit(node.body, frame)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200688 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200689
Armin Ronacher8efc5222008-04-08 14:47:40 +0200690 # make sure that the parent root is called.
Armin Ronacher75cfb862008-04-11 13:47:22 +0200691 if have_extends:
692 if not self.has_known_extends:
693 self.indent()
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200694 self.writeline('if parent_template is not None:')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200695 self.indent()
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200696 self.writeline('for event in parent_template.'
Armin Ronacher5411ce72008-05-25 11:36:22 +0200697 'root_render_func(context):')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200698 self.indent()
699 self.writeline('yield event')
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200700 self.outdent(2 + (not self.has_known_extends))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200701
702 # at this point we now have the blocks collected and can visit them too.
703 for name, block in self.blocks.iteritems():
704 block_frame = Frame()
Armin Ronacherc9705c22008-04-27 21:28:03 +0200705 block_frame.inspect(block.body)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200706 block_frame.block = name
Armin Ronacherd4c64f72008-04-11 17:15:29 +0200707 self.writeline('def block_%s(context, environment=environment):'
708 % name, block, 1)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200709 self.indent()
710 undeclared = find_undeclared(block.body, ('self', 'super'))
711 if 'self' in undeclared:
712 block_frame.identifiers.add_special('self')
713 self.writeline('l_self = TemplateReference(context)')
714 if 'super' in undeclared:
715 block_frame.identifiers.add_special('super')
716 self.writeline('l_super = context.super(%r, '
717 'block_%s)' % (name, name))
Armin Ronachere791c2a2008-04-07 18:39:54 +0200718 self.pull_locals(block_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200719 self.pull_dependencies(block.body)
Armin Ronacher625215e2008-04-13 16:31:08 +0200720 self.blockvisit(block.body, block_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200721 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200722
Armin Ronacher75cfb862008-04-11 13:47:22 +0200723 self.writeline('blocks = {%s}' % ', '.join('%r: block_%s' % (x, x)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200724 for x in self.blocks),
725 extra=1)
726
727 # add a function that returns the debug info
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200728 self.writeline('debug_info = %r' % '&'.join('%s=%s' % x for x
729 in self.debug_info))
Armin Ronacher75cfb862008-04-11 13:47:22 +0200730
Armin Ronachere791c2a2008-04-07 18:39:54 +0200731 def visit_Block(self, node, frame):
732 """Call a block and register it for the template."""
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200733 level = 1
Armin Ronacher75cfb862008-04-11 13:47:22 +0200734 if frame.toplevel:
Armin Ronacherbcb7c532008-04-11 16:30:34 +0200735 # if we know that we are a child template, there is no need to
736 # check if we are one
737 if self.has_known_extends:
738 return
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200739 if self.extends_so_far > 0:
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200740 self.writeline('if parent_template is None:')
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200741 self.indent()
742 level += 1
Armin Ronacher83fbc0f2008-05-15 12:22:28 +0200743 self.writeline('for event in context.blocks[%r][0](context):' %
Armin Ronacherc9705c22008-04-27 21:28:03 +0200744 node.name, node)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200745 self.indent()
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200746 self.simple_write('event', frame)
Armin Ronacher41ef36f2008-04-11 19:55:08 +0200747 self.outdent(level)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200748
749 def visit_Extends(self, node, frame):
750 """Calls the extender."""
Armin Ronacher8efc5222008-04-08 14:47:40 +0200751 if not frame.toplevel:
Armin Ronachere2244882008-05-19 09:25:57 +0200752 self.fail('cannot use extend from a non top-level scope',
753 node.lineno)
Armin Ronacher75cfb862008-04-11 13:47:22 +0200754
Armin Ronacher7fb38972008-04-11 13:54:28 +0200755 # if the number of extends statements in general is zero so
756 # far, we don't have to add a check if something extended
757 # the template before this one.
758 if self.extends_so_far > 0:
Armin Ronacher75cfb862008-04-11 13:47:22 +0200759
Armin Ronacher7fb38972008-04-11 13:54:28 +0200760 # if we have a known extends we just add a template runtime
761 # error into the generated code. We could catch that at compile
762 # time too, but i welcome it not to confuse users by throwing the
763 # same error at different times just "because we can".
764 if not self.has_known_extends:
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200765 self.writeline('if parent_template is not None:')
Armin Ronacher7fb38972008-04-11 13:54:28 +0200766 self.indent()
767 self.writeline('raise TemplateRuntimeError(%r)' %
768 'extended multiple times')
Armin Ronacher75cfb862008-04-11 13:47:22 +0200769
Armin Ronacher7fb38972008-04-11 13:54:28 +0200770 # if we have a known extends already we don't need that code here
771 # as we know that the template execution will end here.
772 if self.has_known_extends:
773 raise CompilerExit()
774 self.outdent()
775
Armin Ronacher9d42abf2008-05-14 18:10:41 +0200776 self.writeline('parent_template = environment.get_template(', node)
Armin Ronacher8efc5222008-04-08 14:47:40 +0200777 self.visit(node.template, frame)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200778 self.write(', %r)' % self.name)
779 self.writeline('for name, parent_block in parent_template.'
780 'blocks.iteritems():')
781 self.indent()
782 self.writeline('context.blocks.setdefault(name, []).'
Armin Ronacher83fbc0f2008-05-15 12:22:28 +0200783 'append(parent_block)')
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200784 self.outdent()
Armin Ronacher75cfb862008-04-11 13:47:22 +0200785
786 # if this extends statement was in the root level we can take
787 # advantage of that information and simplify the generated code
788 # in the top level from this point onwards
Armin Ronacher27069d72008-05-11 19:48:12 +0200789 if frame.rootlevel:
790 self.has_known_extends = True
Armin Ronachere791c2a2008-04-07 18:39:54 +0200791
Armin Ronacher7fb38972008-04-11 13:54:28 +0200792 # and now we have one more
793 self.extends_so_far += 1
794
Armin Ronacherf059ec12008-04-11 22:21:00 +0200795 def visit_Include(self, node, frame):
796 """Handles includes."""
Armin Ronacherea847c52008-05-02 20:04:32 +0200797 if node.with_context:
798 self.writeline('template = environment.get_template(', node)
799 self.visit(node.template, frame)
800 self.write(', %r)' % self.name)
Armin Ronacher5411ce72008-05-25 11:36:22 +0200801 self.writeline('for event in template.root_render_func('
Armin Ronacherea847c52008-05-02 20:04:32 +0200802 'template.new_context(context.parent, True)):')
803 else:
804 self.writeline('for event in environment.get_template(', node)
805 self.visit(node.template, frame)
Armin Ronacher771c7502008-05-18 23:14:14 +0200806 self.write(', %r).module._body_stream:' %
Armin Ronacherea847c52008-05-02 20:04:32 +0200807 self.name)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200808 self.indent()
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200809 self.simple_write('event', frame)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200810 self.outdent()
811
Armin Ronacher0611e492008-04-25 23:44:14 +0200812 def visit_Import(self, node, frame):
813 """Visit regular imports."""
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200814 self.writeline('l_%s = ' % node.target, node)
Armin Ronacherf059ec12008-04-11 22:21:00 +0200815 if frame.toplevel:
Armin Ronacher53042292008-04-26 18:30:19 +0200816 self.write('context.vars[%r] = ' % node.target)
Armin Ronacher0611e492008-04-25 23:44:14 +0200817 self.write('environment.get_template(')
818 self.visit(node.template, frame)
Armin Ronacherea847c52008-05-02 20:04:32 +0200819 self.write(', %r).' % self.name)
820 if node.with_context:
821 self.write('make_module(context.parent, True)')
822 else:
823 self.write('module')
Armin Ronacher903d1682008-05-23 00:51:58 +0200824 if frame.toplevel and not node.target.startswith('_'):
Armin Ronacher53042292008-04-26 18:30:19 +0200825 self.writeline('context.exported_vars.discard(%r)' % node.target)
Armin Ronacher0611e492008-04-25 23:44:14 +0200826
827 def visit_FromImport(self, node, frame):
828 """Visit named imports."""
829 self.newline(node)
830 self.write('included_template = environment.get_template(')
831 self.visit(node.template, frame)
Armin Ronacherea847c52008-05-02 20:04:32 +0200832 self.write(', %r).' % self.name)
833 if node.with_context:
834 self.write('make_module(context.parent, True)')
835 else:
836 self.write('module')
Armin Ronachera78d2762008-05-15 23:18:07 +0200837
838 var_names = []
839 discarded_names = []
Armin Ronacher0611e492008-04-25 23:44:14 +0200840 for name in node.names:
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200841 if isinstance(name, tuple):
842 name, alias = name
843 else:
844 alias = name
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200845 self.writeline('l_%s = getattr(included_template, '
846 '%r, missing)' % (alias, name))
847 self.writeline('if l_%s is missing:' % alias)
Armin Ronacher0611e492008-04-25 23:44:14 +0200848 self.indent()
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200849 self.writeline('l_%s = environment.undefined(%r %% '
Armin Ronacherdc02b642008-05-15 22:47:27 +0200850 'included_template.__name__, '
Armin Ronacher0a2ac692008-05-13 01:03:08 +0200851 'name=%r)' %
Armin Ronacher547d0b62008-07-04 16:35:10 +0200852 (alias, 'the template %%r (imported on %s) does '
853 'not export the requested name %s' % (
854 self.position(node),
855 repr(name)
856 ), name))
Armin Ronacher0611e492008-04-25 23:44:14 +0200857 self.outdent()
858 if frame.toplevel:
Armin Ronachera78d2762008-05-15 23:18:07 +0200859 var_names.append(alias)
Armin Ronacher903d1682008-05-23 00:51:58 +0200860 if not alias.startswith('_'):
Armin Ronachera78d2762008-05-15 23:18:07 +0200861 discarded_names.append(alias)
862
863 if var_names:
864 if len(var_names) == 1:
865 name = var_names[0]
866 self.writeline('context.vars[%r] = l_%s' % (name, name))
867 else:
868 self.writeline('context.vars.update({%s})' % ', '.join(
869 '%r: l_%s' % (name, name) for name in var_names
870 ))
871 if discarded_names:
872 if len(discarded_names) == 1:
873 self.writeline('context.exported_vars.discard(%r)' %
874 discarded_names[0])
875 else:
876 self.writeline('context.exported_vars.difference_'
877 'update((%s))' % ', '.join(map(repr, discarded_names)))
Armin Ronacherf059ec12008-04-11 22:21:00 +0200878
Armin Ronachere791c2a2008-04-07 18:39:54 +0200879 def visit_For(self, node, frame):
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200880 # when calculating the nodes for the inner frame we have to exclude
881 # the iterator contents from it
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200882 children = node.iter_child_nodes(exclude=('iter',))
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200883 if node.recursive:
884 loop_frame = self.function_scoping(node, frame, children,
885 find_special=False)
886 else:
887 loop_frame = frame.inner()
888 loop_frame.inspect(children)
889
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200890 # try to figure out if we have an extended loop. An extended loop
891 # is necessary if the loop is in recursive mode if the special loop
892 # variable is accessed in the body.
893 extended_loop = node.recursive or 'loop' in \
894 find_undeclared(node.iter_child_nodes(
895 only=('body',)), ('loop',))
896
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200897 # if we don't have an recursive loop we have to find the shadowed
Armin Ronacherff53c782008-08-13 18:55:50 +0200898 # variables at that point. Because loops can be nested but the loop
899 # variable is a special one we have to enforce aliasing for it.
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200900 if not node.recursive:
Armin Ronacherff53c782008-08-13 18:55:50 +0200901 aliases = self.collect_shadowed(loop_frame, ('loop',))
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200902
903 # otherwise we set up a buffer and add a function def
904 else:
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200905 self.writeline('def loop(reciter, loop_render_func):', node)
906 self.indent()
Armin Ronachered1e0d42008-05-18 20:25:28 +0200907 self.buffer(loop_frame)
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200908 aliases = {}
909
Armin Ronacherff53c782008-08-13 18:55:50 +0200910 # make sure the loop variable is a special one and raise a template
911 # assertion error if a loop tries to write to loop
Armin Ronacher833a3b52008-08-14 12:31:12 +0200912 if extended_loop:
913 loop_frame.identifiers.add_special('loop')
Armin Ronacherff53c782008-08-13 18:55:50 +0200914 for name in node.find_all(nodes.Name):
915 if name.ctx == 'store' and name.name == 'loop':
916 self.fail('Can\'t assign to special loop variable '
917 'in for-loop target', name.lineno)
918
Armin Ronacherc9705c22008-04-27 21:28:03 +0200919 self.pull_locals(loop_frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200920 if node.else_:
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200921 iteration_indicator = self.temporary_identifier()
922 self.writeline('%s = 1' % iteration_indicator)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200923
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200924 # Create a fake parent loop if the else or test section of a
925 # loop is accessing the special loop variable and no parent loop
926 # exists.
927 if 'loop' not in aliases and 'loop' in find_undeclared(
928 node.iter_child_nodes(only=('else_', 'test')), ('loop',)):
929 self.writeline("l_loop = environment.undefined(%r, name='loop')" %
Armin Ronacher547d0b62008-07-04 16:35:10 +0200930 ("'loop' is undefined. the filter section of a loop as well "
931 "as the else block doesn't have access to the special 'loop'"
932 " variable of the current loop. Because there is no parent "
933 "loop it's undefined. Happened in loop on %s" %
934 self.position(node)))
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200935
936 self.writeline('for ', node)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200937 self.visit(node.target, loop_frame)
Armin Ronacher180a1bd2008-04-09 12:14:24 +0200938 self.write(extended_loop and ', l_loop in LoopContext(' or ' in ')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200939
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200940 # if we have an extened loop and a node test, we filter in the
941 # "outer frame".
942 if extended_loop and node.test is not None:
943 self.write('(')
944 self.visit(node.target, loop_frame)
945 self.write(' for ')
946 self.visit(node.target, loop_frame)
947 self.write(' in ')
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200948 if node.recursive:
949 self.write('reciter')
950 else:
951 self.visit(node.iter, loop_frame)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200952 self.write(' if (')
953 test_frame = loop_frame.copy()
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200954 self.visit(node.test, test_frame)
955 self.write('))')
956
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200957 elif node.recursive:
958 self.write('reciter')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200959 else:
960 self.visit(node.iter, loop_frame)
961
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200962 if node.recursive:
963 self.write(', recurse=loop_render_func):')
964 else:
965 self.write(extended_loop and '):' or ':')
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200966
967 # tests in not extended loops become a continue
968 if not extended_loop and node.test is not None:
969 self.indent()
Armin Ronacher47a506f2008-05-06 12:17:23 +0200970 self.writeline('if not ')
Armin Ronacher32a910f2008-04-26 23:21:03 +0200971 self.visit(node.test, loop_frame)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200972 self.write(':')
973 self.indent()
974 self.writeline('continue')
975 self.outdent(2)
976
Armin Ronacherc9705c22008-04-27 21:28:03 +0200977 self.indent()
Armin Ronacherbe4ae242008-04-18 09:49:08 +0200978 self.blockvisit(node.body, loop_frame, force_generator=True)
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200979 if node.else_:
980 self.writeline('%s = 0' % iteration_indicator)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200981 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200982
983 if node.else_:
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200984 self.writeline('if %s:' % iteration_indicator)
Armin Ronacher3d8b7842008-04-13 13:16:50 +0200985 self.indent()
Armin Ronacher625215e2008-04-13 16:31:08 +0200986 self.blockvisit(node.else_, loop_frame, force_generator=False)
Armin Ronacherc9705c22008-04-27 21:28:03 +0200987 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +0200988
Armin Ronacherd4c64f72008-04-11 17:15:29 +0200989 # reset the aliases if there are any.
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200990 self.restore_shadowed(aliases)
Armin Ronachere791c2a2008-04-07 18:39:54 +0200991
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200992 # if the node was recursive we have to return the buffer contents
993 # and start the iteration code
994 if node.recursive:
Armin Ronachered1e0d42008-05-18 20:25:28 +0200995 self.return_buffer_contents(loop_frame)
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200996 self.outdent()
Armin Ronachera2eb77d2008-05-22 20:28:21 +0200997 self.start_write(frame, node)
998 self.write('loop(')
Armin Ronacher1e1e8902008-05-11 23:21:16 +0200999 self.visit(node.iter, frame)
1000 self.write(', loop)')
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001001 self.end_write(frame)
Armin Ronacher1e1e8902008-05-11 23:21:16 +02001002
Armin Ronachere791c2a2008-04-07 18:39:54 +02001003 def visit_If(self, node, frame):
Armin Ronacher75cfb862008-04-11 13:47:22 +02001004 if_frame = frame.soft()
Armin Ronachere791c2a2008-04-07 18:39:54 +02001005 self.writeline('if ', node)
Armin Ronacher75cfb862008-04-11 13:47:22 +02001006 self.visit(node.test, if_frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001007 self.write(':')
Armin Ronacherc9705c22008-04-27 21:28:03 +02001008 self.indent()
Armin Ronacher75cfb862008-04-11 13:47:22 +02001009 self.blockvisit(node.body, if_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +02001010 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +02001011 if node.else_:
1012 self.writeline('else:')
Armin Ronacherc9705c22008-04-27 21:28:03 +02001013 self.indent()
Armin Ronacher75cfb862008-04-11 13:47:22 +02001014 self.blockvisit(node.else_, if_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +02001015 self.outdent()
Armin Ronachere791c2a2008-04-07 18:39:54 +02001016
Armin Ronacher8efc5222008-04-08 14:47:40 +02001017 def visit_Macro(self, node, frame):
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001018 macro_frame = self.macro_body(node, frame)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001019 self.newline()
1020 if frame.toplevel:
Armin Ronacher903d1682008-05-23 00:51:58 +02001021 if not node.name.startswith('_'):
Armin Ronacherc9705c22008-04-27 21:28:03 +02001022 self.write('context.exported_vars.add(%r)' % node.name)
Armin Ronacher32a910f2008-04-26 23:21:03 +02001023 self.writeline('context.vars[%r] = ' % node.name)
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001024 self.write('l_%s = ' % node.name)
1025 self.macro_def(node, macro_frame)
Armin Ronacher71082072008-04-12 14:19:36 +02001026
1027 def visit_CallBlock(self, node, frame):
Armin Ronacher3da90312008-05-23 16:37:28 +02001028 children = node.iter_child_nodes(exclude=('call',))
1029 call_frame = self.macro_body(node, frame, children)
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001030 self.writeline('caller = ')
1031 self.macro_def(node, call_frame)
1032 self.start_write(frame, node)
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001033 self.visit_Call(node.call, call_frame, forward_caller=True)
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001034 self.end_write(frame)
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001035
1036 def visit_FilterBlock(self, node, frame):
1037 filter_frame = frame.inner()
1038 filter_frame.inspect(node.iter_child_nodes())
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001039 aliases = self.collect_shadowed(filter_frame)
Armin Ronacherc9705c22008-04-27 21:28:03 +02001040 self.pull_locals(filter_frame)
Armin Ronachered1e0d42008-05-18 20:25:28 +02001041 self.buffer(filter_frame)
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001042 self.blockvisit(node.body, filter_frame, force_generator=False)
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001043 self.start_write(frame, node)
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001044 self.visit_Filter(node.filter, filter_frame)
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001045 self.end_write(frame)
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001046 self.restore_shadowed(aliases)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001047
Armin Ronachere791c2a2008-04-07 18:39:54 +02001048 def visit_ExprStmt(self, node, frame):
1049 self.newline(node)
Armin Ronacher6ce170c2008-04-25 12:32:36 +02001050 self.visit(node.node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001051
1052 def visit_Output(self, node, frame):
Armin Ronacher75cfb862008-04-11 13:47:22 +02001053 # if we have a known extends statement, we don't output anything
Armin Ronacher7a52df82008-04-11 13:58:22 +02001054 if self.has_known_extends and frame.toplevel:
Armin Ronacher75cfb862008-04-11 13:47:22 +02001055 return
Armin Ronachere791c2a2008-04-07 18:39:54 +02001056
Armin Ronacher665bfb82008-07-14 13:41:46 +02001057 if self.environment.finalize:
1058 finalize = lambda x: unicode(self.environment.finalize(x))
1059 else:
1060 finalize = unicode
1061
Armin Ronacher75cfb862008-04-11 13:47:22 +02001062 self.newline(node)
Armin Ronacher8edbe492008-04-10 20:43:43 +02001063
Armin Ronacher7fb38972008-04-11 13:54:28 +02001064 # if we are in the toplevel scope and there was already an extends
1065 # statement we have to add a check that disables our yield(s) here
1066 # so that they don't appear in the output.
1067 outdent_later = False
1068 if frame.toplevel and self.extends_so_far != 0:
Armin Ronacher203bfcb2008-04-24 21:54:44 +02001069 self.writeline('if parent_template is None:')
Armin Ronacher75cfb862008-04-11 13:47:22 +02001070 self.indent()
Armin Ronacher7fb38972008-04-11 13:54:28 +02001071 outdent_later = True
Armin Ronacher75cfb862008-04-11 13:47:22 +02001072
Armin Ronachere791c2a2008-04-07 18:39:54 +02001073 # try to evaluate as many chunks as possible into a static
1074 # string at compile time.
1075 body = []
1076 for child in node.nodes:
1077 try:
Armin Ronacher9cf95912008-05-24 19:54:43 +02001078 const = child.as_const()
1079 except nodes.Impossible:
1080 body.append(child)
1081 continue
1082 try:
1083 if self.environment.autoescape:
1084 if hasattr(const, '__html__'):
1085 const = const.__html__()
1086 else:
1087 const = escape(const)
Armin Ronacher665bfb82008-07-14 13:41:46 +02001088 const = finalize(const)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001089 except:
Armin Ronacher9cf95912008-05-24 19:54:43 +02001090 # if something goes wrong here we evaluate the node
1091 # at runtime for easier debugging
Armin Ronachere791c2a2008-04-07 18:39:54 +02001092 body.append(child)
1093 continue
1094 if body and isinstance(body[-1], list):
1095 body[-1].append(const)
1096 else:
1097 body.append([const])
1098
Armin Ronachered1e0d42008-05-18 20:25:28 +02001099 # if we have less than 3 nodes or a buffer we yield or extend/append
1100 if len(body) < 3 or frame.buffer is not None:
Armin Ronacher32a910f2008-04-26 23:21:03 +02001101 if frame.buffer is not None:
Armin Ronacher1e1e8902008-05-11 23:21:16 +02001102 # for one item we append, for more we extend
1103 if len(body) == 1:
1104 self.writeline('%s.append(' % frame.buffer)
1105 else:
1106 self.writeline('%s.extend((' % frame.buffer)
Armin Ronacher1f627ff2008-05-15 13:23:26 +02001107 self.indent()
Armin Ronachere791c2a2008-04-07 18:39:54 +02001108 for item in body:
1109 if isinstance(item, list):
Armin Ronacherde6bf712008-04-26 01:44:14 +02001110 val = repr(concat(item))
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001111 if frame.buffer is None:
1112 self.writeline('yield ' + val)
1113 else:
Armin Ronacher1f627ff2008-05-15 13:23:26 +02001114 self.writeline(val + ', ')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001115 else:
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001116 if frame.buffer is None:
Armin Ronachered1e0d42008-05-18 20:25:28 +02001117 self.writeline('yield ', item)
Armin Ronacher1f627ff2008-05-15 13:23:26 +02001118 else:
1119 self.newline(item)
Armin Ronacherd1342312008-04-28 12:20:12 +02001120 close = 1
1121 if self.environment.autoescape:
1122 self.write('escape(')
1123 else:
1124 self.write('unicode(')
1125 if self.environment.finalize is not None:
1126 self.write('environment.finalize(')
1127 close += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +02001128 self.visit(item, frame)
Armin Ronacherd1342312008-04-28 12:20:12 +02001129 self.write(')' * close)
Armin Ronacher32a910f2008-04-26 23:21:03 +02001130 if frame.buffer is not None:
1131 self.write(', ')
1132 if frame.buffer is not None:
Armin Ronacher1e1e8902008-05-11 23:21:16 +02001133 # close the open parentheses
Armin Ronacher1f627ff2008-05-15 13:23:26 +02001134 self.outdent()
1135 self.writeline(len(body) == 1 and ')' or '))')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001136
1137 # otherwise we create a format string as this is faster in that case
1138 else:
1139 format = []
1140 arguments = []
1141 for item in body:
1142 if isinstance(item, list):
Armin Ronacherde6bf712008-04-26 01:44:14 +02001143 format.append(concat(item).replace('%', '%%'))
Armin Ronachere791c2a2008-04-07 18:39:54 +02001144 else:
1145 format.append('%s')
1146 arguments.append(item)
Armin Ronachered1e0d42008-05-18 20:25:28 +02001147 self.writeline('yield ')
Armin Ronacherde6bf712008-04-26 01:44:14 +02001148 self.write(repr(concat(format)) + ' % (')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001149 idx = -1
Armin Ronachera7f016d2008-05-16 00:22:40 +02001150 self.indent()
Armin Ronacher8e8d0712008-04-16 23:10:49 +02001151 for argument in arguments:
Armin Ronachered1e0d42008-05-18 20:25:28 +02001152 self.newline(argument)
Armin Ronacherd1342312008-04-28 12:20:12 +02001153 close = 0
1154 if self.environment.autoescape:
1155 self.write('escape(')
1156 close += 1
1157 if self.environment.finalize is not None:
1158 self.write('environment.finalize(')
1159 close += 1
Armin Ronachere791c2a2008-04-07 18:39:54 +02001160 self.visit(argument, frame)
Armin Ronacher1f627ff2008-05-15 13:23:26 +02001161 self.write(')' * close + ', ')
Armin Ronachera7f016d2008-05-16 00:22:40 +02001162 self.outdent()
1163 self.writeline(')')
Armin Ronachere791c2a2008-04-07 18:39:54 +02001164
Armin Ronacher7fb38972008-04-11 13:54:28 +02001165 if outdent_later:
Armin Ronacher75cfb862008-04-11 13:47:22 +02001166 self.outdent()
1167
Armin Ronacher8efc5222008-04-08 14:47:40 +02001168 def visit_Assign(self, node, frame):
1169 self.newline(node)
1170 # toplevel assignments however go into the local namespace and
1171 # the current template's context. We create a copy of the frame
1172 # here and add a set so that the Name visitor can add the assigned
1173 # names here.
1174 if frame.toplevel:
1175 assignment_frame = frame.copy()
1176 assignment_frame.assigned_names = set()
1177 else:
1178 assignment_frame = frame
1179 self.visit(node.target, assignment_frame)
1180 self.write(' = ')
1181 self.visit(node.node, frame)
Armin Ronacher9706fab2008-04-08 18:49:56 +02001182
1183 # make sure toplevel assignments are added to the context.
Armin Ronacher8efc5222008-04-08 14:47:40 +02001184 if frame.toplevel:
Armin Ronacher69e12db2008-05-12 09:00:03 +02001185 public_names = [x for x in assignment_frame.assigned_names
Armin Ronacher903d1682008-05-23 00:51:58 +02001186 if not x.startswith('_')]
Armin Ronacher69e12db2008-05-12 09:00:03 +02001187 if len(assignment_frame.assigned_names) == 1:
1188 name = iter(assignment_frame.assigned_names).next()
Armin Ronacherd1ff8582008-05-11 00:30:43 +02001189 self.writeline('context.vars[%r] = l_%s' % (name, name))
Armin Ronacher69e12db2008-05-12 09:00:03 +02001190 else:
1191 self.writeline('context.vars.update({')
1192 for idx, name in enumerate(assignment_frame.assigned_names):
1193 if idx:
1194 self.write(', ')
1195 self.write('%r: l_%s' % (name, name))
1196 self.write('})')
1197 if public_names:
1198 if len(public_names) == 1:
1199 self.writeline('context.exported_vars.add(%r)' %
1200 public_names[0])
1201 else:
1202 self.writeline('context.exported_vars.update((%s))' %
1203 ', '.join(map(repr, public_names)))
Armin Ronacher8efc5222008-04-08 14:47:40 +02001204
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001205 # -- Expression Visitors
1206
Armin Ronachere791c2a2008-04-07 18:39:54 +02001207 def visit_Name(self, node, frame):
Armin Ronacherc9705c22008-04-27 21:28:03 +02001208 if node.ctx == 'store' and frame.toplevel:
1209 frame.assigned_names.add(node.name)
Armin Ronacherd1ff8582008-05-11 00:30:43 +02001210 self.write('l_' + node.name)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001211
1212 def visit_Const(self, node, frame):
1213 val = node.value
1214 if isinstance(val, float):
Armin Ronachere791c2a2008-04-07 18:39:54 +02001215 self.write(str(val))
1216 else:
1217 self.write(repr(val))
1218
Armin Ronacher5411ce72008-05-25 11:36:22 +02001219 def visit_TemplateData(self, node, frame):
1220 self.write(repr(node.as_const()))
1221
Armin Ronacher8efc5222008-04-08 14:47:40 +02001222 def visit_Tuple(self, node, frame):
1223 self.write('(')
1224 idx = -1
1225 for idx, item in enumerate(node.items):
1226 if idx:
1227 self.write(', ')
1228 self.visit(item, frame)
1229 self.write(idx == 0 and ',)' or ')')
1230
Armin Ronacher8edbe492008-04-10 20:43:43 +02001231 def visit_List(self, node, frame):
1232 self.write('[')
1233 for idx, item in enumerate(node.items):
1234 if idx:
1235 self.write(', ')
1236 self.visit(item, frame)
1237 self.write(']')
1238
1239 def visit_Dict(self, node, frame):
1240 self.write('{')
1241 for idx, item in enumerate(node.items):
1242 if idx:
1243 self.write(', ')
1244 self.visit(item.key, frame)
1245 self.write(': ')
1246 self.visit(item.value, frame)
1247 self.write('}')
1248
Armin Ronachere791c2a2008-04-07 18:39:54 +02001249 def binop(operator):
1250 def visitor(self, node, frame):
1251 self.write('(')
1252 self.visit(node.left, frame)
1253 self.write(' %s ' % operator)
1254 self.visit(node.right, frame)
1255 self.write(')')
1256 return visitor
1257
1258 def uaop(operator):
1259 def visitor(self, node, frame):
1260 self.write('(' + operator)
Armin Ronacher9a822052008-04-17 18:44:07 +02001261 self.visit(node.node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001262 self.write(')')
1263 return visitor
1264
1265 visit_Add = binop('+')
1266 visit_Sub = binop('-')
1267 visit_Mul = binop('*')
1268 visit_Div = binop('/')
1269 visit_FloorDiv = binop('//')
1270 visit_Pow = binop('**')
1271 visit_Mod = binop('%')
1272 visit_And = binop('and')
1273 visit_Or = binop('or')
1274 visit_Pos = uaop('+')
1275 visit_Neg = uaop('-')
1276 visit_Not = uaop('not ')
1277 del binop, uaop
1278
Armin Ronacherd1342312008-04-28 12:20:12 +02001279 def visit_Concat(self, node, frame):
Armin Ronacherfdf95302008-05-11 22:20:51 +02001280 self.write('%s((' % (self.environment.autoescape and
1281 'markup_join' or 'unicode_join'))
Armin Ronacherd1342312008-04-28 12:20:12 +02001282 for arg in node.nodes:
1283 self.visit(arg, frame)
1284 self.write(', ')
1285 self.write('))')
1286
Armin Ronachere791c2a2008-04-07 18:39:54 +02001287 def visit_Compare(self, node, frame):
1288 self.visit(node.expr, frame)
1289 for op in node.ops:
1290 self.visit(op, frame)
1291
1292 def visit_Operand(self, node, frame):
1293 self.write(' %s ' % operators[node.op])
1294 self.visit(node.expr, frame)
1295
Armin Ronacher6dc6f292008-06-12 08:50:07 +02001296 def visit_Getattr(self, node, frame):
1297 self.write('environment.getattr(')
1298 self.visit(node.node, frame)
1299 self.write(', %r)' % node.attr)
1300
1301 def visit_Getitem(self, node, frame):
Armin Ronacher5c3c4702008-09-12 23:12:49 +02001302 # slices bypass the environment getitem method.
1303 if isinstance(node.arg, nodes.Slice):
Armin Ronacher8efc5222008-04-08 14:47:40 +02001304 self.visit(node.node, frame)
1305 self.write('[')
1306 self.visit(node.arg, frame)
1307 self.write(']')
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001308 else:
Armin Ronacher6dc6f292008-06-12 08:50:07 +02001309 self.write('environment.getitem(')
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001310 self.visit(node.node, frame)
1311 self.write(', ')
1312 self.visit(node.arg, frame)
1313 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001314
1315 def visit_Slice(self, node, frame):
1316 if node.start is not None:
1317 self.visit(node.start, frame)
1318 self.write(':')
1319 if node.stop is not None:
1320 self.visit(node.stop, frame)
1321 if node.step is not None:
1322 self.write(':')
1323 self.visit(node.step, frame)
1324
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001325 def visit_Filter(self, node, frame):
Armin Ronacherb9e78752008-05-10 23:36:28 +02001326 self.write(self.filters[node.name] + '(')
Christoph Hack80909862008-04-14 01:35:10 +02001327 func = self.environment.filters.get(node.name)
Armin Ronacher0611e492008-04-25 23:44:14 +02001328 if func is None:
Armin Ronachere2244882008-05-19 09:25:57 +02001329 self.fail('no filter named %r' % node.name, node.lineno)
Christoph Hack80909862008-04-14 01:35:10 +02001330 if getattr(func, 'contextfilter', False):
1331 self.write('context, ')
Armin Ronacher9a027f42008-04-17 11:13:40 +02001332 elif getattr(func, 'environmentfilter', False):
1333 self.write('environment, ')
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001334
1335 # if the filter node is None we are inside a filter block
1336 # and want to write to the current buffer
Armin Ronacher3da90312008-05-23 16:37:28 +02001337 if node.node is not None:
Armin Ronacherfa865fb2008-04-12 22:11:53 +02001338 self.visit(node.node, frame)
Armin Ronacher3da90312008-05-23 16:37:28 +02001339 elif self.environment.autoescape:
1340 self.write('Markup(concat(%s))' % frame.buffer)
1341 else:
1342 self.write('concat(%s)' % frame.buffer)
Armin Ronacherd55ab532008-04-09 16:13:39 +02001343 self.signature(node, frame)
1344 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001345
1346 def visit_Test(self, node, frame):
Armin Ronacherb9e78752008-05-10 23:36:28 +02001347 self.write(self.tests[node.name] + '(')
Armin Ronacher0611e492008-04-25 23:44:14 +02001348 if node.name not in self.environment.tests:
Armin Ronachere2244882008-05-19 09:25:57 +02001349 self.fail('no test named %r' % node.name, node.lineno)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001350 self.visit(node.node, frame)
1351 self.signature(node, frame)
Armin Ronachere791c2a2008-04-07 18:39:54 +02001352 self.write(')')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001353
Armin Ronacher3d8b7842008-04-13 13:16:50 +02001354 def visit_CondExpr(self, node, frame):
Armin Ronacher547d0b62008-07-04 16:35:10 +02001355 def write_expr2():
1356 if node.expr2 is not None:
1357 return self.visit(node.expr2, frame)
1358 self.write('environment.undefined(%r)' % ('the inline if-'
1359 'expression on %s evaluated to false and '
1360 'no else section was defined.' % self.position(node)))
1361
Armin Ronacher3d8b7842008-04-13 13:16:50 +02001362 if not have_condexpr:
1363 self.write('((')
1364 self.visit(node.test, frame)
1365 self.write(') and (')
1366 self.visit(node.expr1, frame)
1367 self.write(',) or (')
Armin Ronacher547d0b62008-07-04 16:35:10 +02001368 write_expr2()
Armin Ronacher3d8b7842008-04-13 13:16:50 +02001369 self.write(',))[0]')
1370 else:
1371 self.write('(')
1372 self.visit(node.expr1, frame)
1373 self.write(' if ')
1374 self.visit(node.test, frame)
1375 self.write(' else ')
Armin Ronacher547d0b62008-07-04 16:35:10 +02001376 write_expr2()
Armin Ronacher3d8b7842008-04-13 13:16:50 +02001377 self.write(')')
1378
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001379 def visit_Call(self, node, frame, forward_caller=False):
Armin Ronacherc63243e2008-04-14 22:53:58 +02001380 if self.environment.sandboxed:
Armin Ronacherfd310492008-05-25 00:16:51 +02001381 self.write('environment.call(context, ')
1382 else:
1383 self.write('context.call(')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001384 self.visit(node.node, frame)
Armin Ronacher105f0dc2008-05-23 16:12:47 +02001385 extra_kwargs = forward_caller and {'caller': 'caller'} or None
Armin Ronacherfd310492008-05-25 00:16:51 +02001386 self.signature(node, frame, extra_kwargs)
Armin Ronacher8efc5222008-04-08 14:47:40 +02001387 self.write(')')
1388
1389 def visit_Keyword(self, node, frame):
Armin Ronacher2e9396b2008-04-16 14:21:57 +02001390 self.write(node.key + '=')
Armin Ronacher8efc5222008-04-08 14:47:40 +02001391 self.visit(node.value, frame)
Armin Ronachered1e0d42008-05-18 20:25:28 +02001392
Armin Ronachera2eb77d2008-05-22 20:28:21 +02001393 # -- Unused nodes for extensions
Armin Ronachered1e0d42008-05-18 20:25:28 +02001394
1395 def visit_MarkSafe(self, node, frame):
1396 self.write('Markup(')
1397 self.visit(node.expr, frame)
1398 self.write(')')
1399
1400 def visit_EnvironmentAttribute(self, node, frame):
1401 self.write('environment.' + node.name)
1402
1403 def visit_ExtensionAttribute(self, node, frame):
Armin Ronacher6df604e2008-05-23 22:18:38 +02001404 self.write('environment.extensions[%r].%s' % (node.identifier, node.name))
Armin Ronachered1e0d42008-05-18 20:25:28 +02001405
1406 def visit_ImportedName(self, node, frame):
1407 self.write(self.import_aliases[node.importname])
1408
1409 def visit_InternalName(self, node, frame):
1410 self.write(node.name)
1411
Armin Ronacher6df604e2008-05-23 22:18:38 +02001412 def visit_ContextReference(self, node, frame):
1413 self.write('context')
1414
Armin Ronachered1e0d42008-05-18 20:25:28 +02001415 def visit_Continue(self, node, frame):
1416 self.writeline('continue', node)
1417
1418 def visit_Break(self, node, frame):
1419 self.writeline('break', node)