File: test_sys_monitoring.py

package info (click to toggle)
pydevd 3.3.0%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,892 kB
  • sloc: python: 77,508; cpp: 1,869; sh: 368; makefile: 50; ansic: 4
file content (294 lines) | stat: -rw-r--r-- 9,768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import sys
import pytest
import threading

pytestmark = pytest.mark.skipif(not hasattr(sys, "monitoring"), reason="Requires sys.monitoring")

if hasattr(sys, "monitoring"):
    DEBUGGER_ID = sys.monitoring.DEBUGGER_ID
    monitor = sys.monitoring


def _disable_monitoring():
    if monitor.get_tool(DEBUGGER_ID) == "pydevd":
        sys.monitoring.set_events(sys.monitoring.DEBUGGER_ID, 0)
        monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, None)
        monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RESUME, None)
        monitor.register_callback(DEBUGGER_ID, monitor.events.LINE, None)
        monitor.register_callback(DEBUGGER_ID, monitor.events.RAISE, None)
        monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RETURN, None)
        monitor.register_callback(DEBUGGER_ID, monitor.events.PY_UNWIND, None)
        sys.monitoring.free_tool_id(DEBUGGER_ID)


@pytest.fixture
def with_monitoring():
    monitor.use_tool_id(DEBUGGER_ID, "pydevd")
    yield
    _disable_monitoring()


def test_exceptions(with_monitoring):
    monitor.set_events(DEBUGGER_ID, monitor.events.RAISE | monitor.events.RERAISE)

    found = []

    def _on_raise(code, instruction_offset, exc):
        if code.co_filename.endswith("sys_monitoring.py"):
            found.append(("raise", code.co_name, str(exc), sys._getframe(1).f_lineno))

    def _on_reraise(code, instruction_offset, exc):
        if code.co_filename.endswith("sys_monitoring.py"):
            found.append(("reraise", code.co_name, str(exc), sys._getframe(1).f_lineno))

    monitor.register_callback(DEBUGGER_ID, monitor.events.RAISE, _on_raise)
    monitor.register_callback(DEBUGGER_ID, monitor.events.RERAISE, _on_reraise)

    def method_raise():
        raise RuntimeError("err1")

    def method_2():
        try:
            method_raise()
        except:
            raise

    def method():
        try:
            method_2()
        except:
            pass

    method()
    assert found == [
        ("raise", "method_raise", "err1", method_raise.__code__.co_firstlineno + 1),
        ("raise", "method_2", "err1", method_2.__code__.co_firstlineno + 2),
        # This will be very tricky to handle.
        # See: https://github.com/python/cpython/issues/112086
        ("reraise", "method_2", "err1", method_2.__code__.co_firstlineno + 4),
        ("reraise", "method_2", "err1", method_2.__code__.co_firstlineno + 4),
        ("raise", "method", "err1", method.__code__.co_firstlineno + 2),
    ]


def test_exceptions_and_return(with_monitoring):
    monitor.set_events(DEBUGGER_ID, monitor.events.RAISE | monitor.events.PY_RETURN | monitor.events.PY_UNWIND)

    found = []

    def _on_raise(code, instruction_offset, exc):
        if code.co_filename.endswith("sys_monitoring.py"):
            found.append(("raise", code.co_name, str(exc), sys._getframe(1).f_lineno))

    def _on_return(code, instruction_offset, val):
        if code.co_filename.endswith("sys_monitoring.py"):
            found.append(("return", code.co_name, str(val), sys._getframe(1).f_lineno))

    def _on_unwind(code, instruction_offset, val):
        if code.co_filename.endswith("sys_monitoring.py"):
            found.append(("unwind", code.co_name, str(val), sys._getframe(1).f_lineno))

    monitor.register_callback(DEBUGGER_ID, monitor.events.RAISE, _on_raise)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RETURN, _on_return)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_UNWIND, _on_unwind)

    def method_raise():
        raise RuntimeError("err1")

    def method_2():
        try:
            method_raise()
        except:
            raise

    def method():
        try:
            method_2()
        except:
            pass

    method()

    assert found == [
        ("raise", "method_raise", "err1", 96),
        ("unwind", "method_raise", "err1", 96),
        ("raise", "method_2", "err1", 100),
        ("unwind", "method_2", "err1", 102),  # This will be helpful for unhandled exceptions!
        ("raise", "method", "err1", 106),
        ("return", "method", "None", 108),
    ]


def test_variables_on_call(with_monitoring):
    monitor.set_events(DEBUGGER_ID, monitor.events.PY_START)

    found = []

    def _start_method(code, offset):
        if code.co_name == "method":
            frame = sys._getframe(1)
            found.append(frame.f_locals["arg1"])

    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method)

    def method(arg1):
        pass

    method(22)
    assert found == [22]


