saferepr.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import pprint
  2. import reprlib
  3. from typing import Any
  4. from typing import Dict
  5. from typing import IO
  6. from typing import Optional
  7. def _try_repr_or_str(obj: object) -> str:
  8. try:
  9. return repr(obj)
  10. except (KeyboardInterrupt, SystemExit):
  11. raise
  12. except BaseException:
  13. return f'{type(obj).__name__}("{obj}")'
  14. def _format_repr_exception(exc: BaseException, obj: object) -> str:
  15. try:
  16. exc_info = _try_repr_or_str(exc)
  17. except (KeyboardInterrupt, SystemExit):
  18. raise
  19. except BaseException as exc:
  20. exc_info = f"unpresentable exception ({_try_repr_or_str(exc)})"
  21. return "<[{} raised in repr()] {} object at 0x{:x}>".format(
  22. exc_info, type(obj).__name__, id(obj)
  23. )
  24. def _ellipsize(s: str, maxsize: int) -> str:
  25. if len(s) > maxsize:
  26. i = max(0, (maxsize - 3) // 2)
  27. j = max(0, maxsize - 3 - i)
  28. return s[:i] + "..." + s[len(s) - j :]
  29. return s
  30. class SafeRepr(reprlib.Repr):
  31. """
  32. repr.Repr that limits the resulting size of repr() and includes
  33. information on exceptions raised during the call.
  34. """
  35. def __init__(self, maxsize: Optional[int], use_ascii: bool = False) -> None:
  36. """
  37. :param maxsize:
  38. If not None, will truncate the resulting repr to that specific size, using ellipsis
  39. somewhere in the middle to hide the extra text.
  40. If None, will not impose any size limits on the returning repr.
  41. """
  42. super().__init__()
  43. # ``maxstring`` is used by the superclass, and needs to be an int; using a
  44. # very large number in case maxsize is None, meaning we want to disable
  45. # truncation.
  46. self.maxstring = maxsize if maxsize is not None else 1_000_000_000
  47. self.maxsize = maxsize
  48. self.use_ascii = use_ascii
  49. def repr(self, x: object) -> str:
  50. try:
  51. if self.use_ascii:
  52. s = ascii(x)
  53. else:
  54. s = super().repr(x)
  55. except (KeyboardInterrupt, SystemExit):
  56. raise
  57. except BaseException as exc:
  58. s = _format_repr_exception(exc, x)
  59. if self.maxsize is not None:
  60. s = _ellipsize(s, self.maxsize)
  61. return s
  62. def repr_instance(self, x: object, level: int) -> str:
  63. try:
  64. s = repr(x)
  65. except (KeyboardInterrupt, SystemExit):
  66. raise
  67. except BaseException as exc:
  68. s = _format_repr_exception(exc, x)
  69. if self.maxsize is not None:
  70. s = _ellipsize(s, self.maxsize)
  71. return s
  72. def safeformat(obj: object) -> str:
  73. """Return a pretty printed string for the given object.
  74. Failing __repr__ functions of user instances will be represented
  75. with a short exception info.
  76. """
  77. try:
  78. return pprint.pformat(obj)
  79. except Exception as exc:
  80. return _format_repr_exception(exc, obj)
  81. # Maximum size of overall repr of objects to display during assertion errors.
  82. DEFAULT_REPR_MAX_SIZE = 240
  83. def saferepr(
  84. obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False
  85. ) -> str:
  86. """Return a size-limited safe repr-string for the given object.
  87. Failing __repr__ functions of user instances will be represented
  88. with a short exception info and 'saferepr' generally takes
  89. care to never raise exceptions itself.
  90. This function is a wrapper around the Repr/reprlib functionality of the
  91. stdlib.
  92. """
  93. return SafeRepr(maxsize, use_ascii).repr(obj)
  94. def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
  95. """Return an unlimited-size safe repr-string for the given object.
  96. As with saferepr, failing __repr__ functions of user instances
  97. will be represented with a short exception info.
  98. This function is a wrapper around simple repr.
  99. Note: a cleaner solution would be to alter ``saferepr``this way
  100. when maxsize=None, but that might affect some other code.
  101. """
  102. try:
  103. if use_ascii:
  104. return ascii(obj)
  105. return repr(obj)
  106. except Exception as exc:
  107. return _format_repr_exception(exc, obj)
  108. class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter):
  109. """PrettyPrinter that always dispatches (regardless of width)."""
  110. def _format(
  111. self,
  112. object: object,
  113. stream: IO[str],
  114. indent: int,
  115. allowance: int,
  116. context: Dict[int, Any],
  117. level: int,
  118. ) -> None:
  119. # Type ignored because _dispatch is private.
  120. p = self._dispatch.get(type(object).__repr__, None) # type: ignore[attr-defined]
  121. objid = id(object)
  122. if objid in context or p is None:
  123. # Type ignored because _format is private.
  124. super()._format( # type: ignore[misc]
  125. object,
  126. stream,
  127. indent,
  128. allowance,
  129. context,
  130. level,
  131. )
  132. return
  133. context[objid] = 1
  134. p(self, object, stream, indent, allowance, context, level + 1)
  135. del context[objid]
  136. def _pformat_dispatch(
  137. object: object,
  138. indent: int = 1,
  139. width: int = 80,
  140. depth: Optional[int] = None,
  141. *,
  142. compact: bool = False,
  143. ) -> str:
  144. return AlwaysDispatchingPrettyPrinter(
  145. indent=indent, width=width, depth=depth, compact=compact
  146. ).pformat(object)