| 29 | |
| 30 | |
| 31 | def test_exceptions(with_monitoring): |
| 32 | monitor.set_events(DEBUGGER_ID, monitor.events.RAISE | monitor.events.RERAISE) |
| 33 | |
| 34 | found = [] |
| 35 | |
| 36 | def _on_raise(code, instruction_offset, exc): |
| 37 | if code.co_filename.endswith("sys_monitoring.py"): |
| 38 | found.append(("raise", code.co_name, str(exc), sys._getframe(1).f_lineno)) |
| 39 | |
| 40 | def _on_reraise(code, instruction_offset, exc): |
| 41 | if code.co_filename.endswith("sys_monitoring.py"): |
| 42 | found.append(("reraise", code.co_name, str(exc), sys._getframe(1).f_lineno)) |
| 43 | |
| 44 | monitor.register_callback(DEBUGGER_ID, monitor.events.RAISE, _on_raise) |
| 45 | monitor.register_callback(DEBUGGER_ID, monitor.events.RERAISE, _on_reraise) |
| 46 | |
| 47 | def method_raise(): |
| 48 | raise RuntimeError("err1") |
| 49 | |
| 50 | def method_2(): |
| 51 | try: |
| 52 | method_raise() |
| 53 | except: |
| 54 | raise |
| 55 | |
| 56 | def method(): |
| 57 | try: |
| 58 | method_2() |
| 59 | except: |
| 60 | pass |
| 61 | |
| 62 | method() |
| 63 | assert found == [ |
| 64 | ("raise", "method_raise", "err1", method_raise.__code__.co_firstlineno + 1), |
| 65 | ("raise", "method_2", "err1", method_2.__code__.co_firstlineno + 2), |
| 66 | # This will be very tricky to handle. |
| 67 | # See: https://github.com/python/cpython/issues/112086 |
| 68 | ("reraise", "method_2", "err1", method_2.__code__.co_firstlineno + 4), |
| 69 | ("reraise", "method_2", "err1", method_2.__code__.co_firstlineno + 4), |
| 70 | ("raise", "method", "err1", method.__code__.co_firstlineno + 2), |
| 71 | ] |
| 72 | |
| 73 | |
| 74 | def test_exceptions_and_return(with_monitoring): |