def test_disabling_code(with_monitoring):
    executed = []

    def _start_method(code, offset):
        if code.co_name == "method":
            executed.append(("start", code.co_name, offset))
            monitor.set_local_events(DEBUGGER_ID, code, monitor.events.LINE)
        return monitor.DISABLE

    def _on_line(code, offset):
        if code.co_name == "method":
            executed.append(("line", code.co_name, offset))
        return monitor.DISABLE

    monitor.set_events(DEBUGGER_ID, monitor.events.PY_START | monitor.events.PY_RESUME)

    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RESUME, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.LINE, _on_line)

    def method():
        a = 1

    method()
    method()
    assert executed == [("start", "method", 0), ("line", "method", method.__code__.co_firstlineno + 1)]

    del executed[:]

    # Check: if disable once, even on a new thread we won't get notifications!
    t = threading.Thread(target=method)
    t.start()
    t.join()

    assert not executed

    # Unless restart_events is called...
    monitor.restart_events()
    t = threading.Thread(target=method)
    t.start()
    t.join()
    assert executed == [("start", "method", 0), ("line", "method", method.__code__.co_firstlineno + 1)]


def test_change_line_during_trace(with_monitoring):
    code_to_break_at_line = {}
    do_change_line = [0]
    lines_traced = []

    def _start_method(code, offset):
        monitor.set_local_events(DEBUGGER_ID, code, monitor.events.LINE)
        code_to_break_at_line[code] = {code.co_firstlineno + 3}
        return monitor.DISABLE

    def _on_line(code, line):
        lines_to_break = code_to_break_at_line.get(code)
        if lines_to_break and line in lines_to_break:
            do_change_line[0] += 1
            if do_change_line[0] == 2:
                frame = sys._getframe(1)
                frame.f_lineno = line - 2

    monitor.set_events(DEBUGGER_ID, monitor.events.PY_START | monitor.events.PY_RESUME)

    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RESUME, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.LINE, _on_line)

    def method1():  # code.co_firstlineno
        a = 1  # code.co_firstlineno + 1
        lines_traced.append("before a=2")  # code.co_firstlineno + 2
        a = 2  # code.co_firstlineno + 3
        lines_traced.append("before a=3")  # code.co_firstlineno + 4
        # a = 3  # code.co_firstlineno + 5

    for _i in range(3):
        method1()

    assert lines_traced == [
        "before a=2",
        "before a=3",
        "before a=2",
        "before a=2",
        "before a=3",
        "before a=2",
        "before a=3",
    ]


def test_tracing():
    import sys

    def method():
        a = [1, 2, 3, 4, 5, 6]  # line 1

        # line 3
        def b():  #  line 4
            yield from [j for j in a if j % 2 == 0]  # line 5

        # line 7
        for j in b():  # line 8
            print(j)  # line 9

    def tracefunc(frame, event, arg):
        if "test_sys_monitoring.py" in frame.f_code.co_filename:
            print(frame.f_code.co_name, event, frame.f_lineno - frame.f_code.co_firstlineno)
            return tracefunc

    sys.settrace(tracefunc)
    method()
    sys.settrace(None)


def test_lines_with_yield(with_monitoring):
    def _start_method(code, offset):
        if "test_sys_monitoring.py" in code.co_filename:
            print("start ", code.co_name)
            monitor.set_local_events(DEBUGGER_ID, code, monitor.events.LINE | monitor.events.JUMP | monitor.events.PY_YIELD)

    def _on_line(code, line):
        if "test_sys_monitoring.py" in code.co_filename:
            print("on line", code.co_name, line - code.co_firstlineno)

    def _on_jump(code, from_offset, to_offset):
        if "test_sys_monitoring.py" in code.co_filename:
            frame = sys._getframe().f_back
            print("on jump", code.co_name, frame.f_lineno - code.co_firstlineno)

    def _yield_method(code, offset, retval):
        if "test_sys_monitoring.py" in code.co_filename:
            frame = sys._getframe().f_back
            print("on yield", code.co_name, frame.f_lineno - code.co_firstlineno)

    monitor.set_events(DEBUGGER_ID, monitor.events.PY_START | monitor.events.PY_RESUME)

    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_RESUME, _start_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.PY_YIELD, _yield_method)
    monitor.register_callback(DEBUGGER_ID, monitor.events.LINE, _on_line)
    monitor.register_callback(DEBUGGER_ID, monitor.events.JUMP, _on_jump)

    def method():
        a = [1, 2, 3, 4, 5, 6]  # line 1

        # line 3
        def b():  #  line 4
            yield from [j for j in a if j % 2 == 0]  # line 5

        # line 7
        for j in b():  # line 8
            print(j)  # line 9

    method()
    monitor.set_events(DEBUGGER_ID, 0)