mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
Handle exception chaining and groups in Dev's traceback handling (#6178)
This commit is contained in:
parent
fdcbe00143
commit
9c85917dad
@ -337,11 +337,36 @@ class DevOutput:
|
||||
pass
|
||||
else:
|
||||
exc.lineno -= line_offset
|
||||
if sys.version_info >= (3, 10) and exc.end_lineno is not None:
|
||||
exc.end_lineno -= line_offset
|
||||
else:
|
||||
exc.lineno -= line_offset
|
||||
if sys.version_info >= (3, 10) and exc.end_lineno is not None:
|
||||
exc.end_lineno -= line_offset
|
||||
|
||||
traceback_exc = traceback.TracebackException(exc_type, exc, tb)
|
||||
top_traceback_exc = traceback.TracebackException(exc_type, exc, tb)
|
||||
py311_or_above = sys.version_info >= (3, 11)
|
||||
queue = [ # actually a stack but 'stack' is easy to confuse with actual traceback stack
|
||||
top_traceback_exc,
|
||||
]
|
||||
seen = {id(top_traceback_exc)}
|
||||
while queue:
|
||||
traceback_exc = queue.pop()
|
||||
|
||||
# handle exception groups; this uses getattr() to support `exceptiongroup` backport lib
|
||||
exceptions: List[traceback.TracebackException] = (
|
||||
getattr(traceback_exc, "exceptions", None) or []
|
||||
)
|
||||
# handle exception chaining
|
||||
if traceback_exc.__cause__ is not None:
|
||||
exceptions.append(traceback_exc.__cause__)
|
||||
if traceback_exc.__context__ is not None:
|
||||
exceptions.append(traceback_exc.__context__)
|
||||
for te in exceptions:
|
||||
if id(te) not in seen:
|
||||
queue.append(te)
|
||||
seen.add(id(te))
|
||||
|
||||
stack_summary = traceback_exc.stack
|
||||
for idx, frame_summary in enumerate(stack_summary):
|
||||
try:
|
||||
@ -379,7 +404,7 @@ class DevOutput:
|
||||
)
|
||||
stack_summary[idx] = frame_summary
|
||||
|
||||
return "".join(traceback_exc.format())
|
||||
return "".join(top_traceback_exc.format())
|
||||
|
||||
|
||||
@cog_i18n(_)
|
||||
|
||||
@ -330,6 +330,132 @@ STATEMENT_TESTS = {
|
||||
""",
|
||||
),
|
||||
),
|
||||
# exception chaining
|
||||
"""\
|
||||
try:
|
||||
1 / 0
|
||||
except ZeroDivisionError as exc:
|
||||
try:
|
||||
raise RuntimeError("direct cause") from exc
|
||||
except RuntimeError:
|
||||
raise ValueError("indirect cause")
|
||||
""": (
|
||||
(
|
||||
lambda v: v < (3, 11),
|
||||
"""\
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 2, in <module>
|
||||
1 / 0
|
||||
ZeroDivisionError: division by zero
|
||||
|
||||
The above exception was the direct cause of the following exception:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 5, in <module>
|
||||
raise RuntimeError("direct cause") from exc
|
||||
RuntimeError: direct cause
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 7, in <module>
|
||||
raise ValueError("indirect cause")
|
||||
ValueError: indirect cause
|
||||
""",
|
||||
),
|
||||
(
|
||||
lambda v: v >= (3, 11),
|
||||
"""\
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 2, in <module>
|
||||
1 / 0
|
||||
~~^~~
|
||||
ZeroDivisionError: division by zero
|
||||
|
||||
The above exception was the direct cause of the following exception:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 5, in <module>
|
||||
raise RuntimeError("direct cause") from exc
|
||||
RuntimeError: direct cause
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 7, in <module>
|
||||
raise ValueError("indirect cause")
|
||||
ValueError: indirect cause
|
||||
""",
|
||||
),
|
||||
),
|
||||
# exception groups
|
||||
"""\
|
||||
def f(v):
|
||||
try:
|
||||
1 / 0
|
||||
except ZeroDivisionError:
|
||||
try:
|
||||
raise ValueError(v)
|
||||
except ValueError as e:
|
||||
return e
|
||||
try:
|
||||
raise ExceptionGroup("one", [f(1)])
|
||||
except ExceptionGroup as e:
|
||||
eg = e
|
||||
try:
|
||||
raise ExceptionGroup("two", [f(2), eg])
|
||||
except ExceptionGroup as e:
|
||||
raise RuntimeError("wrapping") from e
|
||||
""": (
|
||||
(
|
||||
lambda v: v >= (3, 11),
|
||||
"""\
|
||||
+ Exception Group Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 14, in <module>
|
||||
| raise ExceptionGroup("two", [f(2), eg])
|
||||
| ExceptionGroup: two (2 sub-exceptions)
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 3, in f
|
||||
| 1 / 0
|
||||
| ~~^~~
|
||||
| ZeroDivisionError: division by zero
|
||||
|
|
||||
| During handling of the above exception, another exception occurred:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 6, in f
|
||||
| raise ValueError(v)
|
||||
| ValueError: 2
|
||||
+---------------- 2 ----------------
|
||||
| Exception Group Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 10, in <module>
|
||||
| raise ExceptionGroup("one", [f(1)])
|
||||
| ExceptionGroup: one (1 sub-exception)
|
||||
+-+---------------- 1 ----------------
|
||||
| Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 3, in f
|
||||
| 1 / 0
|
||||
| ~~^~~
|
||||
| ZeroDivisionError: division by zero
|
||||
|
|
||||
| During handling of the above exception, another exception occurred:
|
||||
|
|
||||
| Traceback (most recent call last):
|
||||
| File "<test run - snippet #0>", line 6, in f
|
||||
| raise ValueError(v)
|
||||
| ValueError: 1
|
||||
+------------------------------------
|
||||
|
||||
The above exception was the direct cause of the following exception:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<test run - snippet #0>", line 16, in <module>
|
||||
raise RuntimeError("wrapping") from e
|
||||
RuntimeError: wrapping
|
||||
""",
|
||||
),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user