MCPcopy Index your code
hub / github.com/python/cpython / format

Method format

Lib/traceback.py:1537–1635  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1535 )
1536
1537 def format(self, *, chain=True, _ctx=None, **kwargs):
1538 """Format the exception.
1539
1540 If chain is not *True*, *__cause__* and *__context__* will not be formatted.
1541
1542 The return value is a generator of strings, each ending in a newline and
1543 some containing internal newlines. `print_exception` is a wrapper around
1544 this method which just prints the lines to a file.
1545
1546 The message indicating which exception occurred is always the last
1547 string in the output.
1548 """
1549 colorize = kwargs.get("colorize", False)
1550 if _ctx is None:
1551 _ctx = _ExceptionPrintContext()
1552
1553 output = []
1554 exc = self
1555 if chain:
1556 while exc:
1557 if exc.__cause__ is not None:
1558 chained_msg = _cause_message
1559 chained_exc = exc.__cause__
1560 elif (exc.__context__ is not None and
1561 not exc.__suppress_context__):
1562 chained_msg = _context_message
1563 chained_exc = exc.__context__
1564 else:
1565 chained_msg = None
1566 chained_exc = None
1567
1568 output.append((chained_msg, exc))
1569 exc = chained_exc
1570 else:
1571 output.append((None, exc))
1572
1573 for msg, exc in reversed(output):
1574 if msg is not None:
1575 yield from _ctx.emit(msg)
1576 if exc.exceptions is None:
1577 if exc.stack:
1578 yield from _ctx.emit('Traceback (most recent call last):\n')
1579 yield from _ctx.emit(exc.stack.format(colorize=colorize))
1580 yield from _ctx.emit(exc.format_exception_only(colorize=colorize))
1581 elif _ctx.exception_group_depth > self.max_group_depth:
1582 # exception group, but depth exceeds limit
1583 yield from _ctx.emit(
1584 f"... (max_group_depth is {self.max_group_depth})\n")
1585 else:
1586 # format exception group
1587 is_toplevel = (_ctx.exception_group_depth == 0)
1588 if is_toplevel:
1589 _ctx.exception_group_depth += 1
1590
1591 if exc.stack:
1592 yield from _ctx.emit(
1593 'Exception Group Traceback (most recent call last):\n',
1594 margin_char = '+' if is_toplevel else None)

Callers 2

format_exceptionFunction · 0.95
printMethod · 0.95

Calls 7

format_exception_onlyMethod · 0.80
getMethod · 0.45
appendMethod · 0.45
emitMethod · 0.45
formatMethod · 0.45
indentMethod · 0.45

Tested by

no test coverage detected