MCPcopy
hub / github.com/pytest-dev/pytest / ExceptionInfoFormatter

Class ExceptionInfoFormatter

src/_pytest/_code/code.py:875–1246  ·  view source on GitHub ↗

Helper object to format ExceptionInfo's and individual exception parts into TerminalRepr's. See :func:`ExceptionInfo.getrepr` for parameters.

Source from the content-addressed store, hash-verified

873
874@dataclasses.dataclass
875class ExceptionInfoFormatter:
876 """Helper object to format ExceptionInfo's and individual exception parts
877 into TerminalRepr's.
878
879 See :func:`ExceptionInfo.getrepr` for parameters.
880 """
881
882 # for traceback entries
883 flow_marker: ClassVar = ">"
884 fail_marker: ClassVar = "E"
885
886 showlocals: bool = False
887 # Note: "native" is handled outside of ExceptionInfoFormatter.
888 style: TracebackStyle = "long"
889 abspath: bool = True
890 tbfilter: TracebackFilter = True
891 funcargs: bool = False
892 truncate_locals: bool = True
893 truncate_args: bool = True
894 chain: bool = True
895
896 astcache: dict[str | Path, ast.AST] = dataclasses.field(
897 default_factory=dict, init=False, repr=False
898 )
899
900 def _getindent(self, source: Source) -> int:
901 # Figure out indent for the given source.
902 try:
903 s = str(source.getstatement(len(source) - 1))
904 except KeyboardInterrupt:
905 raise
906 except BaseException:
907 try:
908 s = str(source[-1])
909 except KeyboardInterrupt:
910 raise
911 except BaseException:
912 return 0
913 return 4 + (len(s) - len(s.lstrip()))
914
915 def _getentrysource(self, entry: TracebackEntry) -> Source | None:
916 source = entry.getsource(self.astcache)
917 if source is not None:
918 source = source.deindent()
919 return source
920
921 def repr_args(self, entry: TracebackEntry) -> ReprFuncArgs | None:
922 if self.funcargs:
923 args = []
924 for argname, argvalue in entry.frame.getargs(var=True):
925 if self.truncate_args:
926 str_repr = saferepr(argvalue)
927 else:
928 str_repr = saferepr(argvalue, maxsize=None)
929 args.append((argname, str_repr))
930 return ReprFuncArgs(args)
931 return None
932

Calls

no outgoing calls