codegen.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. # mako/codegen.py
  2. # Copyright 2006-2025 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """provides functionality for rendering a parsetree constructing into module
  7. source code."""
  8. import json
  9. import re
  10. import time
  11. from mako import ast
  12. from mako import exceptions
  13. from mako import filters
  14. from mako import parsetree
  15. from mako import util
  16. from mako.pygen import PythonPrinter
  17. MAGIC_NUMBER = 10
  18. # names which are hardwired into the
  19. # template and are not accessed via the
  20. # context itself
  21. TOPLEVEL_DECLARED = {"UNDEFINED", "STOP_RENDERING"}
  22. RESERVED_NAMES = {"context", "loop"}.union(TOPLEVEL_DECLARED)
  23. def compile( # noqa
  24. node,
  25. uri,
  26. filename=None,
  27. default_filters=None,
  28. buffer_filters=None,
  29. imports=None,
  30. future_imports=None,
  31. source_encoding=None,
  32. generate_magic_comment=True,
  33. strict_undefined=False,
  34. enable_loop=True,
  35. reserved_names=frozenset(),
  36. ):
  37. """Generate module source code given a parsetree node,
  38. uri, and optional source filename"""
  39. buf = util.FastEncodingBuffer()
  40. printer = PythonPrinter(buf)
  41. _GenerateRenderMethod(
  42. printer,
  43. _CompileContext(
  44. uri,
  45. filename,
  46. default_filters,
  47. buffer_filters,
  48. imports,
  49. future_imports,
  50. source_encoding,
  51. generate_magic_comment,
  52. strict_undefined,
  53. enable_loop,
  54. reserved_names,
  55. ),
  56. node,
  57. )
  58. return buf.getvalue()
  59. class _CompileContext:
  60. def __init__(
  61. self,
  62. uri,
  63. filename,
  64. default_filters,
  65. buffer_filters,
  66. imports,
  67. future_imports,
  68. source_encoding,
  69. generate_magic_comment,
  70. strict_undefined,
  71. enable_loop,
  72. reserved_names,
  73. ):
  74. self.uri = uri
  75. self.filename = filename
  76. self.default_filters = default_filters
  77. self.buffer_filters = buffer_filters
  78. self.imports = imports
  79. self.future_imports = future_imports
  80. self.source_encoding = source_encoding
  81. self.generate_magic_comment = generate_magic_comment
  82. self.strict_undefined = strict_undefined
  83. self.enable_loop = enable_loop
  84. self.reserved_names = reserved_names
  85. class _GenerateRenderMethod:
  86. """A template visitor object which generates the
  87. full module source for a template.
  88. """
  89. def __init__(self, printer, compiler, node):
  90. self.printer = printer
  91. self.compiler = compiler
  92. self.node = node
  93. self.identifier_stack = [None]
  94. self.in_def = isinstance(node, (parsetree.DefTag, parsetree.BlockTag))
  95. if self.in_def:
  96. name = "render_%s" % node.funcname
  97. args = node.get_argument_expressions()
  98. filtered = len(node.filter_args.args) > 0
  99. buffered = eval(node.attributes.get("buffered", "False"))
  100. cached = eval(node.attributes.get("cached", "False"))
  101. defs = None
  102. pagetag = None
  103. if node.is_block and not node.is_anonymous:
  104. args += ["**pageargs"]
  105. else:
  106. defs = self.write_toplevel()
  107. pagetag = self.compiler.pagetag
  108. name = "render_body"
  109. if pagetag is not None:
  110. args = pagetag.body_decl.get_argument_expressions()
  111. if not pagetag.body_decl.kwargs:
  112. args += ["**pageargs"]
  113. cached = eval(pagetag.attributes.get("cached", "False"))
  114. self.compiler.enable_loop = self.compiler.enable_loop or eval(
  115. pagetag.attributes.get("enable_loop", "False")
  116. )
  117. else:
  118. args = ["**pageargs"]
  119. cached = False
  120. buffered = filtered = False
  121. if args is None:
  122. args = ["context"]
  123. else:
  124. args = [a for a in ["context"] + args]
  125. self.write_render_callable(
  126. pagetag or node, name, args, buffered, filtered, cached
  127. )
  128. if defs is not None:
  129. for node in defs:
  130. _GenerateRenderMethod(printer, compiler, node)
  131. if not self.in_def:
  132. self.write_metadata_struct()
  133. def write_metadata_struct(self):
  134. self.printer.source_map[self.printer.lineno] = max(
  135. self.printer.source_map
  136. )
  137. struct = {
  138. "filename": self.compiler.filename,
  139. "uri": self.compiler.uri,
  140. "source_encoding": self.compiler.source_encoding,
  141. "line_map": self.printer.source_map,
  142. }
  143. self.printer.writelines(
  144. '"""',
  145. "__M_BEGIN_METADATA",
  146. json.dumps(struct),
  147. "__M_END_METADATA\n" '"""',
  148. )
  149. @property
  150. def identifiers(self):
  151. return self.identifier_stack[-1]
  152. def write_toplevel(self):
  153. """Traverse a template structure for module-level directives and
  154. generate the start of module-level code.
  155. """
  156. inherit = []
  157. namespaces = {}
  158. module_code = []
  159. self.compiler.pagetag = None
  160. class FindTopLevel:
  161. def visitInheritTag(s, node):
  162. inherit.append(node)
  163. def visitNamespaceTag(s, node):
  164. namespaces[node.name] = node
  165. def visitPageTag(s, node):
  166. self.compiler.pagetag = node
  167. def visitCode(s, node):
  168. if node.ismodule:
  169. module_code.append(node)
  170. f = FindTopLevel()
  171. for n in self.node.nodes:
  172. n.accept_visitor(f)
  173. self.compiler.namespaces = namespaces
  174. module_ident = set()
  175. for n in module_code:
  176. module_ident = module_ident.union(n.declared_identifiers())
  177. module_identifiers = _Identifiers(self.compiler)
  178. module_identifiers.declared = module_ident
  179. # module-level names, python code
  180. if (
  181. self.compiler.generate_magic_comment
  182. and self.compiler.source_encoding
  183. ):
  184. self.printer.writeline(
  185. "# -*- coding:%s -*-" % self.compiler.source_encoding
  186. )
  187. if self.compiler.future_imports:
  188. self.printer.writeline(
  189. "from __future__ import %s"
  190. % (", ".join(self.compiler.future_imports),)
  191. )
  192. self.printer.writeline("from mako import runtime, filters, cache")
  193. self.printer.writeline("UNDEFINED = runtime.UNDEFINED")
  194. self.printer.writeline("STOP_RENDERING = runtime.STOP_RENDERING")
  195. self.printer.writeline("__M_dict_builtin = dict")
  196. self.printer.writeline("__M_locals_builtin = locals")
  197. self.printer.writeline("_magic_number = %r" % MAGIC_NUMBER)
  198. self.printer.writeline("_modified_time = %r" % time.time())
  199. self.printer.writeline("_enable_loop = %r" % self.compiler.enable_loop)
  200. self.printer.writeline(
  201. "_template_filename = %r" % self.compiler.filename
  202. )
  203. self.printer.writeline("_template_uri = %r" % self.compiler.uri)
  204. self.printer.writeline(
  205. "_source_encoding = %r" % self.compiler.source_encoding
  206. )
  207. if self.compiler.imports:
  208. buf = ""
  209. for imp in self.compiler.imports:
  210. buf += imp + "\n"
  211. self.printer.writeline(imp)
  212. impcode = ast.PythonCode(
  213. buf,
  214. source="",
  215. lineno=0,
  216. pos=0,
  217. filename="template defined imports",
  218. )
  219. else:
  220. impcode = None
  221. main_identifiers = module_identifiers.branch(self.node)
  222. mit = module_identifiers.topleveldefs
  223. module_identifiers.topleveldefs = mit.union(
  224. main_identifiers.topleveldefs
  225. )
  226. module_identifiers.declared.update(TOPLEVEL_DECLARED)
  227. if impcode:
  228. module_identifiers.declared.update(impcode.declared_identifiers)
  229. self.compiler.identifiers = module_identifiers
  230. self.printer.writeline(
  231. "_exports = %r"
  232. % [n.name for n in main_identifiers.topleveldefs.values()]
  233. )
  234. self.printer.write_blanks(2)
  235. if len(module_code):
  236. self.write_module_code(module_code)
  237. if len(inherit):
  238. self.write_namespaces(namespaces)
  239. self.write_inherit(inherit[-1])
  240. elif len(namespaces):
  241. self.write_namespaces(namespaces)
  242. return list(main_identifiers.topleveldefs.values())
  243. def write_render_callable(
  244. self, node, name, args, buffered, filtered, cached
  245. ):
  246. """write a top-level render callable.
  247. this could be the main render() method or that of a top-level def."""
  248. if self.in_def:
  249. decorator = node.decorator
  250. if decorator:
  251. self.printer.writeline(
  252. "@runtime._decorate_toplevel(%s)" % decorator
  253. )
  254. self.printer.start_source(node.lineno)
  255. self.printer.writelines(
  256. "def %s(%s):" % (name, ",".join(args)),
  257. # push new frame, assign current frame to __M_caller
  258. "__M_caller = context.caller_stack._push_frame()",
  259. "try:",
  260. )
  261. if buffered or filtered or cached:
  262. self.printer.writeline("context._push_buffer()")
  263. self.identifier_stack.append(
  264. self.compiler.identifiers.branch(self.node)
  265. )
  266. if (not self.in_def or self.node.is_block) and "**pageargs" in args:
  267. self.identifier_stack[-1].argument_declared.add("pageargs")
  268. if not self.in_def and (
  269. len(self.identifiers.locally_assigned) > 0
  270. or len(self.identifiers.argument_declared) > 0
  271. ):
  272. self.printer.writeline(
  273. "__M_locals = __M_dict_builtin(%s)"
  274. % ",".join(
  275. [
  276. "%s=%s" % (x, x)
  277. for x in self.identifiers.argument_declared
  278. ]
  279. )
  280. )
  281. self.write_variable_declares(self.identifiers, toplevel=True)
  282. for n in self.node.nodes:
  283. n.accept_visitor(self)
  284. self.write_def_finish(self.node, buffered, filtered, cached)
  285. self.printer.writeline(None)
  286. self.printer.write_blanks(2)
  287. if cached:
  288. self.write_cache_decorator(
  289. node, name, args, buffered, self.identifiers, toplevel=True
  290. )
  291. def write_module_code(self, module_code):
  292. """write module-level template code, i.e. that which
  293. is enclosed in <%! %> tags in the template."""
  294. for n in module_code:
  295. self.printer.write_indented_block(n.text, starting_lineno=n.lineno)
  296. def write_inherit(self, node):
  297. """write the module-level inheritance-determination callable."""
  298. self.printer.writelines(
  299. "def _mako_inherit(template, context):",
  300. "_mako_generate_namespaces(context)",
  301. "return runtime._inherit_from(context, %s, _template_uri)"
  302. % (node.parsed_attributes["file"]),
  303. None,
  304. )
  305. def write_namespaces(self, namespaces):
  306. """write the module-level namespace-generating callable."""
  307. self.printer.writelines(
  308. "def _mako_get_namespace(context, name):",
  309. "try:",
  310. "return context.namespaces[(__name__, name)]",
  311. "except KeyError:",
  312. "_mako_generate_namespaces(context)",
  313. "return context.namespaces[(__name__, name)]",
  314. None,
  315. None,
  316. )
  317. self.printer.writeline("def _mako_generate_namespaces(context):")
  318. for node in namespaces.values():
  319. if "import" in node.attributes:
  320. self.compiler.has_ns_imports = True
  321. self.printer.start_source(node.lineno)
  322. if len(node.nodes):
  323. self.printer.writeline("def make_namespace():")
  324. export = []
  325. identifiers = self.compiler.identifiers.branch(node)
  326. self.in_def = True
  327. class NSDefVisitor:
  328. def visitDefTag(s, node):
  329. s.visitDefOrBase(node)
  330. def visitBlockTag(s, node):
  331. s.visitDefOrBase(node)
  332. def visitDefOrBase(s, node):
  333. if node.is_anonymous:
  334. raise exceptions.CompileException(
  335. "Can't put anonymous blocks inside "
  336. "<%namespace>",
  337. **node.exception_kwargs,
  338. )
  339. self.write_inline_def(node, identifiers, nested=False)
  340. export.append(node.funcname)
  341. vis = NSDefVisitor()
  342. for n in node.nodes:
  343. n.accept_visitor(vis)
  344. self.printer.writeline("return [%s]" % (",".join(export)))
  345. self.printer.writeline(None)
  346. self.in_def = False
  347. callable_name = "make_namespace()"
  348. else:
  349. callable_name = "None"
  350. if "file" in node.parsed_attributes:
  351. self.printer.writeline(
  352. "ns = runtime.TemplateNamespace(%r,"
  353. " context._clean_inheritance_tokens(),"
  354. " templateuri=%s, callables=%s, "
  355. " calling_uri=_template_uri)"
  356. % (
  357. node.name,
  358. node.parsed_attributes.get("file", "None"),
  359. callable_name,
  360. )
  361. )
  362. elif "module" in node.parsed_attributes:
  363. self.printer.writeline(
  364. "ns = runtime.ModuleNamespace(%r,"
  365. " context._clean_inheritance_tokens(),"
  366. " callables=%s, calling_uri=_template_uri,"
  367. " module=%s)"
  368. % (
  369. node.name,
  370. callable_name,
  371. node.parsed_attributes.get("module", "None"),
  372. )
  373. )
  374. else:
  375. self.printer.writeline(
  376. "ns = runtime.Namespace(%r,"
  377. " context._clean_inheritance_tokens(),"
  378. " callables=%s, calling_uri=_template_uri)"
  379. % (node.name, callable_name)
  380. )
  381. if eval(node.attributes.get("inheritable", "False")):
  382. self.printer.writeline("context['self'].%s = ns" % (node.name))
  383. self.printer.writeline(
  384. "context.namespaces[(__name__, %s)] = ns" % repr(node.name)
  385. )
  386. self.printer.write_blanks(1)
  387. if not len(namespaces):
  388. self.printer.writeline("pass")
  389. self.printer.writeline(None)
  390. def write_variable_declares(self, identifiers, toplevel=False, limit=None):
  391. """write variable declarations at the top of a function.
  392. the variable declarations are in the form of callable
  393. definitions for defs and/or name lookup within the
  394. function's context argument. the names declared are based
  395. on the names that are referenced in the function body,
  396. which don't otherwise have any explicit assignment
  397. operation. names that are assigned within the body are
  398. assumed to be locally-scoped variables and are not
  399. separately declared.
  400. for def callable definitions, if the def is a top-level
  401. callable then a 'stub' callable is generated which wraps
  402. the current Context into a closure. if the def is not
  403. top-level, it is fully rendered as a local closure.
  404. """
  405. # collection of all defs available to us in this scope
  406. comp_idents = {c.funcname: c for c in identifiers.defs}
  407. to_write = set()
  408. # write "context.get()" for all variables we are going to
  409. # need that arent in the namespace yet
  410. to_write = to_write.union(identifiers.undeclared)
  411. # write closure functions for closures that we define
  412. # right here
  413. to_write = to_write.union(
  414. [c.funcname for c in identifiers.closuredefs.values()]
  415. )
  416. # remove identifiers that are declared in the argument
  417. # signature of the callable
  418. to_write = to_write.difference(identifiers.argument_declared)
  419. # remove identifiers that we are going to assign to.
  420. # in this way we mimic Python's behavior,
  421. # i.e. assignment to a variable within a block
  422. # means that variable is now a "locally declared" var,
  423. # which cannot be referenced beforehand.
  424. to_write = to_write.difference(identifiers.locally_declared)
  425. if self.compiler.enable_loop:
  426. has_loop = "loop" in to_write
  427. to_write.discard("loop")
  428. else:
  429. has_loop = False
  430. # if a limiting set was sent, constraint to those items in that list
  431. # (this is used for the caching decorator)
  432. if limit is not None:
  433. to_write = to_write.intersection(limit)
  434. if toplevel and getattr(self.compiler, "has_ns_imports", False):
  435. self.printer.writeline("_import_ns = {}")
  436. self.compiler.has_imports = True
  437. for ident, ns in self.compiler.namespaces.items():
  438. if "import" in ns.attributes:
  439. self.printer.writeline(
  440. "_mako_get_namespace(context, %r)."
  441. "_populate(_import_ns, %r)"
  442. % (
  443. ident,
  444. re.split(r"\s*,\s*", ns.attributes["import"]),
  445. )
  446. )
  447. if has_loop:
  448. self.printer.writeline("loop = __M_loop = runtime.LoopStack()")
  449. for ident in to_write:
  450. if ident in comp_idents:
  451. comp = comp_idents[ident]
  452. if comp.is_block:
  453. if not comp.is_anonymous:
  454. self.write_def_decl(comp, identifiers)
  455. else:
  456. self.write_inline_def(comp, identifiers, nested=True)
  457. else:
  458. if comp.is_root():
  459. self.write_def_decl(comp, identifiers)
  460. else:
  461. self.write_inline_def(comp, identifiers, nested=True)
  462. elif ident in self.compiler.namespaces:
  463. self.printer.writeline(
  464. "%s = _mako_get_namespace(context, %r)" % (ident, ident)
  465. )
  466. else:
  467. if getattr(self.compiler, "has_ns_imports", False):
  468. if self.compiler.strict_undefined:
  469. self.printer.writelines(
  470. "%s = _import_ns.get(%r, UNDEFINED)"
  471. % (ident, ident),
  472. "if %s is UNDEFINED:" % ident,
  473. "try:",
  474. "%s = context[%r]" % (ident, ident),
  475. "except KeyError:",
  476. "raise NameError(\"'%s' is not defined\")" % ident,
  477. None,
  478. None,
  479. )
  480. else:
  481. self.printer.writeline(
  482. "%s = _import_ns.get"
  483. "(%r, context.get(%r, UNDEFINED))"
  484. % (ident, ident, ident)
  485. )
  486. else:
  487. if self.compiler.strict_undefined:
  488. self.printer.writelines(
  489. "try:",
  490. "%s = context[%r]" % (ident, ident),
  491. "except KeyError:",
  492. "raise NameError(\"'%s' is not defined\")" % ident,
  493. None,
  494. )
  495. else:
  496. self.printer.writeline(
  497. "%s = context.get(%r, UNDEFINED)" % (ident, ident)
  498. )
  499. self.printer.writeline("__M_writer = context.writer()")
  500. def write_def_decl(self, node, identifiers):
  501. """write a locally-available callable referencing a top-level def"""
  502. funcname = node.funcname
  503. namedecls = node.get_argument_expressions()
  504. nameargs = node.get_argument_expressions(as_call=True)
  505. if not self.in_def and (
  506. len(self.identifiers.locally_assigned) > 0
  507. or len(self.identifiers.argument_declared) > 0
  508. ):
  509. nameargs.insert(0, "context._locals(__M_locals)")
  510. else:
  511. nameargs.insert(0, "context")
  512. self.printer.writeline("def %s(%s):" % (funcname, ",".join(namedecls)))
  513. self.printer.writeline(
  514. "return render_%s(%s)" % (funcname, ",".join(nameargs))
  515. )
  516. self.printer.writeline(None)
  517. def write_inline_def(self, node, identifiers, nested):
  518. """write a locally-available def callable inside an enclosing def."""
  519. namedecls = node.get_argument_expressions()
  520. decorator = node.decorator
  521. if decorator:
  522. self.printer.writeline(
  523. "@runtime._decorate_inline(context, %s)" % decorator
  524. )
  525. self.printer.writeline(
  526. "def %s(%s):" % (node.funcname, ",".join(namedecls))
  527. )
  528. filtered = len(node.filter_args.args) > 0
  529. buffered = eval(node.attributes.get("buffered", "False"))
  530. cached = eval(node.attributes.get("cached", "False"))
  531. self.printer.writelines(
  532. # push new frame, assign current frame to __M_caller
  533. "__M_caller = context.caller_stack._push_frame()",
  534. "try:",
  535. )
  536. if buffered or filtered or cached:
  537. self.printer.writelines("context._push_buffer()")
  538. identifiers = identifiers.branch(node, nested=nested)
  539. self.write_variable_declares(identifiers)
  540. self.identifier_stack.append(identifiers)
  541. for n in node.nodes:
  542. n.accept_visitor(self)
  543. self.identifier_stack.pop()
  544. self.write_def_finish(node, buffered, filtered, cached)
  545. self.printer.writeline(None)
  546. if cached:
  547. self.write_cache_decorator(
  548. node,
  549. node.funcname,
  550. namedecls,
  551. False,
  552. identifiers,
  553. inline=True,
  554. toplevel=False,
  555. )
  556. def write_def_finish(
  557. self, node, buffered, filtered, cached, callstack=True
  558. ):
  559. """write the end section of a rendering function, either outermost or
  560. inline.
  561. this takes into account if the rendering function was filtered,
  562. buffered, etc. and closes the corresponding try: block if any, and
  563. writes code to retrieve captured content, apply filters, send proper
  564. return value."""
  565. if not buffered and not cached and not filtered:
  566. self.printer.writeline("return ''")
  567. if callstack:
  568. self.printer.writelines(
  569. "finally:", "context.caller_stack._pop_frame()", None
  570. )
  571. if buffered or filtered or cached:
  572. if buffered or cached:
  573. # in a caching scenario, don't try to get a writer
  574. # from the context after popping; assume the caching
  575. # implemenation might be using a context with no
  576. # extra buffers
  577. self.printer.writelines(
  578. "finally:", "__M_buf = context._pop_buffer()"
  579. )
  580. else:
  581. self.printer.writelines(
  582. "finally:",
  583. "__M_buf, __M_writer = context._pop_buffer_and_writer()",
  584. )
  585. if callstack:
  586. self.printer.writeline("context.caller_stack._pop_frame()")
  587. s = "__M_buf.getvalue()"
  588. if filtered:
  589. s = self.create_filter_callable(
  590. node.filter_args.args, s, False
  591. )
  592. self.printer.writeline(None)
  593. if buffered and not cached:
  594. s = self.create_filter_callable(
  595. self.compiler.buffer_filters, s, False
  596. )
  597. if buffered or cached:
  598. self.printer.writeline("return %s" % s)
  599. else:
  600. self.printer.writelines("__M_writer(%s)" % s, "return ''")
  601. def write_cache_decorator(
  602. self,
  603. node_or_pagetag,
  604. name,
  605. args,
  606. buffered,
  607. identifiers,
  608. inline=False,
  609. toplevel=False,
  610. ):
  611. """write a post-function decorator to replace a rendering
  612. callable with a cached version of itself."""
  613. self.printer.writeline("__M_%s = %s" % (name, name))
  614. cachekey = node_or_pagetag.parsed_attributes.get(
  615. "cache_key", repr(name)
  616. )
  617. cache_args = {}
  618. if self.compiler.pagetag is not None:
  619. cache_args.update(
  620. (pa[6:], self.compiler.pagetag.parsed_attributes[pa])
  621. for pa in self.compiler.pagetag.parsed_attributes
  622. if pa.startswith("cache_") and pa != "cache_key"
  623. )
  624. cache_args.update(
  625. (pa[6:], node_or_pagetag.parsed_attributes[pa])
  626. for pa in node_or_pagetag.parsed_attributes
  627. if pa.startswith("cache_") and pa != "cache_key"
  628. )
  629. if "timeout" in cache_args:
  630. cache_args["timeout"] = int(eval(cache_args["timeout"]))
  631. self.printer.writeline("def %s(%s):" % (name, ",".join(args)))
  632. # form "arg1, arg2, arg3=arg3, arg4=arg4", etc.
  633. pass_args = [
  634. "%s=%s" % ((a.split("=")[0],) * 2) if "=" in a else a for a in args
  635. ]
  636. self.write_variable_declares(
  637. identifiers,
  638. toplevel=toplevel,
  639. limit=node_or_pagetag.undeclared_identifiers(),
  640. )
  641. if buffered:
  642. s = (
  643. "context.get('local')."
  644. "cache._ctx_get_or_create("
  645. "%s, lambda:__M_%s(%s), context, %s__M_defname=%r)"
  646. % (
  647. cachekey,
  648. name,
  649. ",".join(pass_args),
  650. "".join(
  651. ["%s=%s, " % (k, v) for k, v in cache_args.items()]
  652. ),
  653. name,
  654. )
  655. )
  656. # apply buffer_filters
  657. s = self.create_filter_callable(
  658. self.compiler.buffer_filters, s, False
  659. )
  660. self.printer.writelines("return " + s, None)
  661. else:
  662. self.printer.writelines(
  663. "__M_writer(context.get('local')."
  664. "cache._ctx_get_or_create("
  665. "%s, lambda:__M_%s(%s), context, %s__M_defname=%r))"
  666. % (
  667. cachekey,
  668. name,
  669. ",".join(pass_args),
  670. "".join(
  671. ["%s=%s, " % (k, v) for k, v in cache_args.items()]
  672. ),
  673. name,
  674. ),
  675. "return ''",
  676. None,
  677. )
  678. def create_filter_callable(self, args, target, is_expression):
  679. """write a filter-applying expression based on the filters
  680. present in the given filter names, adjusting for the global
  681. 'default' filter aliases as needed."""
  682. def locate_encode(name):
  683. if re.match(r"decode\..+", name):
  684. return "filters." + name
  685. else:
  686. return filters.DEFAULT_ESCAPES.get(name, name)
  687. if "n" not in args:
  688. if is_expression:
  689. if self.compiler.pagetag:
  690. args = self.compiler.pagetag.filter_args.args + args
  691. if self.compiler.default_filters and "n" not in args:
  692. args = self.compiler.default_filters + args
  693. for e in args:
  694. # if filter given as a function, get just the identifier portion
  695. if e == "n":
  696. continue
  697. m = re.match(r"(.+?)(\(.*\))", e)
  698. if m:
  699. ident, fargs = m.group(1, 2)
  700. f = locate_encode(ident)
  701. e = f + fargs
  702. else:
  703. e = locate_encode(e)
  704. assert e is not None
  705. target = "%s(%s)" % (e, target)
  706. return target
  707. def visitExpression(self, node):
  708. self.printer.start_source(node.lineno)
  709. if (
  710. len(node.escapes)
  711. or (
  712. self.compiler.pagetag is not None
  713. and len(self.compiler.pagetag.filter_args.args)
  714. )
  715. or len(self.compiler.default_filters)
  716. ):
  717. s = self.create_filter_callable(
  718. node.escapes_code.args, "%s" % node.text, True
  719. )
  720. self.printer.writeline("__M_writer(%s)" % s)
  721. else:
  722. self.printer.writeline("__M_writer(%s)" % node.text)
  723. def visitControlLine(self, node):
  724. if node.isend:
  725. self.printer.writeline(None)
  726. if node.has_loop_context:
  727. self.printer.writeline("finally:")
  728. self.printer.writeline("loop = __M_loop._exit()")
  729. self.printer.writeline(None)
  730. else:
  731. self.printer.start_source(node.lineno)
  732. if self.compiler.enable_loop and node.keyword == "for":
  733. text = mangle_mako_loop(node, self.printer)
  734. else:
  735. text = node.text
  736. self.printer.writeline(text)
  737. children = node.get_children()
  738. # this covers the four situations where we want to insert a pass:
  739. # 1) a ternary control line with no children,
  740. # 2) a primary control line with nothing but its own ternary
  741. # and end control lines, and
  742. # 3) any control line with no content other than comments
  743. # 4) the first control block with no content other than comments
  744. def _search_for_control_line():
  745. for c in children:
  746. if isinstance(c, parsetree.Comment):
  747. continue
  748. elif isinstance(c, parsetree.ControlLine):
  749. return True
  750. return False
  751. if (
  752. not children
  753. or all(
  754. isinstance(c, (parsetree.Comment, parsetree.ControlLine))
  755. for c in children
  756. )
  757. and all(
  758. (node.is_ternary(c.keyword) or c.isend)
  759. for c in children
  760. if isinstance(c, parsetree.ControlLine)
  761. )
  762. or _search_for_control_line()
  763. ):
  764. self.printer.writeline("pass")
  765. def visitText(self, node):
  766. self.printer.start_source(node.lineno)
  767. self.printer.writeline("__M_writer(%s)" % repr(node.content))
  768. def visitTextTag(self, node):
  769. filtered = len(node.filter_args.args) > 0
  770. if filtered:
  771. self.printer.writelines(
  772. "__M_writer = context._push_writer()", "try:"
  773. )
  774. for n in node.nodes:
  775. n.accept_visitor(self)
  776. if filtered:
  777. self.printer.writelines(
  778. "finally:",
  779. "__M_buf, __M_writer = context._pop_buffer_and_writer()",
  780. "__M_writer(%s)"
  781. % self.create_filter_callable(
  782. node.filter_args.args, "__M_buf.getvalue()", False
  783. ),
  784. None,
  785. )
  786. def visitCode(self, node):
  787. if not node.ismodule:
  788. self.printer.write_indented_block(
  789. node.text, starting_lineno=node.lineno
  790. )
  791. if not self.in_def and len(self.identifiers.locally_assigned) > 0:
  792. # if we are the "template" def, fudge locally
  793. # declared/modified variables into the "__M_locals" dictionary,
  794. # which is used for def calls within the same template,
  795. # to simulate "enclosing scope"
  796. self.printer.writeline(
  797. "__M_locals_builtin_stored = __M_locals_builtin()"
  798. )
  799. self.printer.writeline(
  800. "__M_locals.update(__M_dict_builtin([(__M_key,"
  801. " __M_locals_builtin_stored[__M_key]) for __M_key in"
  802. " [%s] if __M_key in __M_locals_builtin_stored]))"
  803. % ",".join([repr(x) for x in node.declared_identifiers()])
  804. )
  805. def visitIncludeTag(self, node):
  806. self.printer.start_source(node.lineno)
  807. args = node.attributes.get("args")
  808. if args:
  809. self.printer.writeline(
  810. "runtime._include_file(context, %s, _template_uri, %s)"
  811. % (node.parsed_attributes["file"], args)
  812. )
  813. else:
  814. self.printer.writeline(
  815. "runtime._include_file(context, %s, _template_uri)"
  816. % (node.parsed_attributes["file"])
  817. )
  818. def visitNamespaceTag(self, node):
  819. pass
  820. def visitDefTag(self, node):
  821. pass
  822. def visitBlockTag(self, node):
  823. if node.is_anonymous:
  824. self.printer.writeline("%s()" % node.funcname)
  825. else:
  826. nameargs = node.get_argument_expressions(as_call=True)
  827. nameargs += ["**pageargs"]
  828. self.printer.writeline(
  829. "if 'parent' not in context._data or "
  830. "not hasattr(context._data['parent'], '%s'):" % node.funcname
  831. )
  832. self.printer.writeline(
  833. "context['self'].%s(%s)" % (node.funcname, ",".join(nameargs))
  834. )
  835. self.printer.writeline("\n")
  836. def visitCallNamespaceTag(self, node):
  837. # TODO: we can put namespace-specific checks here, such
  838. # as ensure the given namespace will be imported,
  839. # pre-import the namespace, etc.
  840. self.visitCallTag(node)
  841. def visitCallTag(self, node):
  842. self.printer.writeline("def ccall(caller):")
  843. export = ["body"]
  844. callable_identifiers = self.identifiers.branch(node, nested=True)
  845. body_identifiers = callable_identifiers.branch(node, nested=False)
  846. # we want the 'caller' passed to ccall to be used
  847. # for the body() function, but for other non-body()
  848. # <%def>s within <%call> we want the current caller
  849. # off the call stack (if any)
  850. body_identifiers.add_declared("caller")
  851. self.identifier_stack.append(body_identifiers)
  852. class DefVisitor:
  853. def visitDefTag(s, node):
  854. s.visitDefOrBase(node)
  855. def visitBlockTag(s, node):
  856. s.visitDefOrBase(node)
  857. def visitDefOrBase(s, node):
  858. self.write_inline_def(node, callable_identifiers, nested=False)
  859. if not node.is_anonymous:
  860. export.append(node.funcname)
  861. # remove defs that are within the <%call> from the
  862. # "closuredefs" defined in the body, so they dont render twice
  863. if node.funcname in body_identifiers.closuredefs:
  864. del body_identifiers.closuredefs[node.funcname]
  865. vis = DefVisitor()
  866. for n in node.nodes:
  867. n.accept_visitor(vis)
  868. self.identifier_stack.pop()
  869. bodyargs = node.body_decl.get_argument_expressions()
  870. self.printer.writeline("def body(%s):" % ",".join(bodyargs))
  871. # TODO: figure out best way to specify
  872. # buffering/nonbuffering (at call time would be better)
  873. buffered = False
  874. if buffered:
  875. self.printer.writelines("context._push_buffer()", "try:")
  876. self.write_variable_declares(body_identifiers)
  877. self.identifier_stack.append(body_identifiers)
  878. for n in node.nodes:
  879. n.accept_visitor(self)
  880. self.identifier_stack.pop()
  881. self.write_def_finish(node, buffered, False, False, callstack=False)
  882. self.printer.writelines(None, "return [%s]" % (",".join(export)), None)
  883. self.printer.writelines(
  884. # push on caller for nested call
  885. "context.caller_stack.nextcaller = "
  886. "runtime.Namespace('caller', context, "
  887. "callables=ccall(__M_caller))",
  888. "try:",
  889. )
  890. self.printer.start_source(node.lineno)
  891. self.printer.writelines(
  892. "__M_writer(%s)"
  893. % self.create_filter_callable([], node.expression, True),
  894. "finally:",
  895. "context.caller_stack.nextcaller = None",
  896. None,
  897. )
  898. class _Identifiers:
  899. """tracks the status of identifier names as template code is rendered."""
  900. def __init__(self, compiler, node=None, parent=None, nested=False):
  901. if parent is not None:
  902. # if we are the branch created in write_namespaces(),
  903. # we don't share any context from the main body().
  904. if isinstance(node, parsetree.NamespaceTag):
  905. self.declared = set()
  906. self.topleveldefs = util.SetLikeDict()
  907. else:
  908. # things that have already been declared
  909. # in an enclosing namespace (i.e. names we can just use)
  910. self.declared = (
  911. set(parent.declared)
  912. .union([c.name for c in parent.closuredefs.values()])
  913. .union(parent.locally_declared)
  914. .union(parent.argument_declared)
  915. )
  916. # if these identifiers correspond to a "nested"
  917. # scope, it means whatever the parent identifiers
  918. # had as undeclared will have been declared by that parent,
  919. # and therefore we have them in our scope.
  920. if nested:
  921. self.declared = self.declared.union(parent.undeclared)
  922. # top level defs that are available
  923. self.topleveldefs = util.SetLikeDict(**parent.topleveldefs)
  924. else:
  925. self.declared = set()
  926. self.topleveldefs = util.SetLikeDict()
  927. self.compiler = compiler
  928. # things within this level that are referenced before they
  929. # are declared (e.g. assigned to)
  930. self.undeclared = set()
  931. # things that are declared locally. some of these things
  932. # could be in the "undeclared" list as well if they are
  933. # referenced before declared
  934. self.locally_declared = set()
  935. # assignments made in explicit python blocks.
  936. # these will be propagated to
  937. # the context of local def calls.
  938. self.locally_assigned = set()
  939. # things that are declared in the argument
  940. # signature of the def callable
  941. self.argument_declared = set()
  942. # closure defs that are defined in this level
  943. self.closuredefs = util.SetLikeDict()
  944. self.node = node
  945. if node is not None:
  946. node.accept_visitor(self)
  947. illegal_names = self.compiler.reserved_names.intersection(
  948. self.locally_declared
  949. )
  950. if illegal_names:
  951. raise exceptions.NameConflictError(
  952. "Reserved words declared in template: %s"
  953. % ", ".join(illegal_names)
  954. )
  955. def branch(self, node, **kwargs):
  956. """create a new Identifiers for a new Node, with
  957. this Identifiers as the parent."""
  958. return _Identifiers(self.compiler, node, self, **kwargs)
  959. @property
  960. def defs(self):
  961. return set(self.topleveldefs.union(self.closuredefs).values())
  962. def __repr__(self):
  963. return (
  964. "Identifiers(declared=%r, locally_declared=%r, "
  965. "undeclared=%r, topleveldefs=%r, closuredefs=%r, "
  966. "argumentdeclared=%r)"
  967. % (
  968. list(self.declared),
  969. list(self.locally_declared),
  970. list(self.undeclared),
  971. [c.name for c in self.topleveldefs.values()],
  972. [c.name for c in self.closuredefs.values()],
  973. self.argument_declared,
  974. )
  975. )
  976. def check_declared(self, node):
  977. """update the state of this Identifiers with the undeclared
  978. and declared identifiers of the given node."""
  979. for ident in node.undeclared_identifiers():
  980. if ident != "context" and ident not in self.declared.union(
  981. self.locally_declared
  982. ):
  983. self.undeclared.add(ident)
  984. for ident in node.declared_identifiers():
  985. self.locally_declared.add(ident)
  986. def add_declared(self, ident):
  987. self.declared.add(ident)
  988. if ident in self.undeclared:
  989. self.undeclared.remove(ident)
  990. def visitExpression(self, node):
  991. self.check_declared(node)
  992. def visitControlLine(self, node):
  993. self.check_declared(node)
  994. def visitCode(self, node):
  995. if not node.ismodule:
  996. self.check_declared(node)
  997. self.locally_assigned = self.locally_assigned.union(
  998. node.declared_identifiers()
  999. )
  1000. def visitNamespaceTag(self, node):
  1001. # only traverse into the sub-elements of a
  1002. # <%namespace> tag if we are the branch created in
  1003. # write_namespaces()
  1004. if self.node is node:
  1005. for n in node.nodes:
  1006. n.accept_visitor(self)
  1007. def _check_name_exists(self, collection, node):
  1008. existing = collection.get(node.funcname)
  1009. collection[node.funcname] = node
  1010. if (
  1011. existing is not None
  1012. and existing is not node
  1013. and (node.is_block or existing.is_block)
  1014. ):
  1015. raise exceptions.CompileException(
  1016. "%%def or %%block named '%s' already "
  1017. "exists in this template." % node.funcname,
  1018. **node.exception_kwargs,
  1019. )
  1020. def visitDefTag(self, node):
  1021. if node.is_root() and not node.is_anonymous:
  1022. self._check_name_exists(self.topleveldefs, node)
  1023. elif node is not self.node:
  1024. self._check_name_exists(self.closuredefs, node)
  1025. for ident in node.undeclared_identifiers():
  1026. if ident != "context" and ident not in self.declared.union(
  1027. self.locally_declared
  1028. ):
  1029. self.undeclared.add(ident)
  1030. # visit defs only one level deep
  1031. if node is self.node:
  1032. for ident in node.declared_identifiers():
  1033. self.argument_declared.add(ident)
  1034. for n in node.nodes:
  1035. n.accept_visitor(self)
  1036. def visitBlockTag(self, node):
  1037. if node is not self.node and not node.is_anonymous:
  1038. if isinstance(self.node, parsetree.DefTag):
  1039. raise exceptions.CompileException(
  1040. "Named block '%s' not allowed inside of def '%s'"
  1041. % (node.name, self.node.name),
  1042. **node.exception_kwargs,
  1043. )
  1044. elif isinstance(
  1045. self.node, (parsetree.CallTag, parsetree.CallNamespaceTag)
  1046. ):
  1047. raise exceptions.CompileException(
  1048. "Named block '%s' not allowed inside of <%%call> tag"
  1049. % (node.name,),
  1050. **node.exception_kwargs,
  1051. )
  1052. for ident in node.undeclared_identifiers():
  1053. if ident != "context" and ident not in self.declared.union(
  1054. self.locally_declared
  1055. ):
  1056. self.undeclared.add(ident)
  1057. if not node.is_anonymous:
  1058. self._check_name_exists(self.topleveldefs, node)
  1059. self.undeclared.add(node.funcname)
  1060. elif node is not self.node:
  1061. self._check_name_exists(self.closuredefs, node)
  1062. for ident in node.declared_identifiers():
  1063. self.argument_declared.add(ident)
  1064. for n in node.nodes:
  1065. n.accept_visitor(self)
  1066. def visitTextTag(self, node):
  1067. for ident in node.undeclared_identifiers():
  1068. if ident != "context" and ident not in self.declared.union(
  1069. self.locally_declared
  1070. ):
  1071. self.undeclared.add(ident)
  1072. def visitIncludeTag(self, node):
  1073. self.check_declared(node)
  1074. def visitPageTag(self, node):
  1075. for ident in node.declared_identifiers():
  1076. self.argument_declared.add(ident)
  1077. self.check_declared(node)
  1078. def visitCallNamespaceTag(self, node):
  1079. self.visitCallTag(node)
  1080. def visitCallTag(self, node):
  1081. if node is self.node:
  1082. for ident in node.undeclared_identifiers():
  1083. if ident != "context" and ident not in self.declared.union(
  1084. self.locally_declared
  1085. ):
  1086. self.undeclared.add(ident)
  1087. for ident in node.declared_identifiers():
  1088. self.argument_declared.add(ident)
  1089. for n in node.nodes:
  1090. n.accept_visitor(self)
  1091. else:
  1092. for ident in node.undeclared_identifiers():
  1093. if ident != "context" and ident not in self.declared.union(
  1094. self.locally_declared
  1095. ):
  1096. self.undeclared.add(ident)
  1097. _FOR_LOOP = re.compile(
  1098. r"^for\s+((?:\(?)\s*"
  1099. r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
  1100. r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
  1101. r"(?:\s*,\s*(?:"
  1102. r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
  1103. r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
  1104. r"),??)*\s*(?:\)?))\s+in\s+(.*):"
  1105. )
  1106. def mangle_mako_loop(node, printer):
  1107. """converts a for loop into a context manager wrapped around a for loop
  1108. when access to the `loop` variable has been detected in the for loop body
  1109. """
  1110. loop_variable = LoopVariable()
  1111. node.accept_visitor(loop_variable)
  1112. if loop_variable.detected:
  1113. node.nodes[-1].has_loop_context = True
  1114. match = _FOR_LOOP.match(node.text)
  1115. if match:
  1116. printer.writelines(
  1117. "loop = __M_loop._enter(%s)" % match.group(2),
  1118. "try:"
  1119. # 'with __M_loop(%s) as loop:' % match.group(2)
  1120. )
  1121. text = "for %s in loop:" % match.group(1)
  1122. else:
  1123. raise SyntaxError("Couldn't apply loop context: %s" % node.text)
  1124. else:
  1125. text = node.text
  1126. return text
  1127. class LoopVariable:
  1128. """A node visitor which looks for the name 'loop' within undeclared
  1129. identifiers."""
  1130. def __init__(self):
  1131. self.detected = False
  1132. def _loop_reference_detected(self, node):
  1133. if "loop" in node.undeclared_identifiers():
  1134. self.detected = True
  1135. else:
  1136. for n in node.get_children():
  1137. n.accept_visitor(self)
  1138. def visitControlLine(self, node):
  1139. self._loop_reference_detected(node)
  1140. def visitCode(self, node):
  1141. self._loop_reference_detected(node)
  1142. def visitExpression(self, node):
  1143. self._loop_reference_detected(node)