repr.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. """Object representations for debugging purposes. Unlike the default
  2. repr, these expose more information and produce HTML instead of ASCII.
  3. Together with the CSS and JavaScript of the debugger this gives a
  4. colorful and more compact output.
  5. """
  6. import codecs
  7. import re
  8. import sys
  9. import typing as t
  10. from collections import deque
  11. from traceback import format_exception_only
  12. from markupsafe import escape
  13. missing = object()
  14. _paragraph_re = re.compile(r"(?:\r\n|\r|\n){2,}")
  15. RegexType = type(_paragraph_re)
  16. HELP_HTML = """\
  17. <div class=box>
  18. <h3>%(title)s</h3>
  19. <pre class=help>%(text)s</pre>
  20. </div>\
  21. """
  22. OBJECT_DUMP_HTML = """\
  23. <div class=box>
  24. <h3>%(title)s</h3>
  25. %(repr)s
  26. <table>%(items)s</table>
  27. </div>\
  28. """
  29. def debug_repr(obj: object) -> str:
  30. """Creates a debug repr of an object as HTML string."""
  31. return DebugReprGenerator().repr(obj)
  32. def dump(obj: object = missing) -> None:
  33. """Print the object details to stdout._write (for the interactive
  34. console of the web debugger.
  35. """
  36. gen = DebugReprGenerator()
  37. if obj is missing:
  38. rv = gen.dump_locals(sys._getframe(1).f_locals)
  39. else:
  40. rv = gen.dump_object(obj)
  41. sys.stdout._write(rv) # type: ignore
  42. class _Helper:
  43. """Displays an HTML version of the normal help, for the interactive
  44. debugger only because it requires a patched sys.stdout.
  45. """
  46. def __repr__(self) -> str:
  47. return "Type help(object) for help about object."
  48. def __call__(self, topic: t.Optional[t.Any] = None) -> None:
  49. if topic is None:
  50. sys.stdout._write(f"<span class=help>{self!r}</span>") # type: ignore
  51. return
  52. import pydoc
  53. pydoc.help(topic)
  54. rv = sys.stdout.reset() # type: ignore
  55. if isinstance(rv, bytes):
  56. rv = rv.decode("utf-8", "ignore")
  57. paragraphs = _paragraph_re.split(rv)
  58. if len(paragraphs) > 1:
  59. title = paragraphs[0]
  60. text = "\n\n".join(paragraphs[1:])
  61. else:
  62. title = "Help"
  63. text = paragraphs[0]
  64. sys.stdout._write(HELP_HTML % {"title": title, "text": text}) # type: ignore
  65. helper = _Helper()
  66. def _add_subclass_info(
  67. inner: str, obj: object, base: t.Union[t.Type, t.Tuple[t.Type, ...]]
  68. ) -> str:
  69. if isinstance(base, tuple):
  70. for cls in base:
  71. if type(obj) is cls:
  72. return inner
  73. elif type(obj) is base:
  74. return inner
  75. module = ""
  76. if obj.__class__.__module__ not in ("__builtin__", "exceptions"):
  77. module = f'<span class="module">{obj.__class__.__module__}.</span>'
  78. return f"{module}{type(obj).__name__}({inner})"
  79. def _sequence_repr_maker(
  80. left: str, right: str, base: t.Type, limit: int = 8
  81. ) -> t.Callable[["DebugReprGenerator", t.Iterable, bool], str]:
  82. def proxy(self: "DebugReprGenerator", obj: t.Iterable, recursive: bool) -> str:
  83. if recursive:
  84. return _add_subclass_info(f"{left}...{right}", obj, base)
  85. buf = [left]
  86. have_extended_section = False
  87. for idx, item in enumerate(obj):
  88. if idx:
  89. buf.append(", ")
  90. if idx == limit:
  91. buf.append('<span class="extended">')
  92. have_extended_section = True
  93. buf.append(self.repr(item))
  94. if have_extended_section:
  95. buf.append("</span>")
  96. buf.append(right)
  97. return _add_subclass_info("".join(buf), obj, base)
  98. return proxy
  99. class DebugReprGenerator:
  100. def __init__(self) -> None:
  101. self._stack: t.List[t.Any] = []
  102. list_repr = _sequence_repr_maker("[", "]", list)
  103. tuple_repr = _sequence_repr_maker("(", ")", tuple)
  104. set_repr = _sequence_repr_maker("set([", "])", set)
  105. frozenset_repr = _sequence_repr_maker("frozenset([", "])", frozenset)
  106. deque_repr = _sequence_repr_maker(
  107. '<span class="module">collections.</span>deque([', "])", deque
  108. )
  109. def regex_repr(self, obj: t.Pattern) -> str:
  110. pattern = repr(obj.pattern)
  111. pattern = codecs.decode(pattern, "unicode-escape", "ignore") # type: ignore
  112. pattern = f"r{pattern}"
  113. return f're.compile(<span class="string regex">{pattern}</span>)'
  114. def string_repr(self, obj: t.Union[str, bytes], limit: int = 70) -> str:
  115. buf = ['<span class="string">']
  116. r = repr(obj)
  117. # shorten the repr when the hidden part would be at least 3 chars
  118. if len(r) - limit > 2:
  119. buf.extend(
  120. (
  121. escape(r[:limit]),
  122. '<span class="extended">',
  123. escape(r[limit:]),
  124. "</span>",
  125. )
  126. )
  127. else:
  128. buf.append(escape(r))
  129. buf.append("</span>")
  130. out = "".join(buf)
  131. # if the repr looks like a standard string, add subclass info if needed
  132. if r[0] in "'\"" or (r[0] == "b" and r[1] in "'\""):
  133. return _add_subclass_info(out, obj, (bytes, str))
  134. # otherwise, assume the repr distinguishes the subclass already
  135. return out
  136. def dict_repr(
  137. self,
  138. d: t.Union[t.Dict[int, None], t.Dict[str, int], t.Dict[t.Union[str, int], int]],
  139. recursive: bool,
  140. limit: int = 5,
  141. ) -> str:
  142. if recursive:
  143. return _add_subclass_info("{...}", d, dict)
  144. buf = ["{"]
  145. have_extended_section = False
  146. for idx, (key, value) in enumerate(d.items()):
  147. if idx:
  148. buf.append(", ")
  149. if idx == limit - 1:
  150. buf.append('<span class="extended">')
  151. have_extended_section = True
  152. buf.append(
  153. f'<span class="pair"><span class="key">{self.repr(key)}</span>:'
  154. f' <span class="value">{self.repr(value)}</span></span>'
  155. )
  156. if have_extended_section:
  157. buf.append("</span>")
  158. buf.append("}")
  159. return _add_subclass_info("".join(buf), d, dict)
  160. def object_repr(
  161. self, obj: t.Optional[t.Union[t.Type[dict], t.Callable, t.Type[list]]]
  162. ) -> str:
  163. r = repr(obj)
  164. return f'<span class="object">{escape(r)}</span>'
  165. def dispatch_repr(self, obj: t.Any, recursive: bool) -> str:
  166. if obj is helper:
  167. return f'<span class="help">{helper!r}</span>'
  168. if isinstance(obj, (int, float, complex)):
  169. return f'<span class="number">{obj!r}</span>'
  170. if isinstance(obj, str) or isinstance(obj, bytes):
  171. return self.string_repr(obj)
  172. if isinstance(obj, RegexType):
  173. return self.regex_repr(obj)
  174. if isinstance(obj, list):
  175. return self.list_repr(obj, recursive)
  176. if isinstance(obj, tuple):
  177. return self.tuple_repr(obj, recursive)
  178. if isinstance(obj, set):
  179. return self.set_repr(obj, recursive)
  180. if isinstance(obj, frozenset):
  181. return self.frozenset_repr(obj, recursive)
  182. if isinstance(obj, dict):
  183. return self.dict_repr(obj, recursive)
  184. if isinstance(obj, deque):
  185. return self.deque_repr(obj, recursive)
  186. return self.object_repr(obj)
  187. def fallback_repr(self) -> str:
  188. try:
  189. info = "".join(format_exception_only(*sys.exc_info()[:2]))
  190. except Exception:
  191. info = "?"
  192. return (
  193. '<span class="brokenrepr">'
  194. f"&lt;broken repr ({escape(info.strip())})&gt;</span>"
  195. )
  196. def repr(self, obj: object) -> str:
  197. recursive = False
  198. for item in self._stack:
  199. if item is obj:
  200. recursive = True
  201. break
  202. self._stack.append(obj)
  203. try:
  204. try:
  205. return self.dispatch_repr(obj, recursive)
  206. except Exception:
  207. return self.fallback_repr()
  208. finally:
  209. self._stack.pop()
  210. def dump_object(self, obj: object) -> str:
  211. repr = None
  212. items: t.Optional[t.List[t.Tuple[str, str]]] = None
  213. if isinstance(obj, dict):
  214. title = "Contents of"
  215. items = []
  216. for key, value in obj.items():
  217. if not isinstance(key, str):
  218. items = None
  219. break
  220. items.append((key, self.repr(value)))
  221. if items is None:
  222. items = []
  223. repr = self.repr(obj)
  224. for key in dir(obj):
  225. try:
  226. items.append((key, self.repr(getattr(obj, key))))
  227. except Exception:
  228. pass
  229. title = "Details for"
  230. title += f" {object.__repr__(obj)[1:-1]}"
  231. return self.render_object_dump(items, title, repr)
  232. def dump_locals(self, d: t.Dict[str, t.Any]) -> str:
  233. items = [(key, self.repr(value)) for key, value in d.items()]
  234. return self.render_object_dump(items, "Local variables in frame")
  235. def render_object_dump(
  236. self, items: t.List[t.Tuple[str, str]], title: str, repr: t.Optional[str] = None
  237. ) -> str:
  238. html_items = []
  239. for key, value in items:
  240. html_items.append(f"<tr><th>{escape(key)}<td><pre class=repr>{value}</pre>")
  241. if not html_items:
  242. html_items.append("<tr><td><em>Nothing</em>")
  243. return OBJECT_DUMP_HTML % {
  244. "title": escape(title),
  245. "repr": f"<pre class=repr>{repr if repr else ''}</pre>",
  246. "items": "\n".join(html_items),
  247. }