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 whi
(self, *, chain=True, _ctx=None)
| 914 | yield "{}: {}{}\n".format(stype, msg, filename_suffix) |
| 915 | |
| 916 | def format(self, *, chain=True, _ctx=None): |
| 917 | """Format the exception. |
| 918 | |
| 919 | If chain is not *True*, *__cause__* and *__context__* will not be formatted. |
| 920 | |
| 921 | The return value is a generator of strings, each ending in a newline and |
| 922 | some containing internal newlines. `print_exception` is a wrapper around |
| 923 | this method which just prints the lines to a file. |
| 924 | |
| 925 | The message indicating which exception occurred is always the last |
| 926 | string in the output. |
| 927 | """ |
| 928 | |
| 929 | if _ctx is None: |
| 930 | _ctx = _ExceptionPrintContext() |
| 931 | |
| 932 | output = [] |
| 933 | exc = self |
| 934 | if chain: |
| 935 | while exc: |
| 936 | if exc.__cause__ is not None: |
| 937 | chained_msg = _cause_message |
| 938 | chained_exc = exc.__cause__ |
| 939 | elif (exc.__context__ is not None and |
| 940 | not exc.__suppress_context__): |
| 941 | chained_msg = _context_message |
| 942 | chained_exc = exc.__context__ |
| 943 | else: |
| 944 | chained_msg = None |
| 945 | chained_exc = None |
| 946 | |
| 947 | output.append((chained_msg, exc)) |
| 948 | exc = chained_exc |
| 949 | else: |
| 950 | output.append((None, exc)) |
| 951 | |
| 952 | for msg, exc in reversed(output): |
| 953 | if msg is not None: |
| 954 | yield from _ctx.emit(msg) |
| 955 | if exc.exceptions is None: |
| 956 | if exc.stack: |
| 957 | yield from _ctx.emit('Traceback (most recent call last):\n') |
| 958 | yield from _ctx.emit(exc.stack.format()) |
| 959 | yield from _ctx.emit(exc.format_exception_only()) |
| 960 | elif _ctx.exception_group_depth > self.max_group_depth: |
| 961 | # exception group, but depth exceeds limit |
| 962 | yield from _ctx.emit( |
| 963 | f"... (max_group_depth is {self.max_group_depth})\n") |
| 964 | else: |
| 965 | # format exception group |
| 966 | is_toplevel = (_ctx.exception_group_depth == 0) |
| 967 | if is_toplevel: |
| 968 | _ctx.exception_group_depth += 1 |
| 969 | |
| 970 | if exc.stack: |
| 971 | yield from _ctx.emit( |
| 972 | 'Exception Group Traceback (most recent call last):\n', |
| 973 | margin_char = '+' if is_toplevel else None) |
no test coverage detected