| 1 | # Our copy of mypy/mypyc/crash.py, patched to write to stderr
 | 
| 2 | 
 | 
| 3 | from typing import Iterator
 | 
| 4 | 
 | 
| 5 | MYPY = False
 | 
| 6 | if MYPY:
 | 
| 7 |     from typing import NoReturn
 | 
| 8 | 
 | 
| 9 | import sys
 | 
| 10 | import traceback
 | 
| 11 | from contextlib import contextmanager
 | 
| 12 | 
 | 
| 13 | 
 | 
| 14 | @contextmanager
 | 
| 15 | def catch_errors(module_path: str, line: int) -> Iterator[None]:
 | 
| 16 |     try:
 | 
| 17 |         yield
 | 
| 18 |     except Exception:
 | 
| 19 |         crash_report(module_path, line)
 | 
| 20 | 
 | 
| 21 | 
 | 
| 22 | def crash_report(module_path: str, line: int) -> 'NoReturn':
 | 
| 23 |     # Adapted from report_internal_error in mypy
 | 
| 24 |     err = sys.exc_info()[1]
 | 
| 25 |     tb = traceback.extract_stack()[:-4]
 | 
| 26 |     # Excise all the traceback from the test runner
 | 
| 27 |     for i, x in enumerate(tb):
 | 
| 28 |         if x.name == 'pytest_runtest_call':
 | 
| 29 |             tb = tb[i + 1:]
 | 
| 30 |             break
 | 
| 31 |     tb2 = traceback.extract_tb(sys.exc_info()[2])[1:]
 | 
| 32 |     print('Traceback (most recent call last):', file=sys.stderr)
 | 
| 33 |     for s in traceback.format_list(tb + tb2):
 | 
| 34 |         print(s.rstrip('\n'), file=sys.stderr)
 | 
| 35 |     print('{}:{}: {}: {}'.format(module_path, line,
 | 
| 36 |                                  type(err).__name__, err),
 | 
| 37 |           file=sys.stderr)
 | 
| 38 |     raise SystemExit(2)
 |