diff options
author | Fred Drake <fdrake@acm.org> | 2001-09-25 20:48:14 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-09-25 20:48:14 (GMT) |
commit | cc91ac09efa5dc269ac1d8c55ffd64f1a6812870 (patch) | |
tree | 806e50e8083e5e3577374f1c806c560e1d933a54 /Lib | |
parent | 8dee809410e2d433bb0be5d8a1699736b90db0b5 (diff) | |
download | cpython-cc91ac09efa5dc269ac1d8c55ffd64f1a6812870.zip cpython-cc91ac09efa5dc269ac1d8c55ffd64f1a6812870.tar.gz cpython-cc91ac09efa5dc269ac1d8c55ffd64f1a6812870.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_profilehooks.py | 100 |
1 files changed, 65 insertions, 35 deletions
diff --git a/Lib/test/test_profilehooks.py b/Lib/test/test_profilehooks.py index c247728..b620a9c 100644 --- a/Lib/test/test_profilehooks.py +++ b/Lib/test/test_profilehooks.py @@ -46,51 +46,49 @@ class ProfileHookTestCase(unittest.TestCase): 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 @@ class ProfileHookTestCase(unittest.TestCase): 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 @@ class ProfileHookTestCase(unittest.TestCase): 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): |