Wraps sys.exc_info() objects and offers help for navigating the traceback.
| 497 | @final |
| 498 | @dataclasses.dataclass |
| 499 | class ExceptionInfo(Generic[E]): |
| 500 | """Wraps sys.exc_info() objects and offers help for navigating the traceback.""" |
| 501 | |
| 502 | _assert_start_repr: ClassVar = "AssertionError('assert " |
| 503 | |
| 504 | _excinfo: tuple[type[E], E, TracebackType] | None |
| 505 | _striptext: str |
| 506 | _traceback: Traceback | None |
| 507 | |
| 508 | def __init__( |
| 509 | self, |
| 510 | excinfo: tuple[type[E], E, TracebackType] | None, |
| 511 | striptext: str = "", |
| 512 | traceback: Traceback | None = None, |
| 513 | *, |
| 514 | _ispytest: bool = False, |
| 515 | ) -> None: |
| 516 | check_ispytest(_ispytest) |
| 517 | self._excinfo = excinfo |
| 518 | self._striptext = striptext |
| 519 | self._traceback = traceback |
| 520 | |
| 521 | @classmethod |
| 522 | def from_exception( |
| 523 | cls, |
| 524 | # Ignoring error: "Cannot use a covariant type variable as a parameter". |
| 525 | # This is OK to ignore because this class is (conceptually) readonly. |
| 526 | # See https://github.com/python/mypy/issues/7049. |
| 527 | exception: E, # type: ignore[misc] |
| 528 | exprinfo: str | None = None, |
| 529 | ) -> ExceptionInfo[E]: |
| 530 | """Return an ExceptionInfo for an existing exception. |
| 531 | |
| 532 | The exception must have a non-``None`` ``__traceback__`` attribute, |
| 533 | otherwise this function fails with an assertion error. This means that |
| 534 | the exception must have been raised, or added a traceback with the |
| 535 | :py:meth:`~BaseException.with_traceback()` method. |
| 536 | |
| 537 | :param exprinfo: |
| 538 | A text string helping to determine if we should strip |
| 539 | ``AssertionError`` from the output. Defaults to the exception |
| 540 | message/``__str__()``. |
| 541 | |
| 542 | .. versionadded:: 7.4 |
| 543 | """ |
| 544 | assert exception.__traceback__, ( |
| 545 | "Exceptions passed to ExcInfo.from_exception(...)" |
| 546 | " must have a non-None __traceback__." |
| 547 | ) |
| 548 | exc_info = (type(exception), exception, exception.__traceback__) |
| 549 | return cls.from_exc_info(exc_info, exprinfo) |
| 550 | |
| 551 | @classmethod |
| 552 | def from_exc_info( |
| 553 | cls, |
| 554 | exc_info: tuple[type[E], E, TracebackType], |
| 555 | exprinfo: str | None = None, |
| 556 | ) -> ExceptionInfo[E]: |