annotate Lib/test/test_profilehooks.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 424cccfd2baa
children d6ac3d09b79d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 import pprint
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 import sys
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 import unittest
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
5 from test import test_support
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
44822
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
7 class TestGetProfile(unittest.TestCase):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
8 def setUp(self):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
9 sys.setprofile(None)
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
10
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
11 def tearDown(self):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
12 sys.setprofile(None)
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
13
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
14 def test_empty(self):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
15 assert sys.getprofile() == None
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
16
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
17 def test_setget(self):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
18 def fn(*args):
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
19 pass
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
20
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
21 sys.setprofile(fn)
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
22 assert sys.getprofile() == fn
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 class HookWatcher:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 def __init__(self):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 self.frames = []
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 self.events = []
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 def callback(self, frame, event, arg):
31796
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
30 if (event == "call"
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
31 or event == "return"
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
32 or event == "exception"):
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
33 self.add_event(event, frame)
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 def add_event(self, event, frame=None):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 """Add an event to the log."""
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 if frame is None:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 frame = sys._getframe(1)
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 try:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 frameno = self.frames.index(frame)
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 except ValueError:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 frameno = len(self.frames)
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 self.frames.append(frame)
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 self.events.append((frameno, event, ident(frame)))
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 def get_events(self):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 """Remove calls to add_event()."""
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
50 disallowed = [ident(self.add_event.im_func), ident(ident)]
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
51 self.frames = None
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
53 return [item for item in self.events if item[2] not in disallowed]
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
56 class ProfileSimulator(HookWatcher):
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
57 def __init__(self, testcase):
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
58 self.testcase = testcase
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
59 self.stack = []
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
60 HookWatcher.__init__(self)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
61
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
62 def callback(self, frame, event, arg):
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
63 # Callback registered with sys.setprofile()/sys.settrace()
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
64 self.dispatch[event](self, frame)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
65
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
66 def trace_call(self, frame):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
67 self.add_event('call', frame)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
68 self.stack.append(frame)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
69
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
70 def trace_return(self, frame):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
71 self.add_event('return', frame)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
72 self.stack.pop()
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
74 def trace_exception(self, frame):
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
75 self.testcase.fail(
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
76 "the profiler should never receive exception events")
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
77
31796
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
78 def trace_pass(self, frame):
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
79 pass
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
80
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
81 dispatch = {
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
82 'call': trace_call,
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
83 'exception': trace_exception,
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
84 'return': trace_return,
31796
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
85 'c_call': trace_pass,
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
86 'c_return': trace_pass,
1c117dbb2def Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents: 28917
diff changeset
87 'c_exception': trace_pass,
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
88 }
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
89
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
90
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
91 class TestCaseBase(unittest.TestCase):
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 def check_events(self, callable, expected):
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
93 events = capture_events(callable, self.new_watcher())
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 if events != expected:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 self.fail("Expected events:\n%s\nReceived events:\n%s"
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 % (pprint.pformat(expected), pprint.pformat(events)))
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
98
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
99 class ProfileHookTestCase(TestCaseBase):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
100 def new_watcher(self):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
101 return HookWatcher()
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
102
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 def test_simple(self):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 def f(p):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 pass
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 f_ident = ident(f)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
107 self.check_events(f, [(1, 'call', f_ident),
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
108 (1, 'return', f_ident),
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 ])
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 def test_exception(self):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 def f(p):
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
113 1/0
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
114 f_ident = ident(f)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
115 self.check_events(f, [(1, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
116 (1, 'return', f_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
117 ])
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
118
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
119 def test_caught_exception(self):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
120 def f(p):
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
121 try: 1/0
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
122 except: pass
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 f_ident = ident(f)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
124 self.check_events(f, [(1, 'call', f_ident),
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
125 (1, 'return', f_ident),
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 ])
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
128 def test_caught_nested_exception(self):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
129 def f(p):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
130 try: 1/0
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
131 except: pass
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
132 f_ident = ident(f)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
133 self.check_events(f, [(1, 'call', f_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
134 (1, 'return', f_ident),
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
135 ])
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
136
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 def test_nested_exception(self):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 def f(p):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 1/0
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 f_ident = ident(f)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
141 self.check_events(f, [(1, 'call', f_ident),
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 # This isn't what I expected:
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
143 # (0, 'exception', protect_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
144 # I expected this again:
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
145 (1, 'return', f_ident),
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146 ])
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
148 def test_exception_in_except_clause(self):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
149 def f(p):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
150 1/0
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
151 def g(p):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
152 try:
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
153 f(p)
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
154 except:
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
155 try: f(p)
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
156 except: pass
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
157 f_ident = ident(f)
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
158 g_ident = ident(g)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
159 self.check_events(g, [(1, 'call', g_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
160 (2, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
161 (2, 'return', f_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
162 (3, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
163 (3, 'return', f_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
164 (1, 'return', g_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
165 ])
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
166
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
167 def test_exception_propogation(self):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
168 def f(p):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
169 1/0
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
170 def g(p):
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
171 try: f(p)
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
172 finally: p.add_event("falling through")
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
173 f_ident = ident(f)
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
174 g_ident = ident(g)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
175 self.check_events(g, [(1, 'call', g_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
176 (2, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
177 (2, 'return', f_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
178 (1, 'falling through', g_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
179 (1, 'return', g_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
180 ])
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
181
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
182 def test_raise_twice(self):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
183 def f(p):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
184 try: 1/0
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
185 except: 1/0
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
186 f_ident = ident(f)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
187 self.check_events(f, [(1, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
188 (1, 'return', f_ident),
20062
6390139ad082 Add more tests showing the relationship between exceptions raised & caught
Fred Drake <fdrake@acm.org>
parents: 20029
diff changeset
189 ])
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
191 def test_raise_reraise(self):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
192 def f(p):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
193 try: 1/0
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
194 except: raise
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
195 f_ident = ident(f)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
196 self.check_events(f, [(1, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
197 (1, 'return', f_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
198 ])
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
199
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
200 def test_raise(self):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
201 def f(p):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
202 raise Exception()
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
203 f_ident = ident(f)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
204 self.check_events(f, [(1, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
205 (1, 'return', f_ident),
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
206 ])
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
207
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
208 def test_distant_exception(self):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
209 def f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
210 1/0
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
211 def g():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
212 f()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
213 def h():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
214 g()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
215 def i():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
216 h()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
217 def j(p):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
218 i()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
219 f_ident = ident(f)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
220 g_ident = ident(g)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
221 h_ident = ident(h)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
222 i_ident = ident(i)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
223 j_ident = ident(j)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
224 self.check_events(j, [(1, 'call', j_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
225 (2, 'call', i_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
226 (3, 'call', h_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
227 (4, 'call', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
228 (5, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
229 (5, 'return', f_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
230 (4, 'return', g_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
231 (3, 'return', h_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
232 (2, 'return', i_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
233 (1, 'return', j_ident),
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
234 ])
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
235
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
236 def test_generator(self):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
237 def f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
238 for i in range(2):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
239 yield i
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
240 def g(p):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
241 for i in f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
242 pass
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
243 f_ident = ident(f)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
244 g_ident = ident(g)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
245 self.check_events(g, [(1, 'call', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
246 # call the iterator twice to generate values
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
247 (2, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
248 (2, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
249 (2, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
250 (2, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
251 # once more; returns end-of-iteration with
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
252 # actually raising an exception
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
253 (2, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
254 (2, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
255 (1, 'return', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
256 ])
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
257
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
258 def test_stop_iteration(self):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
259 def f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
260 for i in range(2):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
261 yield i
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
262 raise StopIteration
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
263 def g(p):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
264 for i in f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
265 pass
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
266 f_ident = ident(f)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
267 g_ident = ident(g)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
268 self.check_events(g, [(1, 'call', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
269 # call the iterator twice to generate values
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
270 (2, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
271 (2, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
272 (2, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
273 (2, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
274 # once more to hit the raise:
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
275 (2, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
276 (2, 'return', f_ident),
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
277 (1, 'return', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
278 ])
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
279
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
280
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
281 class ProfileSimulatorTestCase(TestCaseBase):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
282 def new_watcher(self):
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
283 return ProfileSimulator(self)
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
284
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
285 def test_simple(self):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
286 def f(p):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
287 pass
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
288 f_ident = ident(f)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
289 self.check_events(f, [(1, 'call', f_ident),
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
290 (1, 'return', f_ident),
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
291 ])
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
292
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
293 def test_basic_exception(self):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
294 def f(p):
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
295 1/0
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
296 f_ident = ident(f)
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
297 self.check_events(f, [(1, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
298 (1, 'return', f_ident),
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
299 ])
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
300
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
301 def test_caught_exception(self):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
302 def f(p):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
303 try: 1/0
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
304 except: pass
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
305 f_ident = ident(f)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
306 self.check_events(f, [(1, 'call', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
307 (1, 'return', f_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
308 ])
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
309
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
310 def test_distant_exception(self):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
311 def f():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
312 1/0
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
313 def g():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
314 f()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
315 def h():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
316 g()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
317 def i():
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
318 h()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
319 def j(p):
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
320 i()
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
321 f_ident = ident(f)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
322 g_ident = ident(g)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
323 h_ident = ident(h)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
324 i_ident = ident(i)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
325 j_ident = ident(j)
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
326 self.check_events(j, [(1, 'call', j_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
327 (2, 'call', i_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
328 (3, 'call', h_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
329 (4, 'call', g_ident),
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
330 (5, 'call', f_ident),
20248
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
331 (5, 'return', f_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
332 (4, 'return', g_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
333 (3, 'return', h_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
334 (2, 'return', i_ident),
10b97ae69025 Updated to reflect the rationalized profiler event reporting.
Fred Drake <fdrake@acm.org>
parents: 20236
diff changeset
335 (1, 'return', j_ident),
20236
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
336 ])
9ee6a98f032c Add some more test cases to be sure we do the right thing in various cases.
Fred Drake <fdrake@acm.org>
parents: 20119
diff changeset
337
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
338
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 def ident(function):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 if hasattr(function, "f_code"):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 code = function.f_code
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 else:
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 code = function.func_code
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 return code.co_firstlineno, code.co_name
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
347 def protect(f, p):
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
348 try: f(p)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
349 except: pass
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
350
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
351 protect_ident = ident(protect)
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
352
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
353
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
354 def capture_events(callable, p=None):
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
355 try:
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
356 sys.setprofile()
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
357 except TypeError:
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
358 pass
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
359 else:
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
360 raise test_support.TestFailed(
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22719
diff changeset
361 'sys.setprofile() did not raise TypeError')
22119
0859e764509e SF #506611, fix sys.setprofile(), sys.settrace() core dumps
Neal Norwitz <nnorwitz@gmail.com>
parents: 20248
diff changeset
362
20119
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
363 if p is None:
b9f60b825261 More test cases, including something that simulates what the profiler
Fred Drake <fdrake@acm.org>
parents: 20092
diff changeset
364 p = HookWatcher()
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
365 sys.setprofile(p.callback)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
366 protect(callable, p)
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367 sys.setprofile(None)
20092
b52a63052306 Factor out the protect-from-exceptions helpers and make capture_events()
Fred Drake <fdrake@acm.org>
parents: 20062
diff changeset
368 return p.get_events()[1:-1]
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
369
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
370
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
371 def show_events(callable):
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 import pprint
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373 pprint.pprint(capture_events(callable))
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
374
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
375
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
376 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
377 test_support.run_unittest(
44822
424cccfd2baa #1648: add sys.gettrace() and sys.getprofile().
Georg Brandl <georg@python.org>
parents: 31796
diff changeset
378 TestGetProfile,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
379 ProfileHookTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
380 ProfileSimulatorTestCase
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
381 )
20029
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 if __name__ == "__main__":
4bbb4d9b4d77 Start of a test to make sure the profiler/tracer support in the core
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 test_main()