You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1721 lines
64 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.compiler
  4. ~~~~~~~~~~~~~~~
  5. Compiles nodes into python code.
  6. :copyright: (c) 2017 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from itertools import chain
  10. from copy import deepcopy
  11. from keyword import iskeyword as is_python_keyword
  12. from functools import update_wrapper
  13. from jinja2 import nodes
  14. from jinja2.nodes import EvalContext
  15. from jinja2.visitor import NodeVisitor
  16. from jinja2.optimizer import Optimizer
  17. from jinja2.exceptions import TemplateAssertionError
  18. from jinja2.utils import Markup, concat, escape
  19. from jinja2._compat import range_type, text_type, string_types, \
  20. iteritems, NativeStringIO, imap, izip
  21. from jinja2.idtracking import Symbols, VAR_LOAD_PARAMETER, \
  22. VAR_LOAD_RESOLVE, VAR_LOAD_ALIAS, VAR_LOAD_UNDEFINED
  23. operators = {
  24. 'eq': '==',
  25. 'ne': '!=',
  26. 'gt': '>',
  27. 'gteq': '>=',
  28. 'lt': '<',
  29. 'lteq': '<=',
  30. 'in': 'in',
  31. 'notin': 'not in'
  32. }
  33. # what method to iterate over items do we want to use for dict iteration
  34. # in generated code? on 2.x let's go with iteritems, on 3.x with items
  35. if hasattr(dict, 'iteritems'):
  36. dict_item_iter = 'iteritems'
  37. else:
  38. dict_item_iter = 'items'
  39. code_features = ['division']
  40. # does this python version support generator stops? (PEP 0479)
  41. try:
  42. exec('from __future__ import generator_stop')
  43. code_features.append('generator_stop')
  44. except SyntaxError:
  45. pass
  46. # does this python version support yield from?
  47. try:
  48. exec('def f(): yield from x()')
  49. except SyntaxError:
  50. supports_yield_from = False
  51. else:
  52. supports_yield_from = True
  53. def optimizeconst(f):
  54. def new_func(self, node, frame, **kwargs):
  55. # Only optimize if the frame is not volatile
  56. if self.optimized and not frame.eval_ctx.volatile:
  57. new_node = self.optimizer.visit(node, frame.eval_ctx)
  58. if new_node != node:
  59. return self.visit(new_node, frame)
  60. return f(self, node, frame, **kwargs)
  61. return update_wrapper(new_func, f)
  62. def generate(node, environment, name, filename, stream=None,
  63. defer_init=False, optimized=True):
  64. """Generate the python source for a node tree."""
  65. if not isinstance(node, nodes.Template):
  66. raise TypeError('Can\'t compile non template nodes')
  67. generator = environment.code_generator_class(environment, name, filename,
  68. stream, defer_init,
  69. optimized)
  70. generator.visit(node)
  71. if stream is None:
  72. return generator.stream.getvalue()
  73. def has_safe_repr(value):
  74. """Does the node have a safe representation?"""
  75. if value is None or value is NotImplemented or value is Ellipsis:
  76. return True
  77. if type(value) in (bool, int, float, complex, range_type, Markup) + string_types:
  78. return True
  79. if type(value) in (tuple, list, set, frozenset):
  80. for item in value:
  81. if not has_safe_repr(item):
  82. return False
  83. return True
  84. elif type(value) is dict:
  85. for key, value in iteritems(value):
  86. if not has_safe_repr(key):
  87. return False
  88. if not has_safe_repr(value):
  89. return False
  90. return True
  91. return False
  92. def find_undeclared(nodes, names):
  93. """Check if the names passed are accessed undeclared. The return value
  94. is a set of all the undeclared names from the sequence of names found.
  95. """
  96. visitor = UndeclaredNameVisitor(names)
  97. try:
  98. for node in nodes:
  99. visitor.visit(node)
  100. except VisitorExit:
  101. pass
  102. return visitor.undeclared
  103. class MacroRef(object):
  104. def __init__(self, node):
  105. self.node = node
  106. self.accesses_caller = False
  107. self.accesses_kwargs = False
  108. self.accesses_varargs = False
  109. class Frame(object):
  110. """Holds compile time information for us."""
  111. def __init__(self, eval_ctx, parent=None, level=None):
  112. self.eval_ctx = eval_ctx
  113. self.symbols = Symbols(parent and parent.symbols or None,
  114. level=level)
  115. # a toplevel frame is the root + soft frames such as if conditions.
  116. self.toplevel = False
  117. # the root frame is basically just the outermost frame, so no if
  118. # conditions. This information is used to optimize inheritance
  119. # situations.
  120. self.rootlevel = False
  121. # in some dynamic inheritance situations the compiler needs to add
  122. # write tests around output statements.
  123. self.require_output_check = parent and parent.require_output_check
  124. # inside some tags we are using a buffer rather than yield statements.
  125. # this for example affects {% filter %} or {% macro %}. If a frame
  126. # is buffered this variable points to the name of the list used as
  127. # buffer.
  128. self.buffer = None
  129. # the name of the block we're in, otherwise None.
  130. self.block = parent and parent.block or None
  131. # the parent of this frame
  132. self.parent = parent
  133. if parent is not None:
  134. self.buffer = parent.buffer
  135. def copy(self):
  136. """Create a copy of the current one."""
  137. rv = object.__new__(self.__class__)
  138. rv.__dict__.update(self.__dict__)
  139. rv.symbols = self.symbols.copy()
  140. return rv
  141. def inner(self, isolated=False):
  142. """Return an inner frame."""
  143. if isolated:
  144. return Frame(self.eval_ctx, level=self.symbols.level + 1)
  145. return Frame(self.eval_ctx, self)
  146. def soft(self):
  147. """Return a soft frame. A soft frame may not be modified as
  148. standalone thing as it shares the resources with the frame it
  149. was created of, but it's not a rootlevel frame any longer.
  150. This is only used to implement if-statements.
  151. """
  152. rv = self.copy()
  153. rv.rootlevel = False
  154. return rv
  155. __copy__ = copy
  156. class VisitorExit(RuntimeError):
  157. """Exception used by the `UndeclaredNameVisitor` to signal a stop."""
  158. class DependencyFinderVisitor(NodeVisitor):
  159. """A visitor that collects filter and test calls."""
  160. def __init__(self):
  161. self.filters = set()
  162. self.tests = set()
  163. def visit_Filter(self, node):
  164. self.generic_visit(node)
  165. self.filters.add(node.name)
  166. def visit_Test(self, node):
  167. self.generic_visit(node)
  168. self.tests.add(node.name)
  169. def visit_Block(self, node):
  170. """Stop visiting at blocks."""
  171. class UndeclaredNameVisitor(NodeVisitor):
  172. """A visitor that checks if a name is accessed without being
  173. declared. This is different from the frame visitor as it will
  174. not stop at closure frames.
  175. """
  176. def __init__(self, names):
  177. self.names = set(names)
  178. self.undeclared = set()
  179. def visit_Name(self, node):
  180. if node.ctx == 'load' and node.name in self.names:
  181. self.undeclared.add(node.name)
  182. if self.undeclared == self.names:
  183. raise VisitorExit()
  184. else:
  185. self.names.discard(node.name)
  186. def visit_Block(self, node):
  187. """Stop visiting a blocks."""
  188. class CompilerExit(Exception):
  189. """Raised if the compiler encountered a situation where it just
  190. doesn't make sense to further process the code. Any block that
  191. raises such an exception is not further processed.
  192. """
  193. class CodeGenerator(NodeVisitor):
  194. def __init__(self, environment, name, filename, stream=None,
  195. defer_init=False, optimized=True):
  196. if stream is None:
  197. stream = NativeStringIO()
  198. self.environment = environment
  199. self.name = name
  200. self.filename = filename
  201. self.stream = stream
  202. self.created_block_context = False
  203. self.defer_init = defer_init
  204. self.optimized = optimized
  205. if optimized:
  206. self.optimizer = Optimizer(environment)
  207. # aliases for imports
  208. self.import_aliases = {}
  209. # a registry for all blocks. Because blocks are moved out
  210. # into the global python scope they are registered here
  211. self.blocks = {}
  212. # the number of extends statements so far
  213. self.extends_so_far = 0
  214. # some templates have a rootlevel extends. In this case we
  215. # can safely assume that we're a child template and do some
  216. # more optimizations.
  217. self.has_known_extends = False
  218. # the current line number
  219. self.code_lineno = 1
  220. # registry of all filters and tests (global, not block local)
  221. self.tests = {}
  222. self.filters = {}
  223. # the debug information
  224. self.debug_info = []
  225. self._write_debug_info = None
  226. # the number of new lines before the next write()
  227. self._new_lines = 0
  228. # the line number of the last written statement
  229. self._last_line = 0
  230. # true if nothing was written so far.
  231. self._first_write = True
  232. # used by the `temporary_identifier` method to get new
  233. # unique, temporary identifier
  234. self._last_identifier = 0
  235. # the current indentation
  236. self._indentation = 0
  237. # Tracks toplevel assignments
  238. self._assign_stack = []
  239. # Tracks parameter definition blocks
  240. self._param_def_block = []
  241. # Tracks the current context.
  242. self._context_reference_stack = ['context']
  243. # -- Various compilation helpers
  244. def fail(self, msg, lineno):
  245. """Fail with a :exc:`TemplateAssertionError`."""
  246. raise TemplateAssertionError(msg, lineno, self.name, self.filename)
  247. def temporary_identifier(self):
  248. """Get a new unique identifier."""
  249. self._last_identifier += 1
  250. return 't_%d' % self._last_identifier
  251. def buffer(self, frame):
  252. """Enable buffering for the frame from that point onwards."""
  253. frame.buffer = self.temporary_identifier()
  254. self.writeline('%s = []' % frame.buffer)
  255. def return_buffer_contents(self, frame, force_unescaped=False):
  256. """Return the buffer contents of the frame."""
  257. if not force_unescaped:
  258. if frame.eval_ctx.volatile:
  259. self.writeline('if context.eval_ctx.autoescape:')
  260. self.indent()
  261. self.writeline('return Markup(concat(%s))' % frame.buffer)
  262. self.outdent()
  263. self.writeline('else:')
  264. self.indent()
  265. self.writeline('return concat(%s)' % frame.buffer)
  266. self.outdent()
  267. return
  268. elif frame.eval_ctx.autoescape:
  269. self.writeline('return Markup(concat(%s))' % frame.buffer)
  270. return
  271. self.writeline('return concat(%s)' % frame.buffer)
  272. def indent(self):
  273. """Indent by one."""
  274. self._indentation += 1
  275. def outdent(self, step=1):
  276. """Outdent by step."""
  277. self._indentation -= step
  278. def start_write(self, frame, node=None):
  279. """Yield or write into the frame buffer."""
  280. if frame.buffer is None:
  281. self.writeline('yield ', node)
  282. else:
  283. self.writeline('%s.append(' % frame.buffer, node)
  284. def end_write(self, frame):
  285. """End the writing process started by `start_write`."""
  286. if frame.buffer is not None:
  287. self.write(')')
  288. def simple_write(self, s, frame, node=None):
  289. """Simple shortcut for start_write + write + end_write."""
  290. self.start_write(frame, node)
  291. self.write(s)
  292. self.end_write(frame)
  293. def blockvisit(self, nodes, frame):
  294. """Visit a list of nodes as block in a frame. If the current frame
  295. is no buffer a dummy ``if 0: yield None`` is written automatically.
  296. """
  297. try:
  298. self.writeline('pass')
  299. for node in nodes:
  300. self.visit(node, frame)
  301. except CompilerExit:
  302. pass
  303. def write(self, x):
  304. """Write a string into the output stream."""
  305. if self._new_lines:
  306. if not self._first_write:
  307. self.stream.write('\n' * self._new_lines)
  308. self.code_lineno += self._new_lines
  309. if self._write_debug_info is not None:
  310. self.debug_info.append((self._write_debug_info,
  311. self.code_lineno))
  312. self._write_debug_info = None
  313. self._first_write = False
  314. self.stream.write(' ' * self._indentation)
  315. self._new_lines = 0
  316. self.stream.write(x)
  317. def writeline(self, x, node=None, extra=0):
  318. """Combination of newline and write."""
  319. self.newline(node, extra)
  320. self.write(x)
  321. def newline(self, node=None, extra=0):
  322. """Add one or more newlines before the next write."""
  323. self._new_lines = max(self._new_lines, 1 + extra)
  324. if node is not None and node.lineno != self._last_line:
  325. self._write_debug_info = node.lineno
  326. self._last_line = node.lineno
  327. def signature(self, node, frame, extra_kwargs=None):
  328. """Writes a function call to the stream for the current node.
  329. A leading comma is added automatically. The extra keyword
  330. arguments may not include python keywords otherwise a syntax
  331. error could occour. The extra keyword arguments should be given
  332. as python dict.
  333. """
  334. # if any of the given keyword arguments is a python keyword
  335. # we have to make sure that no invalid call is created.
  336. kwarg_workaround = False
  337. for kwarg in chain((x.key for x in node.kwargs), extra_kwargs or ()):
  338. if is_python_keyword(kwarg):
  339. kwarg_workaround = True
  340. break
  341. for arg in node.args:
  342. self.write(', ')
  343. self.visit(arg, frame)
  344. if not kwarg_workaround:
  345. for kwarg in node.kwargs:
  346. self.write(', ')
  347. self.visit(kwarg, frame)
  348. if extra_kwargs is not None:
  349. for key, value in iteritems(extra_kwargs):
  350. self.write(', %s=%s' % (key, value))
  351. if node.dyn_args:
  352. self.write(', *')
  353. self.visit(node.dyn_args, frame)
  354. if kwarg_workaround:
  355. if node.dyn_kwargs is not None:
  356. self.write(', **dict({')
  357. else:
  358. self.write(', **{')
  359. for kwarg in node.kwargs:
  360. self.write('%r: ' % kwarg.key)
  361. self.visit(kwarg.value, frame)
  362. self.write(', ')
  363. if extra_kwargs is not None:
  364. for key, value in iteritems(extra_kwargs):
  365. self.write('%r: %s, ' % (key, value))
  366. if node.dyn_kwargs is not None:
  367. self.write('}, **')
  368. self.visit(node.dyn_kwargs, frame)
  369. self.write(')')
  370. else:
  371. self.write('}')
  372. elif node.dyn_kwargs is not None:
  373. self.write(', **')
  374. self.visit(node.dyn_kwargs, frame)
  375. def pull_dependencies(self, nodes):
  376. """Pull all the dependencies."""
  377. visitor = DependencyFinderVisitor()
  378. for node in nodes:
  379. visitor.visit(node)
  380. for dependency in 'filters', 'tests':
  381. mapping = getattr(self, dependency)
  382. for name in getattr(visitor, dependency):
  383. if name not in mapping:
  384. mapping[name] = self.temporary_identifier()
  385. self.writeline('%s = environment.%s[%r]' %
  386. (mapping[name], dependency, name))
  387. def enter_frame(self, frame):
  388. undefs = []
  389. for target, (action, param) in iteritems(frame.symbols.loads):
  390. if action == VAR_LOAD_PARAMETER:
  391. pass
  392. elif action == VAR_LOAD_RESOLVE:
  393. self.writeline('%s = %s(%r)' %
  394. (target, self.get_resolve_func(), param))
  395. elif action == VAR_LOAD_ALIAS:
  396. self.writeline('%s = %s' % (target, param))
  397. elif action == VAR_LOAD_UNDEFINED:
  398. undefs.append(target)
  399. else:
  400. raise NotImplementedError('unknown load instruction')
  401. if undefs:
  402. self.writeline('%s = missing' % ' = '.join(undefs))
  403. def leave_frame(self, frame, with_python_scope=False):
  404. if not with_python_scope:
  405. undefs = []
  406. for target, _ in iteritems(frame.symbols.loads):
  407. undefs.append(target)
  408. if undefs:
  409. self.writeline('%s = missing' % ' = '.join(undefs))
  410. def func(self, name):
  411. if self.environment.is_async:
  412. return 'async def %s' % name
  413. return 'def %s' % name
  414. def macro_body(self, node, frame):
  415. """Dump the function def of a macro or call block."""
  416. frame = frame.inner()
  417. frame.symbols.analyze_node(node)
  418. macro_ref = MacroRef(node)
  419. explicit_caller = None
  420. skip_special_params = set()
  421. args = []
  422. for idx, arg in enumerate(node.args):
  423. if arg.name == 'caller':
  424. explicit_caller = idx
  425. if arg.name in ('kwargs', 'varargs'):
  426. skip_special_params.add(arg.name)
  427. args.append(frame.symbols.ref(arg.name))
  428. undeclared = find_undeclared(node.body, ('caller', 'kwargs', 'varargs'))
  429. if 'caller' in undeclared:
  430. # In older Jinja2 versions there was a bug that allowed caller
  431. # to retain the special behavior even if it was mentioned in
  432. # the argument list. However thankfully this was only really
  433. # working if it was the last argument. So we are explicitly
  434. # checking this now and error out if it is anywhere else in
  435. # the argument list.
  436. if explicit_caller is not None:
  437. try:
  438. node.defaults[explicit_caller - len(node.args)]
  439. except IndexError:
  440. self.fail('When defining macros or call blocks the '
  441. 'special "caller" argument must be omitted '
  442. 'or be given a default.', node.lineno)
  443. else:
  444. args.append(frame.symbols.declare_parameter('caller'))
  445. macro_ref.accesses_caller = True
  446. if 'kwargs' in undeclared and not 'kwargs' in skip_special_params:
  447. args.append(frame.symbols.declare_parameter('kwargs'))
  448. macro_ref.accesses_kwargs = True
  449. if 'varargs' in undeclared and not 'varargs' in skip_special_params:
  450. args.append(frame.symbols.declare_parameter('varargs'))
  451. macro_ref.accesses_varargs = True
  452. # macros are delayed, they never require output checks
  453. frame.require_output_check = False
  454. frame.symbols.analyze_node(node)
  455. self.writeline('%s(%s):' % (self.func('macro'), ', '.join(args)), node)
  456. self.indent()
  457. self.buffer(frame)
  458. self.enter_frame(frame)
  459. self.push_parameter_definitions(frame)
  460. for idx, arg in enumerate(node.args):
  461. ref = frame.symbols.ref(arg.name)
  462. self.writeline('if %s is missing:' % ref)
  463. self.indent()
  464. try:
  465. default = node.defaults[idx - len(node.args)]
  466. except IndexError:
  467. self.writeline('%s = undefined(%r, name=%r)' % (
  468. ref,
  469. 'parameter %r was not provided' % arg.name,
  470. arg.name))
  471. else:
  472. self.writeline('%s = ' % ref)
  473. self.visit(default, frame)
  474. self.mark_parameter_stored(ref)
  475. self.outdent()
  476. self.pop_parameter_definitions()
  477. self.blockvisit(node.body, frame)
  478. self.return_buffer_contents(frame, force_unescaped=True)
  479. self.leave_frame(frame, with_python_scope=True)
  480. self.outdent()
  481. return frame, macro_ref
  482. def macro_def(self, macro_ref, frame):
  483. """Dump the macro definition for the def created by macro_body."""
  484. arg_tuple = ', '.join(repr(x.name) for x in macro_ref.node.args)
  485. name = getattr(macro_ref.node, 'name', None)
  486. if len(macro_ref.node.args) == 1:
  487. arg_tuple += ','
  488. self.write('Macro(environment, macro, %r, (%s), %r, %r, %r, '
  489. 'context.eval_ctx.autoescape)' %
  490. (name, arg_tuple, macro_ref.accesses_kwargs,
  491. macro_ref.accesses_varargs, macro_ref.accesses_caller))
  492. def position(self, node):
  493. """Return a human readable position for the node."""
  494. rv = 'line %d' % node.lineno
  495. if self.name is not None:
  496. rv += ' in ' + repr(self.name)
  497. return rv
  498. def dump_local_context(self, frame):
  499. return '{%s}' % ', '.join(
  500. '%r: %s' % (name, target) for name, target
  501. in iteritems(frame.symbols.dump_stores()))
  502. def write_commons(self):
  503. """Writes a common preamble that is used by root and block functions.
  504. Primarily this sets up common local helpers and enforces a generator
  505. through a dead branch.
  506. """
  507. self.writeline('resolve = context.resolve_or_missing')
  508. self.writeline('undefined = environment.undefined')
  509. self.writeline('if 0: yield None')
  510. def push_parameter_definitions(self, frame):
  511. """Pushes all parameter targets from the given frame into a local
  512. stack that permits tracking of yet to be assigned parameters. In
  513. particular this enables the optimization from `visit_Name` to skip
  514. undefined expressions for parameters in macros as macros can reference
  515. otherwise unbound parameters.
  516. """
  517. self._param_def_block.append(frame.symbols.dump_param_targets())
  518. def pop_parameter_definitions(self):
  519. """Pops the current parameter definitions set."""
  520. self._param_def_block.pop()
  521. def mark_parameter_stored(self, target):
  522. """Marks a parameter in the current parameter definitions as stored.
  523. This will skip the enforced undefined checks.
  524. """
  525. if self._param_def_block:
  526. self._param_def_block[-1].discard(target)
  527. def push_context_reference(self, target):
  528. self._context_reference_stack.append(target)
  529. def pop_context_reference(self):
  530. self._context_reference_stack.pop()
  531. def get_context_ref(self):
  532. return self._context_reference_stack[-1]
  533. def get_resolve_func(self):
  534. target = self._context_reference_stack[-1]
  535. if target == 'context':
  536. return 'resolve'
  537. return '%s.resolve' % target
  538. def derive_context(self, frame):
  539. return '%s.derived(%s)' % (
  540. self.get_context_ref(),
  541. self.dump_local_context(frame),
  542. )
  543. def parameter_is_undeclared(self, target):
  544. """Checks if a given target is an undeclared parameter."""
  545. if not self._param_def_block:
  546. return False
  547. return target in self._param_def_block[-1]
  548. def push_assign_tracking(self):
  549. """Pushes a new layer for assignment tracking."""
  550. self._assign_stack.append(set())
  551. def pop_assign_tracking(self, frame):
  552. """Pops the topmost level for assignment tracking and updates the
  553. context variables if necessary.
  554. """
  555. vars = self._assign_stack.pop()
  556. if not frame.toplevel or not vars:
  557. return
  558. public_names = [x for x in vars if x[:1] != '_']
  559. if len(vars) == 1:
  560. name = next(iter(vars))
  561. ref = frame.symbols.ref(name)
  562. self.writeline('context.vars[%r] = %s' % (name, ref))
  563. else:
  564. self.writeline('context.vars.update({')
  565. for idx, name in enumerate(vars):
  566. if idx:
  567. self.write(', ')
  568. ref = frame.symbols.ref(name)
  569. self.write('%r: %s' % (name, ref))
  570. self.write('})')
  571. if public_names:
  572. if len(public_names) == 1:
  573. self.writeline('context.exported_vars.add(%r)' %
  574. public_names[0])
  575. else:
  576. self.writeline('context.exported_vars.update((%s))' %
  577. ', '.join(imap(repr, public_names)))
  578. # -- Statement Visitors
  579. def visit_Template(self, node, frame=None):
  580. assert frame is None, 'no root frame allowed'
  581. eval_ctx = EvalContext(self.environment, self.name)
  582. from jinja2.runtime import __all__ as exported
  583. self.writeline('from __future__ import %s' % ', '.join(code_features))
  584. self.writeline('from jinja2.runtime import ' + ', '.join(exported))
  585. if self.environment.is_async:
  586. self.writeline('from jinja2.asyncsupport import auto_await, '
  587. 'auto_aiter, make_async_loop_context')
  588. # if we want a deferred initialization we cannot move the
  589. # environment into a local name
  590. envenv = not self.defer_init and ', environment=environment' or ''
  591. # do we have an extends tag at all? If not, we can save some
  592. # overhead by just not processing any inheritance code.
  593. have_extends = node.find(nodes.Extends) is not None
  594. # find all blocks
  595. for block in node.find_all(nodes.Block):
  596. if block.name in self.blocks:
  597. self.fail('block %r defined twice' % block.name, block.lineno)
  598. self.blocks[block.name] = block
  599. # find all imports and import them
  600. for import_ in node.find_all(nodes.ImportedName):
  601. if import_.importname not in self.import_aliases:
  602. imp = import_.importname
  603. self.import_aliases[imp] = alias = self.temporary_identifier()
  604. if '.' in imp:
  605. module, obj = imp.rsplit('.', 1)
  606. self.writeline('from %s import %s as %s' %
  607. (module, obj, alias))
  608. else:
  609. self.writeline('import %s as %s' % (imp, alias))
  610. # add the load name
  611. self.writeline('name = %r' % self.name)
  612. # generate the root render function.
  613. self.writeline('%s(context, missing=missing%s):' %
  614. (self.func('root'), envenv), extra=1)
  615. self.indent()
  616. self.write_commons()
  617. # process the root
  618. frame = Frame(eval_ctx)
  619. if 'self' in find_undeclared(node.body, ('self',)):
  620. ref = frame.symbols.declare_parameter('self')
  621. self.writeline('%s = TemplateReference(context)' % ref)
  622. frame.symbols.analyze_node(node)
  623. frame.toplevel = frame.rootlevel = True
  624. frame.require_output_check = have_extends and not self.has_known_extends
  625. if have_extends:
  626. self.writeline('parent_template = None')
  627. self.enter_frame(frame)
  628. self.pull_dependencies(node.body)
  629. self.blockvisit(node.body, frame)
  630. self.leave_frame(frame, with_python_scope=True)
  631. self.outdent()
  632. # make sure that the parent root is called.
  633. if have_extends:
  634. if not self.has_known_extends:
  635. self.indent()
  636. self.writeline('if parent_template is not None:')
  637. self.indent()
  638. if supports_yield_from and not self.environment.is_async:
  639. self.writeline('yield from parent_template.'
  640. 'root_render_func(context)')
  641. else:
  642. self.writeline('%sfor event in parent_template.'
  643. 'root_render_func(context):' %
  644. (self.environment.is_async and 'async ' or ''))
  645. self.indent()
  646. self.writeline('yield event')
  647. self.outdent()
  648. self.outdent(1 + (not self.has_known_extends))
  649. # at this point we now have the blocks collected and can visit them too.
  650. for name, block in iteritems(self.blocks):
  651. self.writeline('%s(context, missing=missing%s):' %
  652. (self.func('block_' + name), envenv),
  653. block, 1)
  654. self.indent()
  655. self.write_commons()
  656. # It's important that we do not make this frame a child of the
  657. # toplevel template. This would cause a variety of
  658. # interesting issues with identifier tracking.
  659. block_frame = Frame(eval_ctx)
  660. undeclared = find_undeclared(block.body, ('self', 'super'))
  661. if 'self' in undeclared:
  662. ref = block_frame.symbols.declare_parameter('self')
  663. self.writeline('%s = TemplateReference(context)' % ref)
  664. if 'super' in undeclared:
  665. ref = block_frame.symbols.declare_parameter('super')
  666. self.writeline('%s = context.super(%r, '
  667. 'block_%s)' % (ref, name, name))
  668. block_frame.symbols.analyze_node(block)
  669. block_frame.block = name
  670. self.enter_frame(block_frame)
  671. self.pull_dependencies(block.body)
  672. self.blockvisit(block.body, block_frame)
  673. self.leave_frame(block_frame, with_python_scope=True)
  674. self.outdent()
  675. self.writeline('blocks = {%s}' % ', '.join('%r: block_%s' % (x, x)
  676. for x in self.blocks),
  677. extra=1)
  678. # add a function that returns the debug info
  679. self.writeline('debug_info = %r' % '&'.join('%s=%s' % x for x
  680. in self.debug_info))
  681. def visit_Block(self, node, frame):
  682. """Call a block and register it for the template."""
  683. level = 0
  684. if frame.toplevel:
  685. # if we know that we are a child template, there is no need to
  686. # check if we are one
  687. if self.has_known_extends:
  688. return
  689. if self.extends_so_far > 0:
  690. self.writeline('if parent_template is None:')
  691. self.indent()
  692. level += 1
  693. if node.scoped:
  694. context = self.derive_context(frame)
  695. else:
  696. context = self.get_context_ref()
  697. if supports_yield_from and not self.environment.is_async and \
  698. frame.buffer is None:
  699. self.writeline('yield from context.blocks[%r][0](%s)' % (
  700. node.name, context), node)
  701. else:
  702. loop = self.environment.is_async and 'async for' or 'for'
  703. self.writeline('%s event in context.blocks[%r][0](%s):' % (
  704. loop, node.name, context), node)
  705. self.indent()
  706. self.simple_write('event', frame)
  707. self.outdent()
  708. self.outdent(level)
  709. def visit_Extends(self, node, frame):
  710. """Calls the extender."""
  711. if not frame.toplevel:
  712. self.fail('cannot use extend from a non top-level scope',
  713. node.lineno)
  714. # if the number of extends statements in general is zero so
  715. # far, we don't have to add a check if something extended
  716. # the template before this one.
  717. if self.extends_so_far > 0:
  718. # if we have a known extends we just add a template runtime
  719. # error into the generated code. We could catch that at compile
  720. # time too, but i welcome it not to confuse users by throwing the
  721. # same error at different times just "because we can".
  722. if not self.has_known_extends:
  723. self.writeline('if parent_template is not None:')
  724. self.indent()
  725. self.writeline('raise TemplateRuntimeError(%r)' %
  726. 'extended multiple times')
  727. # if we have a known extends already we don't need that code here
  728. # as we know that the template execution will end here.
  729. if self.has_known_extends:
  730. raise CompilerExit()
  731. else:
  732. self.outdent()
  733. self.writeline('parent_template = environment.get_template(', node)
  734. self.visit(node.template, frame)
  735. self.write(', %r)' % self.name)
  736. self.writeline('for name, parent_block in parent_template.'
  737. 'blocks.%s():' % dict_item_iter)
  738. self.indent()
  739. self.writeline('context.blocks.setdefault(name, []).'
  740. 'append(parent_block)')
  741. self.outdent()
  742. # if this extends statement was in the root level we can take
  743. # advantage of that information and simplify the generated code
  744. # in the top level from this point onwards
  745. if frame.rootlevel:
  746. self.has_known_extends = True
  747. # and now we have one more
  748. self.extends_so_far += 1
  749. def visit_Include(self, node, frame):
  750. """Handles includes."""
  751. if node.ignore_missing:
  752. self.writeline('try:')
  753. self.indent()
  754. func_name = 'get_or_select_template'
  755. if isinstance(node.template, nodes.Const):
  756. if isinstance(node.template.value, string_types):
  757. func_name = 'get_template'
  758. elif isinstance(node.template.value, (tuple, list)):
  759. func_name = 'select_template'
  760. elif isinstance(node.template, (nodes.Tuple, nodes.List)):
  761. func_name = 'select_template'
  762. self.writeline('template = environment.%s(' % func_name, node)
  763. self.visit(node.template, frame)
  764. self.write(', %r)' % self.name)
  765. if node.ignore_missing:
  766. self.outdent()
  767. self.writeline('except TemplateNotFound:')
  768. self.indent()
  769. self.writeline('pass')
  770. self.outdent()
  771. self.writeline('else:')
  772. self.indent()
  773. skip_event_yield = False
  774. if node.with_context:
  775. loop = self.environment.is_async and 'async for' or 'for'
  776. self.writeline('%s event in template.root_render_func('
  777. 'template.new_context(context.get_all(), True, '
  778. '%s)):' % (loop, self.dump_local_context(frame)))
  779. elif self.environment.is_async:
  780. self.writeline('for event in (await '
  781. 'template._get_default_module_async())'
  782. '._body_stream:')
  783. else:
  784. if supports_yield_from:
  785. self.writeline('yield from template._get_default_module()'
  786. '._body_stream')
  787. skip_event_yield = True
  788. else:
  789. self.writeline('for event in template._get_default_module()'
  790. '._body_stream:')
  791. if not skip_event_yield:
  792. self.indent()
  793. self.simple_write('event', frame)
  794. self.outdent()
  795. if node.ignore_missing:
  796. self.outdent()
  797. def visit_Import(self, node, frame):
  798. """Visit regular imports."""
  799. self.writeline('%s = ' % frame.symbols.ref(node.target), node)
  800. if frame.toplevel:
  801. self.write('context.vars[%r] = ' % node.target)
  802. if self.environment.is_async:
  803. self.write('await ')
  804. self.write('environment.get_template(')
  805. self.visit(node.template, frame)
  806. self.write(', %r).' % self.name)
  807. if node.with_context:
  808. self.write('make_module%s(context.get_all(), True, %s)'
  809. % (self.environment.is_async and '_async' or '',
  810. self.dump_local_context(frame)))
  811. elif self.environment.is_async:
  812. self.write('_get_default_module_async()')
  813. else:
  814. self.write('_get_default_module()')
  815. if frame.toplevel and not node.target.startswith('_'):
  816. self.writeline('context.exported_vars.discard(%r)' % node.target)
  817. def visit_FromImport(self, node, frame):
  818. """Visit named imports."""
  819. self.newline(node)
  820. self.write('included_template = %senvironment.get_template('
  821. % (self.environment.is_async and 'await ' or ''))
  822. self.visit(node.template, frame)
  823. self.write(', %r).' % self.name)
  824. if node.with_context:
  825. self.write('make_module%s(context.get_all(), True, %s)'
  826. % (self.environment.is_async and '_async' or '',
  827. self.dump_local_context(frame)))
  828. elif self.environment.is_async:
  829. self.write('_get_default_module_async()')
  830. else:
  831. self.write('_get_default_module()')
  832. var_names = []
  833. discarded_names = []
  834. for name in node.names:
  835. if isinstance(name, tuple):
  836. name, alias = name
  837. else:
  838. alias = name
  839. self.writeline('%s = getattr(included_template, '
  840. '%r, missing)' % (frame.symbols.ref(alias), name))
  841. self.writeline('if %s is missing:' % frame.symbols.ref(alias))
  842. self.indent()
  843. self.writeline('%s = undefined(%r %% '
  844. 'included_template.__name__, '
  845. 'name=%r)' %
  846. (frame.symbols.ref(alias),
  847. 'the template %%r (imported on %s) does '
  848. 'not export the requested name %s' % (
  849. self.position(node),
  850. repr(name)
  851. ), name))
  852. self.outdent()
  853. if frame.toplevel:
  854. var_names.append(alias)
  855. if not alias.startswith('_'):
  856. discarded_names.append(alias)
  857. if var_names:
  858. if len(var_names) == 1:
  859. name = var_names[0]
  860. self.writeline('context.vars[%r] = %s' %
  861. (name, frame.symbols.ref(name)))
  862. else:
  863. self.writeline('context.vars.update({%s})' % ', '.join(
  864. '%r: %s' % (name, frame.symbols.ref(name)) for name in var_names
  865. ))
  866. if discarded_names:
  867. if len(discarded_names) == 1:
  868. self.writeline('context.exported_vars.discard(%r)' %
  869. discarded_names[0])
  870. else:
  871. self.writeline('context.exported_vars.difference_'
  872. 'update((%s))' % ', '.join(imap(repr, discarded_names)))
  873. def visit_For(self, node, frame):
  874. loop_frame = frame.inner()
  875. test_frame = frame.inner()
  876. else_frame = frame.inner()
  877. # try to figure out if we have an extended loop. An extended loop
  878. # is necessary if the loop is in recursive mode if the special loop
  879. # variable is accessed in the body.
  880. extended_loop = node.recursive or 'loop' in \
  881. find_undeclared(node.iter_child_nodes(
  882. only=('body',)), ('loop',))
  883. loop_ref = None
  884. if extended_loop:
  885. loop_ref = loop_frame.symbols.declare_parameter('loop')
  886. loop_frame.symbols.analyze_node(node, for_branch='body')
  887. if node.else_:
  888. else_frame.symbols.analyze_node(node, for_branch='else')
  889. if node.test:
  890. loop_filter_func = self.temporary_identifier()
  891. test_frame.symbols.analyze_node(node, for_branch='test')
  892. self.writeline('%s(fiter):' % self.func(loop_filter_func), node.test)
  893. self.indent()
  894. self.enter_frame(test_frame)
  895. self.writeline(self.environment.is_async and 'async for ' or 'for ')
  896. self.visit(node.target, loop_frame)
  897. self.write(' in ')
  898. self.write(self.environment.is_async and 'auto_aiter(fiter)' or 'fiter')
  899. self.write(':')
  900. self.indent()
  901. self.writeline('if ', node.test)
  902. self.visit(node.test, test_frame)
  903. self.write(':')
  904. self.indent()
  905. self.writeline('yield ')
  906. self.visit(node.target, loop_frame)
  907. self.outdent(3)
  908. self.leave_frame(test_frame, with_python_scope=True)
  909. # if we don't have an recursive loop we have to find the shadowed
  910. # variables at that point. Because loops can be nested but the loop
  911. # variable is a special one we have to enforce aliasing for it.
  912. if node.recursive:
  913. self.writeline('%s(reciter, loop_render_func, depth=0):' %
  914. self.func('loop'), node)
  915. self.indent()
  916. self.buffer(loop_frame)
  917. # Use the same buffer for the else frame
  918. else_frame.buffer = loop_frame.buffer
  919. # make sure the loop variable is a special one and raise a template
  920. # assertion error if a loop tries to write to loop
  921. if extended_loop:
  922. self.writeline('%s = missing' % loop_ref)
  923. for name in node.find_all(nodes.Name):
  924. if name.ctx == 'store' and name.name == 'loop':
  925. self.fail('Can\'t assign to special loop variable '
  926. 'in for-loop target', name.lineno)
  927. if node.else_:
  928. iteration_indicator = self.temporary_identifier()
  929. self.writeline('%s = 1' % iteration_indicator)
  930. self.writeline(self.environment.is_async and 'async for ' or 'for ', node)
  931. self.visit(node.target, loop_frame)
  932. if extended_loop:
  933. if self.environment.is_async:
  934. self.write(', %s in await make_async_loop_context(' % loop_ref)
  935. else:
  936. self.write(', %s in LoopContext(' % loop_ref)
  937. else:
  938. self.write(' in ')
  939. if node.test:
  940. self.write('%s(' % loop_filter_func)
  941. if node.recursive:
  942. self.write('reciter')
  943. else:
  944. if self.environment.is_async and not extended_loop:
  945. self.write('auto_aiter(')
  946. self.visit(node.iter, frame)
  947. if self.environment.is_async and not extended_loop:
  948. self.write(')')
  949. if node.test:
  950. self.write(')')
  951. if node.recursive:
  952. self.write(', undefined, loop_render_func, depth):')
  953. else:
  954. self.write(extended_loop and ', undefined):' or ':')
  955. self.indent()
  956. self.enter_frame(loop_frame)
  957. self.blockvisit(node.body, loop_frame)
  958. if node.else_:
  959. self.writeline('%s = 0' % iteration_indicator)
  960. self.outdent()
  961. self.leave_frame(loop_frame, with_python_scope=node.recursive
  962. and not node.else_)
  963. if node.else_:
  964. self.writeline('if %s:' % iteration_indicator)
  965. self.indent()
  966. self.enter_frame(else_frame)
  967. self.blockvisit(node.else_, else_frame)
  968. self.leave_frame(else_frame)
  969. self.outdent()
  970. # if the node was recursive we have to return the buffer contents
  971. # and start the iteration code
  972. if node.recursive:
  973. self.return_buffer_contents(loop_frame)
  974. self.outdent()
  975. self.start_write(frame, node)
  976. if self.environment.is_async:
  977. self.write('await ')
  978. self.write('loop(')
  979. if self.environment.is_async:
  980. self.write('auto_aiter(')
  981. self.visit(node.iter, frame)
  982. if self.environment.is_async:
  983. self.write(')')
  984. self.write(', loop)')
  985. self.end_write(frame)
  986. def visit_If(self, node, frame):
  987. if_frame = frame.soft()
  988. self.writeline('if ', node)
  989. self.visit(node.test, if_frame)
  990. self.write(':')
  991. self.indent()
  992. self.blockvisit(node.body, if_frame)
  993. self.outdent()
  994. for elif_ in node.elif_:
  995. self.writeline('elif ', elif_)
  996. self.visit(elif_.test, if_frame)
  997. self.write(':')
  998. self.indent()
  999. self.blockvisit(elif_.body, if_frame)
  1000. self.outdent()
  1001. if node.else_:
  1002. self.writeline('else:')
  1003. self.indent()
  1004. self.blockvisit(node.else_, if_frame)
  1005. self.outdent()
  1006. def visit_Macro(self, node, frame):
  1007. macro_frame, macro_ref = self.macro_body(node, frame)
  1008. self.newline()
  1009. if frame.toplevel:
  1010. if not node.name.startswith('_'):
  1011. self.write('context.exported_vars.add(%r)' % node.name)
  1012. ref = frame.symbols.ref(node.name)
  1013. self.writeline('context.vars[%r] = ' % node.name)
  1014. self.write('%s = ' % frame.symbols.ref(node.name))
  1015. self.macro_def(macro_ref, macro_frame)
  1016. def visit_CallBlock(self, node, frame):
  1017. call_frame, macro_ref = self.macro_body(node, frame)
  1018. self.writeline('caller = ')
  1019. self.macro_def(macro_ref, call_frame)
  1020. self.start_write(frame, node)
  1021. self.visit_Call(node.call, frame, forward_caller=True)
  1022. self.end_write(frame)
  1023. def visit_FilterBlock(self, node, frame):
  1024. filter_frame = frame.inner()
  1025. filter_frame.symbols.analyze_node(node)
  1026. self.enter_frame(filter_frame)
  1027. self.buffer(filter_frame)
  1028. self.blockvisit(node.body, filter_frame)
  1029. self.start_write(frame, node)
  1030. self.visit_Filter(node.filter, filter_frame)
  1031. self.end_write(frame)
  1032. self.leave_frame(filter_frame)
  1033. def visit_With(self, node, frame):
  1034. with_frame = frame.inner()
  1035. with_frame.symbols.analyze_node(node)
  1036. self.enter_frame(with_frame)
  1037. for idx, (target, expr) in enumerate(izip(node.targets, node.values)):
  1038. self.newline()
  1039. self.visit(target, with_frame)
  1040. self.write(' = ')
  1041. self.visit(expr, frame)
  1042. self.blockvisit(node.body, with_frame)
  1043. self.leave_frame(with_frame)
  1044. def visit_ExprStmt(self, node, frame):
  1045. self.newline(node)
  1046. self.visit(node.node, frame)
  1047. def visit_Output(self, node, frame):
  1048. # if we have a known extends statement, we don't output anything
  1049. # if we are in a require_output_check section
  1050. if self.has_known_extends and frame.require_output_check:
  1051. return
  1052. allow_constant_finalize = True
  1053. if self.environment.finalize:
  1054. func = self.environment.finalize
  1055. if getattr(func, 'contextfunction', False) or \
  1056. getattr(func, 'evalcontextfunction', False):
  1057. allow_constant_finalize = False
  1058. elif getattr(func, 'environmentfunction', False):
  1059. finalize = lambda x: text_type(
  1060. self.environment.finalize(self.environment, x))
  1061. else:
  1062. finalize = lambda x: text_type(self.environment.finalize(x))
  1063. else:
  1064. finalize = text_type
  1065. # if we are inside a frame that requires output checking, we do so
  1066. outdent_later = False
  1067. if frame.require_output_check:
  1068. self.writeline('if parent_template is None:')
  1069. self.indent()
  1070. outdent_later = True
  1071. # try to evaluate as many chunks as possible into a static
  1072. # string at compile time.
  1073. body = []
  1074. for child in node.nodes:
  1075. try:
  1076. if not allow_constant_finalize:
  1077. raise nodes.Impossible()
  1078. const = child.as_const(frame.eval_ctx)
  1079. except nodes.Impossible:
  1080. body.append(child)
  1081. continue
  1082. # the frame can't be volatile here, becaus otherwise the
  1083. # as_const() function would raise an Impossible exception
  1084. # at that point.
  1085. try:
  1086. if frame.eval_ctx.autoescape:
  1087. if hasattr(const, '__html__'):
  1088. const = const.__html__()
  1089. else:
  1090. const = escape(const)
  1091. const = finalize(const)
  1092. except Exception:
  1093. # if something goes wrong here we evaluate the node
  1094. # at runtime for easier debugging
  1095. body.append(child)
  1096. continue
  1097. if body and isinstance(body[-1], list):
  1098. body[-1].append(const)
  1099. else:
  1100. body.append([const])
  1101. # if we have less than 3 nodes or a buffer we yield or extend/append
  1102. if len(body) < 3 or frame.buffer is not None:
  1103. if frame.buffer is not None:
  1104. # for one item we append, for more we extend
  1105. if len(body) == 1:
  1106. self.writeline('%s.append(' % frame.buffer)
  1107. else:
  1108. self.writeline('%s.extend((' % frame.buffer)
  1109. self.indent()
  1110. for item in body:
  1111. if isinstance(item, list):
  1112. val = repr(concat(item))
  1113. if frame.buffer is None:
  1114. self.writeline('yield ' + val)
  1115. else:
  1116. self.writeline(val + ',')
  1117. else:
  1118. if frame.buffer is None:
  1119. self.writeline('yield ', item)
  1120. else:
  1121. self.newline(item)
  1122. close = 1
  1123. if frame.eval_ctx.volatile:
  1124. self.write('(escape if context.eval_ctx.autoescape'
  1125. ' else to_string)(')
  1126. elif frame.eval_ctx.autoescape:
  1127. self.write('escape(')
  1128. else:
  1129. self.write('to_string(')
  1130. if self.environment.finalize is not None:
  1131. self.write('environment.finalize(')
  1132. if getattr(self.environment.finalize,
  1133. "contextfunction", False):
  1134. self.write('context, ')
  1135. close += 1
  1136. self.visit(item, frame)
  1137. self.write(')' * close)
  1138. if frame.buffer is not None:
  1139. self.write(',')
  1140. if frame.buffer is not None:
  1141. # close the open parentheses
  1142. self.outdent()
  1143. self.writeline(len(body) == 1 and ')' or '))')
  1144. # otherwise we create a format string as this is faster in that case
  1145. else:
  1146. format = []
  1147. arguments = []
  1148. for item in body:
  1149. if isinstance(item, list):
  1150. format.append(concat(item).replace('%', '%%'))
  1151. else:
  1152. format.append('%s')
  1153. arguments.append(item)
  1154. self.writeline('yield ')
  1155. self.write(repr(concat(format)) + ' % (')
  1156. self.indent()
  1157. for argument in arguments:
  1158. self.newline(argument)
  1159. close = 0
  1160. if frame.eval_ctx.volatile:
  1161. self.write('(escape if context.eval_ctx.autoescape else'
  1162. ' to_string)(')
  1163. close += 1
  1164. elif frame.eval_ctx.autoescape:
  1165. self.write('escape(')
  1166. close += 1
  1167. if self.environment.finalize is not None:
  1168. self.write('environment.finalize(')
  1169. if getattr(self.environment.finalize,
  1170. 'contextfunction', False):
  1171. self.write('context, ')
  1172. elif getattr(self.environment.finalize,
  1173. 'evalcontextfunction', False):
  1174. self.write('context.eval_ctx, ')
  1175. elif getattr(self.environment.finalize,
  1176. 'environmentfunction', False):
  1177. self.write('environment, ')
  1178. close += 1
  1179. self.visit(argument, frame)
  1180. self.write(')' * close + ', ')
  1181. self.outdent()
  1182. self.writeline(')')
  1183. if outdent_later:
  1184. self.outdent()
  1185. def visit_Assign(self, node, frame):
  1186. self.push_assign_tracking()
  1187. self.newline(node)
  1188. self.visit(node.target, frame)
  1189. self.write(' = ')
  1190. self.visit(node.node, frame)
  1191. self.pop_assign_tracking(frame)
  1192. def visit_AssignBlock(self, node, frame):
  1193. self.push_assign_tracking()
  1194. block_frame = frame.inner()
  1195. # This is a special case. Since a set block always captures we
  1196. # will disable output checks. This way one can use set blocks
  1197. # toplevel even in extended templates.
  1198. block_frame.require_output_check = False
  1199. block_frame.symbols.analyze_node(node)
  1200. self.enter_frame(block_frame)
  1201. self.buffer(block_frame)
  1202. self.blockvisit(node.body, block_frame)
  1203. self.newline(node)
  1204. self.visit(node.target, frame)
  1205. self.write(' = (Markup if context.eval_ctx.autoescape '
  1206. 'else identity)(')
  1207. if node.filter is not None:
  1208. self.visit_Filter(node.filter, block_frame)
  1209. else:
  1210. self.write('concat(%s)' % block_frame.buffer)
  1211. self.write(')')
  1212. self.pop_assign_tracking(frame)
  1213. self.leave_frame(block_frame)
  1214. # -- Expression Visitors
  1215. def visit_Name(self, node, frame):
  1216. if node.ctx == 'store' and frame.toplevel:
  1217. if self._assign_stack:
  1218. self._assign_stack[-1].add(node.name)
  1219. ref = frame.symbols.ref(node.name)
  1220. # If we are looking up a variable we might have to deal with the
  1221. # case where it's undefined. We can skip that case if the load
  1222. # instruction indicates a parameter which are always defined.
  1223. if node.ctx == 'load':
  1224. load = frame.symbols.find_load(ref)
  1225. if not (load is not None and load[0] == VAR_LOAD_PARAMETER and \
  1226. not self.parameter_is_undeclared(ref)):
  1227. self.write('(undefined(name=%r) if %s is missing else %s)' %
  1228. (node.name, ref, ref))
  1229. return
  1230. self.write(ref)
  1231. def visit_NSRef(self, node, frame):
  1232. # NSRefs can only be used to store values; since they use the normal
  1233. # `foo.bar` notation they will be parsed as a normal attribute access
  1234. # when used anywhere but in a `set` context
  1235. ref = frame.symbols.ref(node.name)
  1236. self.writeline('if not isinstance(%s, Namespace):' % ref)
  1237. self.indent()
  1238. self.writeline('raise TemplateRuntimeError(%r)' %
  1239. 'cannot assign attribute on non-namespace object')
  1240. self.outdent()
  1241. self.writeline('%s[%r]' % (ref, node.attr))
  1242. def visit_Const(self, node, frame):
  1243. val = node.as_const(frame.eval_ctx)
  1244. if isinstance(val, float):
  1245. self.write(str(val))
  1246. else:
  1247. self.write(repr(val))
  1248. def visit_TemplateData(self, node, frame):
  1249. try:
  1250. self.write(repr(node.as_const(frame.eval_ctx)))
  1251. except nodes.Impossible:
  1252. self.write('(Markup if context.eval_ctx.autoescape else identity)(%r)'
  1253. % node.data)
  1254. def visit_Tuple(self, node, frame):
  1255. self.write('(')
  1256. idx = -1
  1257. for idx, item in enumerate(node.items):
  1258. if idx:
  1259. self.write(', ')
  1260. self.visit(item, frame)
  1261. self.write(idx == 0 and ',)' or ')')
  1262. def visit_List(self, node, frame):
  1263. self.write('[')
  1264. for idx, item in enumerate(node.items):
  1265. if idx:
  1266. self.write(', ')
  1267. self.visit(item, frame)
  1268. self.write(']')
  1269. def visit_Dict(self, node, frame):
  1270. self.write('{')
  1271. for idx, item in enumerate(node.items):
  1272. if idx:
  1273. self.write(', ')
  1274. self.visit(item.key, frame)
  1275. self.write(': ')
  1276. self.visit(item.value, frame)
  1277. self.write('}')
  1278. def binop(operator, interceptable=True):
  1279. @optimizeconst
  1280. def visitor(self, node, frame):
  1281. if self.environment.sandboxed and \
  1282. operator in self.environment.intercepted_binops:
  1283. self.write('environment.call_binop(context, %r, ' % operator)
  1284. self.visit(node.left, frame)
  1285. self.write(', ')
  1286. self.visit(node.right, frame)
  1287. else:
  1288. self.write('(')
  1289. self.visit(node.left, frame)
  1290. self.write(' %s ' % operator)
  1291. self.visit(node.right, frame)
  1292. self.write(')')
  1293. return visitor
  1294. def uaop(operator, interceptable=True):
  1295. @optimizeconst
  1296. def visitor(self, node, frame):
  1297. if self.environment.sandboxed and \
  1298. operator in self.environment.intercepted_unops:
  1299. self.write('environment.call_unop(context, %r, ' % operator)
  1300. self.visit(node.node, frame)
  1301. else:
  1302. self.write('(' + operator)
  1303. self.visit(node.node, frame)
  1304. self.write(')')
  1305. return visitor
  1306. visit_Add = binop('+')
  1307. visit_Sub = binop('-')
  1308. visit_Mul = binop('*')
  1309. visit_Div = binop('/')
  1310. visit_FloorDiv = binop('//')
  1311. visit_Pow = binop('**')
  1312. visit_Mod = binop('%')
  1313. visit_And = binop('and', interceptable=False)
  1314. visit_Or = binop('or', interceptable=False)
  1315. visit_Pos = uaop('+')
  1316. visit_Neg = uaop('-')
  1317. visit_Not = uaop('not ', interceptable=False)
  1318. del binop, uaop
  1319. @optimizeconst
  1320. def visit_Concat(self, node, frame):
  1321. if frame.eval_ctx.volatile:
  1322. func_name = '(context.eval_ctx.volatile and' \
  1323. ' markup_join or unicode_join)'
  1324. elif frame.eval_ctx.autoescape:
  1325. func_name = 'markup_join'
  1326. else:
  1327. func_name = 'unicode_join'
  1328. self.write('%s((' % func_name)
  1329. for arg in node.nodes:
  1330. self.visit(arg, frame)
  1331. self.write(', ')
  1332. self.write('))')
  1333. @optimizeconst
  1334. def visit_Compare(self, node, frame):
  1335. self.visit(node.expr, frame)
  1336. for op in node.ops:
  1337. self.visit(op, frame)
  1338. def visit_Operand(self, node, frame):
  1339. self.write(' %s ' % operators[node.op])
  1340. self.visit(node.expr, frame)
  1341. @optimizeconst
  1342. def visit_Getattr(self, node, frame):
  1343. self.write('environment.getattr(')
  1344. self.visit(node.node, frame)
  1345. self.write(', %r)' % node.attr)
  1346. @optimizeconst
  1347. def visit_Getitem(self, node, frame):
  1348. # slices bypass the environment getitem method.
  1349. if isinstance(node.arg, nodes.Slice):
  1350. self.visit(node.node, frame)
  1351. self.write('[')
  1352. self.visit(node.arg, frame)
  1353. self.write(']')
  1354. else:
  1355. self.write('environment.getitem(')
  1356. self.visit(node.node, frame)
  1357. self.write(', ')
  1358. self.visit(node.arg, frame)
  1359. self.write(')')
  1360. def visit_Slice(self, node, frame):
  1361. if node.start is not None:
  1362. self.visit(node.start, frame)
  1363. self.write(':')
  1364. if node.stop is not None:
  1365. self.visit(node.stop, frame)
  1366. if node.step is not None:
  1367. self.write(':')
  1368. self.visit(node.step, frame)
  1369. @optimizeconst
  1370. def visit_Filter(self, node, frame):
  1371. if self.environment.is_async:
  1372. self.write('await auto_await(')
  1373. self.write(self.filters[node.name] + '(')
  1374. func = self.environment.filters.get(node.name)
  1375. if func is None:
  1376. self.fail('no filter named %r' % node.name, node.lineno)
  1377. if getattr(func, 'contextfilter', False):
  1378. self.write('context, ')
  1379. elif getattr(func, 'evalcontextfilter', False):
  1380. self.write('context.eval_ctx, ')
  1381. elif getattr(func, 'environmentfilter', False):
  1382. self.write('environment, ')
  1383. # if the filter node is None we are inside a filter block
  1384. # and want to write to the current buffer
  1385. if node.node is not None:
  1386. self.visit(node.node, frame)
  1387. elif frame.eval_ctx.volatile:
  1388. self.write('(context.eval_ctx.autoescape and'
  1389. ' Markup(concat(%s)) or concat(%s))' %
  1390. (frame.buffer, frame.buffer))
  1391. elif frame.eval_ctx.autoescape:
  1392. self.write('Markup(concat(%s))' % frame.buffer)
  1393. else:
  1394. self.write('concat(%s)' % frame.buffer)
  1395. self.signature(node, frame)
  1396. self.write(')')
  1397. if self.environment.is_async:
  1398. self.write(')')
  1399. @optimizeconst
  1400. def visit_Test(self, node, frame):
  1401. self.write(self.tests[node.name] + '(')
  1402. if node.name not in self.environment.tests:
  1403. self.fail('no test named %r' % node.name, node.lineno)
  1404. self.visit(node.node, frame)
  1405. self.signature(node, frame)
  1406. self.write(')')
  1407. @optimizeconst
  1408. def visit_CondExpr(self, node, frame):
  1409. def write_expr2():
  1410. if node.expr2 is not None:
  1411. return self.visit(node.expr2, frame)
  1412. self.write('undefined(%r)' % ('the inline if-'
  1413. 'expression on %s evaluated to false and '
  1414. 'no else section was defined.' % self.position(node)))
  1415. self.write('(')
  1416. self.visit(node.expr1, frame)
  1417. self.write(' if ')
  1418. self.visit(node.test, frame)
  1419. self.write(' else ')
  1420. write_expr2()
  1421. self.write(')')
  1422. @optimizeconst
  1423. def visit_Call(self, node, frame, forward_caller=False):
  1424. if self.environment.is_async:
  1425. self.write('await auto_await(')
  1426. if self.environment.sandboxed:
  1427. self.write('environment.call(context, ')
  1428. else:
  1429. self.write('context.call(')
  1430. self.visit(node.node, frame)
  1431. extra_kwargs = forward_caller and {'caller': 'caller'} or None
  1432. self.signature(node, frame, extra_kwargs)
  1433. self.write(')')
  1434. if self.environment.is_async:
  1435. self.write(')')
  1436. def visit_Keyword(self, node, frame):
  1437. self.write(node.key + '=')
  1438. self.visit(node.value, frame)
  1439. # -- Unused nodes for extensions
  1440. def visit_MarkSafe(self, node, frame):
  1441. self.write('Markup(')
  1442. self.visit(node.expr, frame)
  1443. self.write(')')
  1444. def visit_MarkSafeIfAutoescape(self, node, frame):
  1445. self.write('(context.eval_ctx.autoescape and Markup or identity)(')
  1446. self.visit(node.expr, frame)
  1447. self.write(')')
  1448. def visit_EnvironmentAttribute(self, node, frame):
  1449. self.write('environment.' + node.name)
  1450. def visit_ExtensionAttribute(self, node, frame):
  1451. self.write('environment.extensions[%r].%s' % (node.identifier, node.name))
  1452. def visit_ImportedName(self, node, frame):
  1453. self.write(self.import_aliases[node.importname])
  1454. def visit_InternalName(self, node, frame):
  1455. self.write(node.name)
  1456. def visit_ContextReference(self, node, frame):
  1457. self.write('context')
  1458. def visit_Continue(self, node, frame):
  1459. self.writeline('continue', node)
  1460. def visit_Break(self, node, frame):
  1461. self.writeline('break', node)
  1462. def visit_Scope(self, node, frame):
  1463. scope_frame = frame.inner()
  1464. scope_frame.symbols.analyze_node(node)
  1465. self.enter_frame(scope_frame)
  1466. self.blockvisit(node.body, scope_frame)
  1467. self.leave_frame(scope_frame)
  1468. def visit_OverlayScope(self, node, frame):
  1469. ctx = self.temporary_identifier()
  1470. self.writeline('%s = %s' % (ctx, self.derive_context(frame)))
  1471. self.writeline('%s.vars = ' % ctx)
  1472. self.visit(node.context, frame)
  1473. self.push_context_reference(ctx)
  1474. scope_frame = frame.inner(isolated=True)
  1475. scope_frame.symbols.analyze_node(node)
  1476. self.enter_frame(scope_frame)
  1477. self.blockvisit(node.body, scope_frame)
  1478. self.leave_frame(scope_frame)
  1479. self.pop_context_reference()
  1480. def visit_EvalContextModifier(self, node, frame):
  1481. for keyword in node.options:
  1482. self.writeline('context.eval_ctx.%s = ' % keyword.key)
  1483. self.visit(keyword.value, frame)
  1484. try:
  1485. val = keyword.value.as_const(frame.eval_ctx)
  1486. except nodes.Impossible:
  1487. frame.eval_ctx.volatile = True
  1488. else:
  1489. setattr(frame.eval_ctx, keyword.key, val)
  1490. def visit_ScopedEvalContextModifier(self, node, frame):
  1491. old_ctx_name = self.temporary_identifier()
  1492. saved_ctx = frame.eval_ctx.save()
  1493. self.writeline('%s = context.eval_ctx.save()' % old_ctx_name)
  1494. self.visit_EvalContextModifier(node, frame)
  1495. for child in node.body:
  1496. self.visit(child, frame)
  1497. frame.eval_ctx.revert(saved_ctx)
  1498. self.writeline('context.eval_ctx.revert(%s)' % old_ctx_name)