MCPcopy Index your code
hub / github.com/python/cpython / HookWatcher

Class HookWatcher

Lib/test/test_sys_setprofile.py:24–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 self.assertIs(sys.getprofile(), fn)
23
24class HookWatcher:
25 def __init__(self):
26 self.frames = []
27 self.events = []
28
29 def callback(self, frame, event, arg):
30 if (event == "call"
31 or event == "return"
32 or event == "exception"):
33 self.add_event(event, frame, arg)
34
35 def add_event(self, event, frame=None, arg=None):
36 """Add an event to the log."""
37 if frame is None:
38 frame = sys._getframe(1)
39
40 try:
41 frameno = self.frames.index(frame)
42 except ValueError:
43 frameno = len(self.frames)
44 self.frames.append(frame)
45
46 self.events.append((frameno, event, ident(frame), arg))
47
48 def get_events(self):
49 """Remove calls to add_event()."""
50 disallowed = [ident(self.add_event.__func__), ident(ident)]
51 self.frames = None
52
53 return [item for item in self.events if item[2] not in disallowed]
54
55
56class ProfileSimulator(HookWatcher):

Callers 2

new_watcherMethod · 0.85
capture_eventsFunction · 0.85

Calls

no outgoing calls

Tested by 2

new_watcherMethod · 0.68
capture_eventsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…