diff options
author | Fred Drake <fdrake@acm.org> | 2001-09-26 21:00:33 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-09-26 21:00:33 (GMT) |
commit | 39cd603fc884b60b8c7a6dad0346f819ea181f4a (patch) | |
tree | aec7704dc64116358cc656339ae4087dc630caba /Lib | |
parent | 38a50f9065ab27d5b5fcff5d01192ed1dd767e23 (diff) | |
download | cpython-39cd603fc884b60b8c7a6dad0346f819ea181f4a.zip cpython-39cd603fc884b60b8c7a6dad0346f819ea181f4a.tar.gz cpython-39cd603fc884b60b8c7a6dad0346f819ea181f4a.tar.bz2 |
More test cases, including something that simulates what the profiler
probably *should* be doing.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_profilehooks.py | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/Lib/test/test_profilehooks.py b/Lib/test/test_profilehooks.py index b620a9c..579443e 100644 --- a/Lib/test/test_profilehooks.py +++ b/Lib/test/test_profilehooks.py @@ -34,14 +34,48 @@ class HookWatcher: return [item for item in self.events if item[2] not in disallowed] -class ProfileHookTestCase(unittest.TestCase): +class ProfileSimulator(HookWatcher): + def __init__(self): + self.stack = [] + HookWatcher.__init__(self) + + def callback(self, frame, event, arg): + self.dispatch[event](self, frame) + + def trace_call(self, frame): + self.add_event('call', frame) + self.stack.append(frame) + + def trace_return(self, frame): + self.add_event('return', frame) + self.stack.pop() + + def trace_exception(self, frame): + if len(self.stack) >= 2 and frame is self.stack[-2]: + self.add_event('propogate-from', self.stack[-1]) + self.stack.pop() + else: + self.add_event('ignore', frame) + + dispatch = { + 'call': trace_call, + 'exception': trace_exception, + 'return': trace_return, + } + +class TestCaseBase(unittest.TestCase): def check_events(self, callable, expected): - events = capture_events(callable) + events = capture_events(callable, self.new_watcher()) if events != expected: self.fail("Expected events:\n%s\nReceived events:\n%s" % (pprint.pformat(expected), pprint.pformat(events))) + +class ProfileHookTestCase(TestCaseBase): + def new_watcher(self): + return HookWatcher() + def test_simple(self): def f(p): pass @@ -159,6 +193,28 @@ class ProfileHookTestCase(unittest.TestCase): ]) +class ProfileSimulatorTestCase(TestCaseBase): + def new_watcher(self): + return ProfileSimulator() + + def test_simple(self): + def f(p): + pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_basic_exception(self): + def f(p): + 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'ignore', f_ident), + (1, 'propogate-from', f_ident), + ]) + + def ident(function): if hasattr(function, "f_code"): code = function.f_code @@ -174,8 +230,9 @@ def protect(f, p): protect_ident = ident(protect) -def capture_events(callable): - p = HookWatcher() +def capture_events(callable, p=None): + if p is None: + p = HookWatcher() sys.setprofile(p.callback) protect(callable, p) sys.setprofile(None) @@ -188,7 +245,11 @@ def show_events(callable): def test_main(): - test_support.run_unittest(ProfileHookTestCase) + loader = unittest.TestLoader() + suite = unittest.TestSuite() + suite.addTest(loader.loadTestsFromTestCase(ProfileHookTestCase)) + suite.addTest(loader.loadTestsFromTestCase(ProfileSimulatorTestCase)) + test_support.run_suite(suite) if __name__ == "__main__": |