compiler.py 71 KB

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