| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import traceback |
| 5 |
from collections.abc import Iterator |
| 6 |
from contextlib import contextmanager |
| 7 |
from typing import NoReturn |
| 8 |
|
| 9 |
|
| 10 |
@contextmanager |
| 11 |
def catch_errors(module_path: str, line: int) -> Iterator[None]: |
| 12 |
try: |
| 13 |
yield |
| 14 |
except Exception: |
| 15 |
crash_report(module_path, line) |
| 16 |
|
| 17 |
|
| 18 |
def crash_report(module_path: str, line: int) -> NoReturn: |
| 19 |
# Adapted from report_internal_error in mypy |
| 20 |
err = sys.exc_info()[1] |
| 21 |
tb = traceback.extract_stack()[:-4] |
| 22 |
# Excise all the traceback from the test runner |
| 23 |
for i, x in enumerate(tb): |
| 24 |
if x.name == "pytest_runtest_call": |
| 25 |
tb = tb[i + 1 :] |
| 26 |
break |
| 27 |
tb2 = traceback.extract_tb(sys.exc_info()[2])[1:] |
| 28 |
print("Traceback (most recent call last):") |
| 29 |
for s in traceback.format_list(tb + tb2): |
| 30 |
print(s.rstrip("\n")) |
| 31 |
print(f"{module_path}:{line}: {type(err).__name__}: {err}") |
| 32 |
print( |
| 33 |
f"{module_path}:{line}: note: this is an internal mypyc error; " |
| 34 |
"please report a bug at https://github.com/mypyc/mypyc/issues" |
| 35 |
) |
| 36 |
raise SystemExit(2) |