Format the exception. If chain is not *True*, *__cause__* and *__context__* will not be formatted. The return value is a generator of strings, each ending in a newline and some containing internal newlines. `print_exception` is a wrapper around this method which jus
(self, *, chain=True, _ctx=None, **kwargs)
| 1466 | ) |
| 1467 | |
| 1468 | def format(self, *, chain=True, _ctx=None, **kwargs): |
| 1469 | """Format the exception. |
| 1470 | |
| 1471 | If chain is not *True*, *__cause__* and *__context__* will not be formatted. |
| 1472 | |
| 1473 | The return value is a generator of strings, each ending in a newline and |
| 1474 | some containing internal newlines. `print_exception` is a wrapper around |
| 1475 | this method which just prints the lines to a file. |
| 1476 | |
| 1477 | The message indicating which exception occurred is always the last |
| 1478 | string in the output. |
| 1479 | """ |
| 1480 | colorize = kwargs.get("colorize", False) |
| 1481 | if _ctx is None: |
| 1482 | _ctx = _ExceptionPrintContext() |
| 1483 | |
| 1484 | output = [] |
| 1485 | exc = self |
| 1486 | if chain: |
| 1487 | while exc: |
| 1488 | if exc.__cause__ is not None: |
| 1489 | chained_msg = _cause_message |
| 1490 | chained_exc = exc.__cause__ |
| 1491 | elif (exc.__context__ is not None and |
| 1492 | not exc.__suppress_context__): |
| 1493 | chained_msg = _context_message |
| 1494 | chained_exc = exc.__context__ |
| 1495 | else: |
| 1496 | chained_msg = None |
| 1497 | chained_exc = None |
| 1498 | |
| 1499 | output.append((chained_msg, exc)) |
| 1500 | exc = chained_exc |
| 1501 | else: |
| 1502 | output.append((None, exc)) |
| 1503 | |
| 1504 | for msg, exc in reversed(output): |
| 1505 | if msg is not None: |
| 1506 | yield from _ctx.emit(msg) |
| 1507 | if exc.exceptions is None: |
| 1508 | if exc.stack: |
| 1509 | yield from _ctx.emit('Traceback (most recent call last):\n') |
| 1510 | yield from _ctx.emit(exc.stack.format(colorize=colorize)) |
| 1511 | yield from _ctx.emit(exc.format_exception_only(colorize=colorize)) |
| 1512 | elif _ctx.exception_group_depth > self.max_group_depth: |
| 1513 | # exception group, but depth exceeds limit |
| 1514 | yield from _ctx.emit( |
| 1515 | f"... (max_group_depth is {self.max_group_depth})\n") |
| 1516 | else: |
| 1517 | # format exception group |
| 1518 | is_toplevel = (_ctx.exception_group_depth == 0) |
| 1519 | if is_toplevel: |
| 1520 | _ctx.exception_group_depth += 1 |
| 1521 | |
| 1522 | if exc.stack: |
| 1523 | yield from _ctx.emit( |
| 1524 | 'Exception Group Traceback (most recent call last):\n', |
| 1525 | margin_char = '+' if is_toplevel else None) |
no test coverage detected