|
| 1 | +################################################################ |
| 2 | +# ExceptionGroup traceback formatting |
| 3 | +# |
| 4 | +# This file contains the terrible, terrible monkey patching of various things, |
| 5 | +# especially the traceback module, to add support for handling |
| 6 | +# ExceptionGroups. |
| 7 | +################################################################ |
| 8 | + |
| 9 | +import sys |
| 10 | +import textwrap |
| 11 | +import traceback |
| 12 | +import warnings |
| 13 | + |
| 14 | +from . import ExceptionGroup |
| 15 | + |
| 16 | +traceback_exception_original_init = traceback.TracebackException.__init__ |
| 17 | + |
| 18 | + |
| 19 | +def traceback_exception_init( |
| 20 | + self, |
| 21 | + exc_type, |
| 22 | + exc_value, |
| 23 | + exc_traceback, |
| 24 | + *, |
| 25 | + limit=None, |
| 26 | + lookup_lines=True, |
| 27 | + capture_locals=False, |
| 28 | + _seen=None |
| 29 | +): |
| 30 | + if _seen is None: |
| 31 | + _seen = set() |
| 32 | + |
| 33 | + # Capture the original exception and its cause and context as |
| 34 | + # TracebackExceptions |
| 35 | + traceback_exception_original_init( |
| 36 | + self, |
| 37 | + exc_type, |
| 38 | + exc_value, |
| 39 | + exc_traceback, |
| 40 | + limit=limit, |
| 41 | + lookup_lines=lookup_lines, |
| 42 | + capture_locals=capture_locals, |
| 43 | + _seen=_seen, |
| 44 | + ) |
| 45 | + |
| 46 | + # Capture each of the exceptions in the ExceptionGroup along with each of |
| 47 | + # their causes and contexts |
| 48 | + if isinstance(exc_value, ExceptionGroup): |
| 49 | + exceptions = [] |
| 50 | + sources = [] |
| 51 | + for exc, source in zip(exc_value.exceptions, exc_value.sources): |
| 52 | + if exc not in _seen: |
| 53 | + exceptions.append( |
| 54 | + traceback.TracebackException.from_exception( |
| 55 | + exc, |
| 56 | + limit=limit, |
| 57 | + lookup_lines=lookup_lines, |
| 58 | + capture_locals=capture_locals, |
| 59 | + # copy the set of _seen exceptions so that duplicates |
| 60 | + # shared between sub-exceptions are not omitted |
| 61 | + _seen=set(_seen), |
| 62 | + ) |
| 63 | + ) |
| 64 | + sources.append(source) |
| 65 | + self.exceptions = exceptions |
| 66 | + self.sources = sources |
| 67 | + else: |
| 68 | + self.exceptions = [] |
| 69 | + self.sources = [] |
| 70 | + |
| 71 | + |
| 72 | +def traceback_exception_format(self, *, chain=True): |
| 73 | + yield from traceback_exception_original_format(self, chain=chain) |
| 74 | + |
| 75 | + for exc, source in zip(self.exceptions, self.sources): |
| 76 | + yield "\n {}:\n\n".format(source) |
| 77 | + yield from ( |
| 78 | + textwrap.indent(line, " " * 4) for line in exc.format(chain=chain) |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def exceptiongroup_excepthook(etype, value, tb): |
| 83 | + sys.stderr.write("".join(traceback.format_exception(etype, value, tb))) |
| 84 | + |
| 85 | + |
| 86 | +traceback.TracebackException.__init__ = traceback_exception_init |
| 87 | +traceback_exception_original_format = traceback.TracebackException.format |
| 88 | +traceback.TracebackException.format = traceback_exception_format |
| 89 | + |
| 90 | +IPython_handler_installed = False |
| 91 | +warning_given = False |
| 92 | +if "IPython" in sys.modules: |
| 93 | + import IPython |
| 94 | + |
| 95 | + ip = IPython.get_ipython() |
| 96 | + if ip is not None: |
| 97 | + if ip.custom_exceptions != (): |
| 98 | + warnings.warn( |
| 99 | + "IPython detected, but you already have a custom exception " |
| 100 | + "handler installed. I'll skip installing exceptiongroup's " |
| 101 | + "custom handler, but this means you won't see full tracebacks " |
| 102 | + "for ExceptionGroups.", |
| 103 | + category=RuntimeWarning, |
| 104 | + ) |
| 105 | + warning_given = True |
| 106 | + else: |
| 107 | + |
| 108 | + def trio_show_traceback(self, etype, value, tb, tb_offset=None): |
| 109 | + # XX it would be better to integrate with IPython's fancy |
| 110 | + # exception formatting stuff (and not ignore tb_offset) |
| 111 | + exceptiongroup_excepthook(etype, value, tb) |
| 112 | + |
| 113 | + ip.set_custom_exc((ExceptionGroup,), trio_show_traceback) |
| 114 | + IPython_handler_installed = True |
| 115 | + |
| 116 | +if sys.excepthook is sys.__excepthook__: |
| 117 | + sys.excepthook = exceptiongroup_excepthook |
| 118 | +else: |
| 119 | + if not IPython_handler_installed and not warning_given: |
| 120 | + warnings.warn( |
| 121 | + "You seem to already have a custom sys.excepthook handler " |
| 122 | + "installed. I'll skip installing exceptiongroup's custom handler, " |
| 123 | + "but this means you won't see full tracebacks for " |
| 124 | + "ExceptionGroups.", |
| 125 | + category=RuntimeWarning, |
| 126 | + ) |
0 commit comments