| 139 | |
| 140 | |
| 141 | def test_disabling_code(with_monitoring): |
| 142 | executed = [] |
| 143 | |
| 144 | def _start_method(code, offset): |
| 145 | if code.co_name == "method": |
| 146 | executed.append(("start", code.co_name, offset)) |
| 147 | monitor.set_local_events(DEBUGGER_ID, code, monitor.events.LINE) |
| 148 | return monitor.DISABLE |
| 149 | |
| 150 | def _on_line(code, offset): |
| 151 | if code.co_name == "method": |
| 152 | executed.append(("line", code.co_name, offset)) |
| 153 | return monitor.DISABLE |
| 154 | |
| 155 | monitor.set_events(DEBUGGER_ID, monitor.events.PY_START | monitor.events.PY_RESUME) |
| 156 | |
| 157 | monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method) |
| 158 | monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RESUME, _start_method) |
| 159 | monitor.register_callback(DEBUGGER_ID, monitor.events.LINE, _on_line) |
| 160 | |
| 161 | def method(): |
| 162 | a = 1 |
| 163 | |
| 164 | method() |
| 165 | method() |
| 166 | assert executed == [("start", "method", 0), ("line", "method", method.__code__.co_firstlineno + 1)] |
| 167 | |
| 168 | del executed[:] |
| 169 | |
| 170 | # Check: if disable once, even on a new thread we won't get notifications! |
| 171 | t = threading.Thread(target=method) |
| 172 | t.start() |
| 173 | t.join() |
| 174 | |
| 175 | assert not executed |
| 176 | |
| 177 | # Unless restart_events is called... |
| 178 | monitor.restart_events() |
| 179 | t = threading.Thread(target=method) |
| 180 | t.start() |
| 181 | t.join() |
| 182 | assert executed == [("start", "method", 0), ("line", "method", method.__code__.co_firstlineno + 1)] |
| 183 | |
| 184 | |
| 185 | def test_change_line_during_trace(with_monitoring): |