| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from types import TracebackType |
| 4 |
|
| 5 |
|
| 6 |
# this is used for collapsing single-exception ExceptionGroups when using |
| 7 |
# `strict_exception_groups=False`. Once that is retired this function can |
| 8 |
# be removed as well. |
| 9 |
def concat_tb( |
| 10 |
head: TracebackType | None, |
| 11 |
tail: TracebackType | None, |
| 12 |
) -> TracebackType | None: |
| 13 |
# We have to use an iterative algorithm here, because in the worst case |
| 14 |
# this might be a RecursionError stack that is by definition too deep to |
| 15 |
# process by recursion! |
| 16 |
head_tbs = [] |
| 17 |
pointer = head |
| 18 |
while pointer is not None: |
| 19 |
head_tbs.append(pointer) |
| 20 |
pointer = pointer.tb_next |
| 21 |
current_head = tail |
| 22 |
for head_tb in reversed(head_tbs): |
| 23 |
current_head = TracebackType( |
| 24 |
current_head, head_tb.tb_frame, head_tb.tb_lasti, head_tb.tb_lineno |
| 25 |
) |
| 26 |
return current_head |