utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import inspect
  2. import linecache
  3. import os.path
  4. import sys
  5. import warnings
  6. from pprint import PrettyPrinter, pformat
  7. from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
  8. from asgiref.local import Local
  9. from django.http import QueryDict
  10. from django.template import Node
  11. from django.utils.html import format_html
  12. from django.utils.safestring import SafeString, mark_safe
  13. from debug_toolbar import _stubs as stubs, settings as dt_settings
  14. try:
  15. import threading
  16. except ImportError:
  17. threading = None
  18. _local_data = Local()
  19. def _is_excluded_frame(frame: Any, excluded_modules: Optional[Sequence[str]]) -> bool:
  20. if not excluded_modules:
  21. return False
  22. frame_module = frame.f_globals.get("__name__")
  23. if not isinstance(frame_module, str):
  24. return False
  25. return any(
  26. frame_module == excluded_module
  27. or frame_module.startswith(excluded_module + ".")
  28. for excluded_module in excluded_modules
  29. )
  30. def _stack_trace_deprecation_warning() -> None:
  31. warnings.warn(
  32. "get_stack() and tidy_stacktrace() are deprecated in favor of"
  33. " get_stack_trace()",
  34. DeprecationWarning,
  35. stacklevel=2,
  36. )
  37. def tidy_stacktrace(stack: List[stubs.InspectStack]) -> stubs.TidyStackTrace:
  38. """
  39. Clean up stacktrace and remove all entries that are excluded by the
  40. HIDE_IN_STACKTRACES setting.
  41. ``stack`` should be a list of frame tuples from ``inspect.stack()`` or
  42. ``debug_toolbar.utils.get_stack()``.
  43. """
  44. _stack_trace_deprecation_warning()
  45. trace = []
  46. excluded_modules = dt_settings.get_config()["HIDE_IN_STACKTRACES"]
  47. for frame, path, line_no, func_name, text in (f[:5] for f in stack):
  48. if _is_excluded_frame(frame, excluded_modules):
  49. continue
  50. text = "".join(text).strip() if text else ""
  51. frame_locals = (
  52. pformat(frame.f_locals)
  53. if dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"]
  54. else None
  55. )
  56. trace.append((path, line_no, func_name, text, frame_locals))
  57. return trace
  58. def render_stacktrace(trace: stubs.TidyStackTrace) -> SafeString:
  59. show_locals = dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"]
  60. html = ""
  61. for abspath, lineno, func, code, locals_ in trace:
  62. if os.path.sep in abspath:
  63. directory, filename = abspath.rsplit(os.path.sep, 1)
  64. # We want the separator to appear in the UI so add it back.
  65. directory += os.path.sep
  66. else:
  67. # abspath could be something like "<frozen importlib._bootstrap>"
  68. directory = ""
  69. filename = abspath
  70. html += format_html(
  71. (
  72. '<span class="djdt-path">{}</span>'
  73. + '<span class="djdt-file">{}</span> in'
  74. + ' <span class="djdt-func">{}</span>'
  75. + '(<span class="djdt-lineno">{}</span>)\n'
  76. + ' <span class="djdt-code">{}</span>\n'
  77. ),
  78. directory,
  79. filename,
  80. func,
  81. lineno,
  82. code,
  83. )
  84. if show_locals:
  85. html += format_html(
  86. ' <pre class="djdt-locals">{}</pre>\n',
  87. locals_,
  88. )
  89. html += "\n"
  90. return mark_safe(html)
  91. def get_template_info() -> Optional[Dict[str, Any]]:
  92. template_info = None
  93. cur_frame = sys._getframe().f_back
  94. try:
  95. while cur_frame is not None:
  96. in_utils_module = cur_frame.f_code.co_filename.endswith(
  97. "/debug_toolbar/utils.py"
  98. )
  99. is_get_template_context = (
  100. cur_frame.f_code.co_name == get_template_context.__name__
  101. )
  102. if in_utils_module and is_get_template_context:
  103. # If the method in the stack trace is this one
  104. # then break from the loop as it's being check recursively.
  105. break
  106. elif cur_frame.f_code.co_name == "render":
  107. node = cur_frame.f_locals["self"]
  108. context = cur_frame.f_locals["context"]
  109. if isinstance(node, Node):
  110. template_info = get_template_context(node, context)
  111. break
  112. cur_frame = cur_frame.f_back
  113. except Exception:
  114. pass
  115. del cur_frame
  116. return template_info
  117. def get_template_context(
  118. node: Node, context: stubs.RequestContext, context_lines: int = 3
  119. ) -> Dict[str, Any]:
  120. line, source_lines, name = get_template_source_from_exception_info(node, context)
  121. debug_context = []
  122. start = max(1, line - context_lines)
  123. end = line + 1 + context_lines
  124. for line_num, content in source_lines:
  125. if start <= line_num <= end:
  126. debug_context.append(
  127. {"num": line_num, "content": content, "highlight": (line_num == line)}
  128. )
  129. return {"name": name, "context": debug_context}
  130. def get_template_source_from_exception_info(
  131. node: Node, context: stubs.RequestContext
  132. ) -> Tuple[int, List[Tuple[int, str]], str]:
  133. if context.template.origin == node.origin:
  134. exception_info = context.template.get_exception_info(
  135. Exception("DDT"), node.token
  136. )
  137. else:
  138. exception_info = context.render_context.template.get_exception_info(
  139. Exception("DDT"), node.token
  140. )
  141. line = exception_info["line"]
  142. source_lines = exception_info["source_lines"]
  143. name = exception_info["name"]
  144. return line, source_lines, name
  145. def get_name_from_obj(obj: Any) -> str:
  146. if hasattr(obj, "__name__"):
  147. name = obj.__name__
  148. else:
  149. name = obj.__class__.__name__
  150. if hasattr(obj, "__module__"):
  151. module = obj.__module__
  152. name = f"{module}.{name}"
  153. return name
  154. def getframeinfo(frame: Any, context: int = 1) -> inspect.Traceback:
  155. """
  156. Get information about a frame or traceback object.
  157. A tuple of five things is returned: the filename, the line number of
  158. the current line, the function name, a list of lines of context from
  159. the source code, and the index of the current line within that list.
  160. The optional second argument specifies the number of lines of context
  161. to return, which are centered around the current line.
  162. This originally comes from ``inspect`` but is modified to handle issues
  163. with ``findsource()``.
  164. """
  165. if inspect.istraceback(frame):
  166. lineno = frame.tb_lineno
  167. frame = frame.tb_frame
  168. else:
  169. lineno = frame.f_lineno
  170. if not inspect.isframe(frame):
  171. raise TypeError("arg is not a frame or traceback object")
  172. filename = inspect.getsourcefile(frame) or inspect.getfile(frame)
  173. if context > 0:
  174. start = lineno - 1 - context // 2
  175. try:
  176. lines, lnum = inspect.findsource(frame)
  177. except Exception: # findsource raises platform-dependant exceptions
  178. lines = index = None
  179. else:
  180. start = max(start, 1)
  181. start = max(0, min(start, len(lines) - context))
  182. lines = lines[start : (start + context)]
  183. index = lineno - 1 - start
  184. else:
  185. lines = index = None
  186. return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)
  187. def get_sorted_request_variable(
  188. variable: Union[Dict[str, Any], QueryDict]
  189. ) -> Dict[str, Union[List[Tuple[str, Any]], Any]]:
  190. """
  191. Get a data structure for showing a sorted list of variables from the
  192. request data.
  193. """
  194. try:
  195. if isinstance(variable, dict):
  196. return {"list": [(k, variable.get(k)) for k in sorted(variable)]}
  197. else:
  198. return {"list": [(k, variable.getlist(k)) for k in sorted(variable)]}
  199. except TypeError:
  200. return {"raw": variable}
  201. def get_stack(context=1) -> List[stubs.InspectStack]:
  202. """
  203. Get a list of records for a frame and all higher (calling) frames.
  204. Each record contains a frame object, filename, line number, function
  205. name, a list of lines of context, and index within the context.
  206. Modified version of ``inspect.stack()`` which calls our own ``getframeinfo()``
  207. """
  208. _stack_trace_deprecation_warning()
  209. frame = sys._getframe(1)
  210. framelist = []
  211. while frame:
  212. framelist.append((frame,) + getframeinfo(frame, context))
  213. frame = frame.f_back
  214. return framelist
  215. def _stack_frames(*, skip=0):
  216. skip += 1 # Skip the frame for this generator.
  217. frame = inspect.currentframe()
  218. while frame is not None:
  219. if skip > 0:
  220. skip -= 1
  221. else:
  222. yield frame
  223. frame = frame.f_back
  224. class _StackTraceRecorder:
  225. pretty_printer = PrettyPrinter()
  226. def __init__(self):
  227. self.filename_cache = {}
  228. def get_source_file(self, frame):
  229. frame_filename = frame.f_code.co_filename
  230. value = self.filename_cache.get(frame_filename)
  231. if value is None:
  232. filename = inspect.getsourcefile(frame)
  233. if filename is None:
  234. is_source = False
  235. filename = frame_filename
  236. else:
  237. is_source = True
  238. # Ensure linecache validity the first time this recorder
  239. # encounters the filename in this frame.
  240. linecache.checkcache(filename)
  241. value = (filename, is_source)
  242. self.filename_cache[frame_filename] = value
  243. return value
  244. def get_stack_trace(
  245. self,
  246. *,
  247. excluded_modules: Optional[Sequence[str]] = None,
  248. include_locals: bool = False,
  249. skip: int = 0,
  250. ):
  251. trace = []
  252. skip += 1 # Skip the frame for this method.
  253. for frame in _stack_frames(skip=skip):
  254. if _is_excluded_frame(frame, excluded_modules):
  255. continue
  256. filename, is_source = self.get_source_file(frame)
  257. line_no = frame.f_lineno
  258. func_name = frame.f_code.co_name
  259. if is_source:
  260. module = inspect.getmodule(frame, filename)
  261. module_globals = module.__dict__ if module is not None else None
  262. source_line = linecache.getline(
  263. filename, line_no, module_globals
  264. ).strip()
  265. else:
  266. source_line = ""
  267. if include_locals:
  268. frame_locals = self.pretty_printer.pformat(frame.f_locals)
  269. else:
  270. frame_locals = None
  271. trace.append((filename, line_no, func_name, source_line, frame_locals))
  272. trace.reverse()
  273. return trace
  274. def get_stack_trace(*, skip=0):
  275. """
  276. Return a processed stack trace for the current call stack.
  277. If the ``ENABLE_STACKTRACES`` setting is False, return an empty :class:`list`.
  278. Otherwise return a :class:`list` of processed stack frame tuples (file name, line
  279. number, function name, source line, frame locals) for the current call stack. The
  280. first entry in the list will be for the bottom of the stack and the last entry will
  281. be for the top of the stack.
  282. ``skip`` is an :class:`int` indicating the number of stack frames above the frame
  283. for this function to omit from the stack trace. The default value of ``0`` means
  284. that the entry for the caller of this function will be the last entry in the
  285. returned stack trace.
  286. """
  287. config = dt_settings.get_config()
  288. if not config["ENABLE_STACKTRACES"]:
  289. return []
  290. skip += 1 # Skip the frame for this function.
  291. stack_trace_recorder = getattr(_local_data, "stack_trace_recorder", None)
  292. if stack_trace_recorder is None:
  293. stack_trace_recorder = _StackTraceRecorder()
  294. _local_data.stack_trace_recorder = stack_trace_recorder
  295. return stack_trace_recorder.get_stack_trace(
  296. excluded_modules=config["HIDE_IN_STACKTRACES"],
  297. include_locals=config["ENABLE_STACKTRACES_LOCALS"],
  298. skip=skip,
  299. )
  300. def clear_stack_trace_caches():
  301. if hasattr(_local_data, "stack_trace_recorder"):
  302. del _local_data.stack_trace_recorder
  303. class ThreadCollector:
  304. def __init__(self):
  305. if threading is None:
  306. raise NotImplementedError(
  307. "threading module is not available, "
  308. "this panel cannot be used without it"
  309. )
  310. self.collections = {} # a dictionary that maps threads to collections
  311. def get_collection(self, thread=None):
  312. """
  313. Returns a list of collected items for the provided thread, of if none
  314. is provided, returns a list for the current thread.
  315. """
  316. if thread is None:
  317. thread = threading.current_thread()
  318. if thread not in self.collections:
  319. self.collections[thread] = []
  320. return self.collections[thread]
  321. def clear_collection(self, thread=None):
  322. if thread is None:
  323. thread = threading.current_thread()
  324. if thread in self.collections:
  325. del self.collections[thread]
  326. def collect(self, item, thread=None):
  327. self.get_collection(thread).append(item)