tbtools.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import itertools
  2. import linecache
  3. import os
  4. import re
  5. import sys
  6. import sysconfig
  7. import traceback
  8. import typing as t
  9. from markupsafe import escape
  10. from ..utils import cached_property
  11. from .console import Console
  12. HEADER = """\
  13. <!doctype html>
  14. <html lang=en>
  15. <head>
  16. <title>%(title)s // Werkzeug Debugger</title>
  17. <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css">
  18. <link rel="shortcut icon"
  19. href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
  20. <script src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
  21. <script>
  22. var CONSOLE_MODE = %(console)s,
  23. EVALEX = %(evalex)s,
  24. EVALEX_TRUSTED = %(evalex_trusted)s,
  25. SECRET = "%(secret)s";
  26. </script>
  27. </head>
  28. <body style="background-color: #fff">
  29. <div class="debugger">
  30. """
  31. FOOTER = """\
  32. <div class="footer">
  33. Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
  34. friendly Werkzeug powered traceback interpreter.
  35. </div>
  36. </div>
  37. <div class="pin-prompt">
  38. <div class="inner">
  39. <h3>Console Locked</h3>
  40. <p>
  41. The console is locked and needs to be unlocked by entering the PIN.
  42. You can find the PIN printed out on the standard output of your
  43. shell that runs the server.
  44. <form>
  45. <p>PIN:
  46. <input type=text name=pin size=14>
  47. <input type=submit name=btn value="Confirm Pin">
  48. </form>
  49. </div>
  50. </div>
  51. </body>
  52. </html>
  53. """
  54. PAGE_HTML = (
  55. HEADER
  56. + """\
  57. <h1>%(exception_type)s</h1>
  58. <div class="detail">
  59. <p class="errormsg">%(exception)s</p>
  60. </div>
  61. <h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
  62. %(summary)s
  63. <div class="plain">
  64. <p>
  65. This is the Copy/Paste friendly version of the traceback.
  66. </p>
  67. <textarea cols="50" rows="10" name="code" readonly>%(plaintext)s</textarea>
  68. </div>
  69. <div class="explanation">
  70. The debugger caught an exception in your WSGI application. You can now
  71. look at the traceback which led to the error. <span class="nojavascript">
  72. If you enable JavaScript you can also use additional features such as code
  73. execution (if the evalex feature is enabled), automatic pasting of the
  74. exceptions and much more.</span>
  75. </div>
  76. """
  77. + FOOTER
  78. + """
  79. <!--
  80. %(plaintext_cs)s
  81. -->
  82. """
  83. )
  84. CONSOLE_HTML = (
  85. HEADER
  86. + """\
  87. <h1>Interactive Console</h1>
  88. <div class="explanation">
  89. In this console you can execute Python expressions in the context of the
  90. application. The initial namespace was created by the debugger automatically.
  91. </div>
  92. <div class="console"><div class="inner">The Console requires JavaScript.</div></div>
  93. """
  94. + FOOTER
  95. )
  96. SUMMARY_HTML = """\
  97. <div class="%(classes)s">
  98. %(title)s
  99. <ul>%(frames)s</ul>
  100. %(description)s
  101. </div>
  102. """
  103. FRAME_HTML = """\
  104. <div class="frame" id="frame-%(id)d">
  105. <h4>File <cite class="filename">"%(filename)s"</cite>,
  106. line <em class="line">%(lineno)s</em>,
  107. in <code class="function">%(function_name)s</code></h4>
  108. <div class="source %(library)s">%(lines)s</div>
  109. </div>
  110. """
  111. def _process_traceback(
  112. exc: BaseException,
  113. te: t.Optional[traceback.TracebackException] = None,
  114. *,
  115. skip: int = 0,
  116. hide: bool = True,
  117. ) -> traceback.TracebackException:
  118. if te is None:
  119. te = traceback.TracebackException.from_exception(exc, lookup_lines=False)
  120. # Get the frames the same way StackSummary.extract did, in order
  121. # to match each frame with the FrameSummary to augment.
  122. frame_gen = traceback.walk_tb(exc.__traceback__)
  123. limit = getattr(sys, "tracebacklimit", None)
  124. if limit is not None:
  125. if limit < 0:
  126. limit = 0
  127. frame_gen = itertools.islice(frame_gen, limit)
  128. if skip:
  129. frame_gen = itertools.islice(frame_gen, skip, None)
  130. del te.stack[:skip]
  131. new_stack: t.List[DebugFrameSummary] = []
  132. hidden = False
  133. # Match each frame with the FrameSummary that was generated.
  134. # Hide frames using Paste's __traceback_hide__ rules. Replace
  135. # all visible FrameSummary with DebugFrameSummary.
  136. for (f, _), fs in zip(frame_gen, te.stack):
  137. if hide:
  138. hide_value = f.f_locals.get("__traceback_hide__", False)
  139. if hide_value in {"before", "before_and_this"}:
  140. new_stack = []
  141. hidden = False
  142. if hide_value == "before_and_this":
  143. continue
  144. elif hide_value in {"reset", "reset_and_this"}:
  145. hidden = False
  146. if hide_value == "reset_and_this":
  147. continue
  148. elif hide_value in {"after", "after_and_this"}:
  149. hidden = True
  150. if hide_value == "after_and_this":
  151. continue
  152. elif hide_value or hidden:
  153. continue
  154. frame_args: t.Dict[str, t.Any] = {
  155. "filename": fs.filename,
  156. "lineno": fs.lineno,
  157. "name": fs.name,
  158. "locals": f.f_locals,
  159. "globals": f.f_globals,
  160. }
  161. if hasattr(fs, "colno"):
  162. frame_args["colno"] = fs.colno # type: ignore[attr-defined]
  163. frame_args["end_colno"] = fs.end_colno # type: ignore[attr-defined]
  164. new_stack.append(DebugFrameSummary(**frame_args))
  165. # The codeop module is used to compile code from the interactive
  166. # debugger. Hide any codeop frames from the bottom of the traceback.
  167. while new_stack:
  168. module = new_stack[0].global_ns.get("__name__")
  169. if module is None:
  170. module = new_stack[0].local_ns.get("__name__")
  171. if module == "codeop":
  172. del new_stack[0]
  173. else:
  174. break
  175. te.stack[:] = new_stack
  176. if te.__context__:
  177. context_exc = t.cast(BaseException, exc.__context__)
  178. te.__context__ = _process_traceback(context_exc, te.__context__, hide=hide)
  179. if te.__cause__:
  180. cause_exc = t.cast(BaseException, exc.__cause__)
  181. te.__cause__ = _process_traceback(cause_exc, te.__cause__, hide=hide)
  182. return te
  183. class DebugTraceback:
  184. __slots__ = ("_te", "_cache_all_tracebacks", "_cache_all_frames")
  185. def __init__(
  186. self,
  187. exc: BaseException,
  188. te: t.Optional[traceback.TracebackException] = None,
  189. *,
  190. skip: int = 0,
  191. hide: bool = True,
  192. ) -> None:
  193. self._te = _process_traceback(exc, te, skip=skip, hide=hide)
  194. def __str__(self) -> str:
  195. return f"<{type(self).__name__} {self._te}>"
  196. @cached_property
  197. def all_tracebacks(
  198. self,
  199. ) -> t.List[t.Tuple[t.Optional[str], traceback.TracebackException]]:
  200. out = []
  201. current = self._te
  202. while current is not None:
  203. if current.__cause__ is not None:
  204. chained_msg = (
  205. "The above exception was the direct cause of the"
  206. " following exception"
  207. )
  208. chained_exc = current.__cause__
  209. elif current.__context__ is not None and not current.__suppress_context__:
  210. chained_msg = (
  211. "During handling of the above exception, another"
  212. " exception occurred"
  213. )
  214. chained_exc = current.__context__
  215. else:
  216. chained_msg = None
  217. chained_exc = None
  218. out.append((chained_msg, current))
  219. current = chained_exc
  220. return out
  221. @cached_property
  222. def all_frames(self) -> t.List["DebugFrameSummary"]:
  223. return [
  224. f for _, te in self.all_tracebacks for f in te.stack # type: ignore[misc]
  225. ]
  226. def render_traceback_text(self) -> str:
  227. return "".join(self._te.format())
  228. def render_traceback_html(self, include_title: bool = True) -> str:
  229. library_frames = [f.is_library for f in self.all_frames]
  230. mark_library = 0 < sum(library_frames) < len(library_frames)
  231. rows = []
  232. if not library_frames:
  233. classes = "traceback noframe-traceback"
  234. else:
  235. classes = "traceback"
  236. for msg, current in reversed(self.all_tracebacks):
  237. row_parts = []
  238. if msg is not None:
  239. row_parts.append(f'<li><div class="exc-divider">{msg}:</div>')
  240. for frame in current.stack:
  241. frame = t.cast(DebugFrameSummary, frame)
  242. info = f' title="{escape(frame.info)}"' if frame.info else ""
  243. row_parts.append(f"<li{info}>{frame.render_html(mark_library)}")
  244. rows.append("\n".join(row_parts))
  245. is_syntax_error = issubclass(self._te.exc_type, SyntaxError)
  246. if include_title:
  247. if is_syntax_error:
  248. title = "Syntax Error"
  249. else:
  250. title = "Traceback <em>(most recent call last)</em>:"
  251. else:
  252. title = ""
  253. exc_full = escape("".join(self._te.format_exception_only()))
  254. if is_syntax_error:
  255. description = f"<pre class=syntaxerror>{exc_full}</pre>"
  256. else:
  257. description = f"<blockquote>{exc_full}</blockquote>"
  258. return SUMMARY_HTML % {
  259. "classes": classes,
  260. "title": f"<h3>{title}</h3>",
  261. "frames": "\n".join(rows),
  262. "description": description,
  263. }
  264. def render_debugger_html(
  265. self, evalex: bool, secret: str, evalex_trusted: bool
  266. ) -> str:
  267. exc_lines = list(self._te.format_exception_only())
  268. plaintext = "".join(self._te.format())
  269. return PAGE_HTML % {
  270. "evalex": "true" if evalex else "false",
  271. "evalex_trusted": "true" if evalex_trusted else "false",
  272. "console": "false",
  273. "title": exc_lines[0],
  274. "exception": escape("".join(exc_lines)),
  275. "exception_type": escape(self._te.exc_type.__name__),
  276. "summary": self.render_traceback_html(include_title=False),
  277. "plaintext": escape(plaintext),
  278. "plaintext_cs": re.sub("-{2,}", "-", plaintext),
  279. "secret": secret,
  280. }
  281. class DebugFrameSummary(traceback.FrameSummary):
  282. """A :class:`traceback.FrameSummary` that can evaluate code in the
  283. frame's namespace.
  284. """
  285. __slots__ = (
  286. "local_ns",
  287. "global_ns",
  288. "_cache_info",
  289. "_cache_is_library",
  290. "_cache_console",
  291. )
  292. def __init__(
  293. self,
  294. *,
  295. locals: t.Dict[str, t.Any],
  296. globals: t.Dict[str, t.Any],
  297. **kwargs: t.Any,
  298. ) -> None:
  299. super().__init__(locals=None, **kwargs)
  300. self.local_ns = locals
  301. self.global_ns = globals
  302. @cached_property
  303. def info(self) -> t.Optional[str]:
  304. return self.local_ns.get("__traceback_info__")
  305. @cached_property
  306. def is_library(self) -> bool:
  307. return any(
  308. self.filename.startswith((path, os.path.realpath(path)))
  309. for path in sysconfig.get_paths().values()
  310. )
  311. @cached_property
  312. def console(self) -> Console:
  313. return Console(self.global_ns, self.local_ns)
  314. def eval(self, code: str) -> t.Any:
  315. return self.console.eval(code)
  316. def render_html(self, mark_library: bool) -> str:
  317. context = 5
  318. lines = linecache.getlines(self.filename)
  319. line_idx = self.lineno - 1 # type: ignore[operator]
  320. start_idx = max(0, line_idx - context)
  321. stop_idx = min(len(lines), line_idx + context + 1)
  322. rendered_lines = []
  323. def render_line(line: str, cls: str) -> None:
  324. line = line.expandtabs().rstrip()
  325. stripped_line = line.strip()
  326. prefix = len(line) - len(stripped_line)
  327. colno = getattr(self, "colno", 0)
  328. end_colno = getattr(self, "end_colno", 0)
  329. if cls == "current" and colno and end_colno:
  330. arrow = (
  331. f'\n<span class="ws">{" " * prefix}</span>'
  332. f'{" " * (colno - prefix)}{"^" * (end_colno - colno)}'
  333. )
  334. else:
  335. arrow = ""
  336. rendered_lines.append(
  337. f'<pre class="line {cls}"><span class="ws">{" " * prefix}</span>'
  338. f"{escape(stripped_line) if stripped_line else ' '}"
  339. f"{arrow if arrow else ''}</pre>"
  340. )
  341. if lines:
  342. for line in lines[start_idx:line_idx]:
  343. render_line(line, "before")
  344. render_line(lines[line_idx], "current")
  345. for line in lines[line_idx + 1 : stop_idx]:
  346. render_line(line, "after")
  347. return FRAME_HTML % {
  348. "id": id(self),
  349. "filename": escape(self.filename),
  350. "lineno": self.lineno,
  351. "function_name": escape(self.name),
  352. "lines": "\n".join(rendered_lines),
  353. "library": "library" if mark_library and self.is_library else "",
  354. }
  355. def render_console_html(secret: str, evalex_trusted: bool) -> str:
  356. return CONSOLE_HTML % {
  357. "evalex": "true",
  358. "evalex_trusted": "true" if evalex_trusted else "false",
  359. "console": "true",
  360. "title": "Console",
  361. "secret": secret,
  362. }