From cc91ac09efa5dc269ac1d8c55ffd64f1a6812870 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Tue, 25 Sep 2001 20:48:14 +0000 Subject: [PATCH] Factor out the protect-from-exceptions helpers and make capture_events() use it. This simplifies the individual tests a little. Added some new tests related to exception handling. --- Lib/test/test_profilehooks.py | 100 ++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/Lib/test/test_profilehooks.py b/Lib/test/test_profilehooks.py index c24772856b8..b620a9c8f45 100644 --- a/Lib/test/test_profilehooks.py +++ b/Lib/test/test_profilehooks.py @@ -46,51 +46,49 @@ def test_simple(self): def f(p): pass f_ident = ident(f) - self.check_events(f, [(0, 'call', f_ident), - (0, 'return', f_ident), + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), ]) def test_exception(self): + def f(p): + 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'exception', f_ident), + (0, 'exception', protect_ident), + ]) + + def test_caught_exception(self): def f(p): try: 1/0 except: pass f_ident = ident(f) - self.check_events(f, [(0, 'call', f_ident), - (0, 'exception', f_ident), - (0, 'return', f_ident), + self.check_events(f, [(1, 'call', f_ident), + (1, 'exception', f_ident), + (1, 'return', f_ident), ]) def test_caught_nested_exception(self): def f(p): try: 1/0 except: pass - def g(p): - f(p) f_ident = ident(f) - g_ident = ident(g) - self.check_events(g, [(0, 'call', g_ident), - (1, 'call', f_ident), + self.check_events(f, [(1, 'call', f_ident), (1, 'exception', f_ident), (1, 'return', f_ident), - (0, 'return', g_ident), ]) def test_nested_exception(self): def f(p): 1/0 - def g(p): - try: f(p) - except: pass f_ident = ident(f) - g_ident = ident(g) - self.check_events(g, [(0, 'call', g_ident), - (1, 'call', f_ident), + self.check_events(f, [(1, 'call', f_ident), (1, 'exception', f_ident), # This isn't what I expected: - (0, 'exception', g_ident), + (0, 'exception', protect_ident), # I expected this again: # (1, 'exception', f_ident), - (0, 'return', g_ident), ]) def test_exception_in_except_clause(self): @@ -104,14 +102,14 @@ def g(p): except: pass f_ident = ident(f) g_ident = ident(g) - self.check_events(g, [(0, 'call', g_ident), - (1, 'call', f_ident), - (1, 'exception', f_ident), - (0, 'exception', g_ident), + self.check_events(g, [(1, 'call', g_ident), (2, 'call', f_ident), (2, 'exception', f_ident), - (0, 'exception', g_ident), - (0, 'return', g_ident), + (1, 'exception', g_ident), + (3, 'call', f_ident), + (3, 'exception', f_ident), + (1, 'exception', g_ident), + (1, 'return', g_ident), ]) def test_exception_propogation(self): @@ -120,22 +118,47 @@ def f(p): def g(p): try: f(p) finally: p.add_event("falling through") - def h(p): - try: g(p) - except: pass f_ident = ident(f) g_ident = ident(g) - h_ident = ident(h) - self.check_events(h, [(0, 'call', h_ident), - (1, 'call', g_ident), + self.check_events(g, [(1, 'call', g_ident), (2, 'call', f_ident), (2, 'exception', f_ident), (1, 'exception', g_ident), (1, 'falling through', g_ident), - (0, 'exception', h_ident), - (0, 'return', h_ident), + (0, 'exception', protect_ident), ]) + def test_raise_twice(self): + def f(p): + try: 1/0 + except: 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'exception', f_ident), + (1, 'exception', f_ident), + (0, 'exception', protect_ident) + ]) + + def test_raise_reraise(self): + def f(p): + try: 1/0 + except: raise + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'exception', f_ident), + (0, 'exception', protect_ident) + ]) + + def test_raise(self): + def f(p): + raise Exception() + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'exception', f_ident), + (0, 'exception', protect_ident) + ]) + + def ident(function): if hasattr(function, "f_code"): code = function.f_code @@ -144,12 +167,19 @@ def ident(function): return code.co_firstlineno, code.co_name +def protect(f, p): + try: f(p) + except: pass + +protect_ident = ident(protect) + + def capture_events(callable): p = HookWatcher() sys.setprofile(p.callback) - callable(p) + protect(callable, p) sys.setprofile(None) - return p.get_events() + return p.get_events()[1:-1] def show_events(callable):