MCPcopy Create free account
hub / github.com/pytest-dev/pytest / ExceptionInfo

Class ExceptionInfo

src/_pytest/_code/code.py:446–680  ·  view source on GitHub ↗

Wraps sys.exc_info() objects and offers help for navigating the traceback.

Source from the content-addressed store, hash-verified

444@final
445@attr.s(repr=False, init=False, auto_attribs=True)
446class ExceptionInfo(Generic[E]):
447 """Wraps sys.exc_info() objects and offers help for navigating the traceback."""
448
449 _assert_start_repr: ClassVar = "AssertionError('assert "
450
451 _excinfo: Optional[Tuple[Type["E"], "E", TracebackType]]
452 _striptext: str
453 _traceback: Optional[Traceback]
454
455 def __init__(
456 self,
457 excinfo: Optional[Tuple[Type["E"], "E", TracebackType]],
458 striptext: str = "",
459 traceback: Optional[Traceback] = None,
460 *,
461 _ispytest: bool = False,
462 ) -> None:
463 check_ispytest(_ispytest)
464 self._excinfo = excinfo
465 self._striptext = striptext
466 self._traceback = traceback
467
468 @classmethod
469 def from_exc_info(
470 cls,
471 exc_info: Tuple[Type[E], E, TracebackType],
472 exprinfo: Optional[str] = None,
473 ) -> "ExceptionInfo[E]":
474 """Return an ExceptionInfo for an existing exc_info tuple.
475
476 .. warning::
477
478 Experimental API
479
480 :param exprinfo:
481 A text string helping to determine if we should strip
482 ``AssertionError`` from the output. Defaults to the exception
483 message/``__str__()``.
484 """
485 _striptext = ""
486 if exprinfo is None and isinstance(exc_info[1], AssertionError):
487 exprinfo = getattr(exc_info[1], "msg", None)
488 if exprinfo is None:
489 exprinfo = saferepr(exc_info[1])
490 if exprinfo and exprinfo.startswith(cls._assert_start_repr):
491 _striptext = "AssertionError: "
492
493 return cls(exc_info, _striptext, _ispytest=True)
494
495 @classmethod
496 def from_current(
497 cls, exprinfo: Optional[str] = None
498 ) -> "ExceptionInfo[BaseException]":
499 """Return an ExceptionInfo matching the current traceback.
500
501 .. warning::
502
503 Experimental API

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected