/
_formatting.py
399 lines (343 loc) · 13.3 KB
/
_formatting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# traceback_exception_init() adapted from trio
#
# _ExceptionPrintContext and traceback_exception_format() copied from the standard
# library
from __future__ import annotations
import collections.abc
import sys
import textwrap
import traceback
from functools import singledispatch
from types import TracebackType
from typing import Any, List, Optional
from ._exceptions import BaseExceptionGroup
max_group_width = 15
max_group_depth = 10
_cause_message = (
"\nThe above exception was the direct cause of the following exception:\n\n"
)
_context_message = (
"\nDuring handling of the above exception, another exception occurred:\n\n"
)
def _format_final_exc_line(etype, value):
valuestr = _safe_string(value, "exception")
if value is None or not valuestr:
line = f"{etype}\n"
else:
line = f"{etype}: {valuestr}\n"
return line
def _safe_string(value, what, func=str):
try:
return func(value)
except BaseException:
return f"<{what} {func.__name__}() failed>"
class _ExceptionPrintContext:
def __init__(self):
self.seen = set()
self.exception_group_depth = 0
self.need_close = False
def indent(self):
return " " * (2 * self.exception_group_depth)
def emit(self, text_gen, margin_char=None):
if margin_char is None:
margin_char = "|"
indent_str = self.indent()
if self.exception_group_depth:
indent_str += margin_char + " "
if isinstance(text_gen, str):
yield textwrap.indent(text_gen, indent_str, lambda line: True)
else:
for text in text_gen:
yield textwrap.indent(text, indent_str, lambda line: True)
def exceptiongroup_excepthook(
etype: type[BaseException], value: BaseException, tb: TracebackType | None
) -> None:
sys.stderr.write("".join(traceback.format_exception(etype, value, tb)))
class PatchedTracebackException(traceback.TracebackException):
def __init__(
self,
exc_type: type[BaseException],
exc_value: BaseException,
exc_traceback: TracebackType | None,
*,
limit: int | None = None,
lookup_lines: bool = True,
capture_locals: bool = False,
compact: bool = False,
_seen: set[int] | None = None,
) -> None:
kwargs: dict[str, Any] = {}
if sys.version_info >= (3, 10):
kwargs["compact"] = compact
traceback_exception_original_init(
self,
exc_type,
exc_value,
exc_traceback,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=_seen,
**kwargs,
)
is_recursive_call = _seen is not None
if _seen is None:
_seen = set()
_seen.add(id(exc_value))
# Convert __cause__ and __context__ to `TracebackExceptions`s, use a
# queue to avoid recursion (only the top-level call gets _seen == None)
if not is_recursive_call:
queue = [(self, exc_value)]
while queue:
te, e = queue.pop()
if e and e.__cause__ is not None and id(e.__cause__) not in _seen:
cause = PatchedTracebackException(
type(e.__cause__),
e.__cause__,
e.__cause__.__traceback__,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=_seen,
)
else:
cause = None
if compact:
need_context = (
cause is None and e is not None and not e.__suppress_context__
)
else:
need_context = True
if (
e
and e.__context__ is not None
and need_context
and id(e.__context__) not in _seen
):
context = PatchedTracebackException(
type(e.__context__),
e.__context__,
e.__context__.__traceback__,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=_seen,
)
else:
context = None
# Capture each of the exceptions in the ExceptionGroup along with each
# of their causes and contexts
if e and isinstance(e, BaseExceptionGroup):
exceptions = []
for exc in e.exceptions:
texc = PatchedTracebackException(
type(exc),
exc,
exc.__traceback__,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=_seen,
)
exceptions.append(texc)
else:
exceptions = None
te.__cause__ = cause
te.__context__ = context
te.exceptions = exceptions
if cause:
queue.append((te.__cause__, e.__cause__))
if context:
queue.append((te.__context__, e.__context__))
if exceptions:
queue.extend(zip(te.exceptions, e.exceptions))
self.__notes__ = getattr(exc_value, "__notes__", ())
def format(self, *, chain=True, _ctx=None):
if _ctx is None:
_ctx = _ExceptionPrintContext()
output = []
exc = self
if chain:
while exc:
if exc.__cause__ is not None:
chained_msg = _cause_message
chained_exc = exc.__cause__
elif exc.__context__ is not None and not exc.__suppress_context__:
chained_msg = _context_message
chained_exc = exc.__context__
else:
chained_msg = None
chained_exc = None
output.append((chained_msg, exc))
exc = chained_exc
else:
output.append((None, exc))
for msg, exc in reversed(output):
if msg is not None:
yield from _ctx.emit(msg)
if exc.exceptions is None:
if exc.stack:
yield from _ctx.emit("Traceback (most recent call last):\n")
yield from _ctx.emit(exc.stack.format())
yield from _ctx.emit(exc.format_exception_only())
elif _ctx.exception_group_depth > max_group_depth:
# exception group, but depth exceeds limit
yield from _ctx.emit(f"... (max_group_depth is {max_group_depth})\n")
else:
# format exception group
is_toplevel = _ctx.exception_group_depth == 0
if is_toplevel:
_ctx.exception_group_depth += 1
if exc.stack:
yield from _ctx.emit(
"Exception Group Traceback (most recent call last):\n",
margin_char="+" if is_toplevel else None,
)
yield from _ctx.emit(exc.stack.format())
yield from _ctx.emit(exc.format_exception_only())
num_excs = len(exc.exceptions)
if num_excs <= max_group_width:
n = num_excs
else:
n = max_group_width + 1
_ctx.need_close = False
for i in range(n):
last_exc = i == n - 1
if last_exc:
# The closing frame may be added by a recursive call
_ctx.need_close = True
if max_group_width is not None:
truncated = i >= max_group_width
else:
truncated = False
title = f"{i + 1}" if not truncated else "..."
yield (
_ctx.indent()
+ ("+-" if i == 0 else " ")
+ f"+---------------- {title} ----------------\n"
)
_ctx.exception_group_depth += 1
if not truncated:
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx)
else:
remaining = num_excs - max_group_width
plural = "s" if remaining > 1 else ""
yield from _ctx.emit(
f"and {remaining} more exception{plural}\n"
)
if last_exc and _ctx.need_close:
yield _ctx.indent() + "+------------------------------------\n"
_ctx.need_close = False
_ctx.exception_group_depth -= 1
if is_toplevel:
assert _ctx.exception_group_depth == 1
_ctx.exception_group_depth = 0
def format_exception_only(self):
"""Format the exception part of the traceback.
The return value is a generator of strings, each ending in a newline.
Normally, the generator emits a single string; however, for
SyntaxError exceptions, it emits several lines that (when
printed) display detailed information about where the syntax
error occurred.
The message indicating which exception occurred is always the last
string in the output.
"""
if self.exc_type is None:
yield traceback._format_final_exc_line(None, self._str)
return
stype = self.exc_type.__qualname__
smod = self.exc_type.__module__
if smod not in ("__main__", "builtins"):
if not isinstance(smod, str):
smod = "<unknown>"
stype = smod + "." + stype
if not issubclass(self.exc_type, SyntaxError):
yield _format_final_exc_line(stype, self._str)
elif traceback_exception_format_syntax_error is not None:
yield from traceback_exception_format_syntax_error(self, stype)
else:
yield from traceback_exception_original_format_exception_only(self)
if isinstance(self.__notes__, collections.abc.Sequence):
for note in self.__notes__:
note = _safe_string(note, "note")
yield from [line + "\n" for line in note.split("\n")]
elif self.__notes__ is not None:
yield _safe_string(self.__notes__, "__notes__", func=repr)
traceback_exception_original_init = traceback.TracebackException.__init__
traceback_exception_original_format = traceback.TracebackException.format
traceback_exception_original_format_exception_only = (
traceback.TracebackException.format_exception_only
)
traceback_exception_format_syntax_error = getattr(
traceback.TracebackException, "_format_syntax_error", None
)
if sys.excepthook is sys.__excepthook__:
traceback.TracebackException.__init__ = ( # type: ignore[assignment]
PatchedTracebackException.__init__
)
traceback.TracebackException.format = ( # type: ignore[assignment]
PatchedTracebackException.format
)
traceback.TracebackException.format_exception_only = ( # type: ignore[assignment]
PatchedTracebackException.format_exception_only
)
sys.excepthook = exceptiongroup_excepthook
@singledispatch
def format_exception_only(__exc: BaseException) -> List[str]:
return list(
PatchedTracebackException(
type(__exc), __exc, None, compact=True
).format_exception_only()
)
@format_exception_only.register
def _(__exc: type, value: BaseException) -> List[str]:
return format_exception_only(value)
@singledispatch
def format_exception(
__exc: BaseException,
limit: Optional[int] = None,
chain: bool = True,
) -> List[str]:
return list(
PatchedTracebackException(
type(__exc), __exc, __exc.__traceback__, limit=limit, compact=True
).format(chain=chain)
)
@format_exception.register
def _(
__exc: type,
value: BaseException,
tb: TracebackType,
limit: Optional[int] = None,
chain: bool = True,
) -> List[str]:
return format_exception(value, limit, chain)
@singledispatch
def print_exception(
__exc: BaseException,
limit: Optional[int] = None,
file: Any = None,
chain: bool = True,
) -> None:
if file is None:
file = sys.stderr
for line in PatchedTracebackException(
type(__exc), __exc, __exc.__traceback__, limit=limit
).format(chain=chain):
print(line, file=file, end="")
@print_exception.register
def _(
__exc: type,
value: BaseException,
tb: TracebackType,
limit: Optional[int] = None,
file: Any = None,
chain: bool = True,
) -> None:
print_exception(value, limit, file, chain)
def print_exc(
limit: Optional[int] = None,
file: Any | None = None,
chain: bool = True,
) -> None:
value = sys.exc_info()[1]
print_exception(value, limit, file, chain)