| 185 | |
| 186 | |
| 187 | def append_exception_context(error: BaseException, context: BaseException) -> None: |
| 188 | if _exception_graph_contains(error, context) or _exception_graph_contains(context, error): |
| 189 | return |
| 190 | if error.__context__ is None: |
| 191 | error.__context__ = context |
| 192 | return |
| 193 | tail = error |
| 194 | seen: set[int] = set() |
| 195 | while id(tail) not in seen: |
| 196 | seen.add(id(tail)) |
| 197 | if (next_error := tail.__cause__ if tail.__cause__ is not None else tail.__context__) is None: |
| 198 | tail.__context__ = context |
| 199 | return |
| 200 | tail = next_error |
| 201 | |
| 202 | |
| 203 | def _exception_graph_contains(error: BaseException, target: BaseException) -> bool: |