Extract traceback information. Args: exc_type (Type[BaseException]): Exception type. exc_value (BaseException): Exception value. traceback (TracebackType): Python Traceback object. show_locals (bool, optional): Enable display of local variable
(
cls,
exc_type: Type[BaseException],
exc_value: BaseException,
traceback: Optional[TracebackType],
*,
show_locals: bool = False,
locals_max_length: int = LOCALS_MAX_LENGTH,
locals_max_string: int = LOCALS_MAX_STRING,
locals_max_depth: Optional[int] = None,
locals_hide_dunder: bool = True,
locals_hide_sunder: bool = False,
_visited_exceptions: Optional[Set[BaseException]] = None,
)
| 431 | |
| 432 | @classmethod |
| 433 | def extract( |
| 434 | cls, |
| 435 | exc_type: Type[BaseException], |
| 436 | exc_value: BaseException, |
| 437 | traceback: Optional[TracebackType], |
| 438 | *, |
| 439 | show_locals: bool = False, |
| 440 | locals_max_length: int = LOCALS_MAX_LENGTH, |
| 441 | locals_max_string: int = LOCALS_MAX_STRING, |
| 442 | locals_max_depth: Optional[int] = None, |
| 443 | locals_hide_dunder: bool = True, |
| 444 | locals_hide_sunder: bool = False, |
| 445 | _visited_exceptions: Optional[Set[BaseException]] = None, |
| 446 | ) -> Trace: |
| 447 | """Extract traceback information. |
| 448 | |
| 449 | Args: |
| 450 | exc_type (Type[BaseException]): Exception type. |
| 451 | exc_value (BaseException): Exception value. |
| 452 | traceback (TracebackType): Python Traceback object. |
| 453 | show_locals (bool, optional): Enable display of local variables. Defaults to False. |
| 454 | locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. |
| 455 | Defaults to 10. |
| 456 | locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. |
| 457 | locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. |
| 458 | locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True. |
| 459 | locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False. |
| 460 | |
| 461 | Returns: |
| 462 | Trace: A Trace instance which you can use to construct a `Traceback`. |
| 463 | """ |
| 464 | |
| 465 | stacks: List[Stack] = [] |
| 466 | is_cause = False |
| 467 | |
| 468 | from rich import _IMPORT_CWD |
| 469 | |
| 470 | notes: List[str] = getattr(exc_value, "__notes__", None) or [] |
| 471 | |
| 472 | grouped_exceptions: Set[BaseException] = ( |
| 473 | set() if _visited_exceptions is None else _visited_exceptions |
| 474 | ) |
| 475 | |
| 476 | def safe_str(_object: Any) -> str: |
| 477 | """Don't allow exceptions from __str__ to propagate.""" |
| 478 | try: |
| 479 | return str(_object) |
| 480 | except Exception: |
| 481 | return "<exception str() failed>" |
| 482 | |
| 483 | while True: |
| 484 | stack = Stack( |
| 485 | exc_type=safe_str(exc_type.__name__), |
| 486 | exc_value=safe_str(exc_value), |
| 487 | is_cause=is_cause, |
| 488 | notes=notes, |
| 489 | ) |
| 490 |