blob: 2589141615b5ca6e390ef8bcdf40ce2bc3589b87 [file] [log] [blame]
Georg Brandl243ad662009-05-05 09:00:19 +00001# A test suite for pdb; at the moment, this only validates skipping of
2# specified test modules (RFE #5142).
3
4import imp
5import os
6import sys
7import doctest
8import tempfile
9
10from test import support
11# This little helper class is essential for testing pdb under doctest.
12from test.test_doctest import _FakeInput
13
14
Georg Brandl22fff432009-10-27 20:19:02 +000015class PdbTestInput(object):
16 """Context manager that makes testing Pdb in doctests easier."""
17
18 def __init__(self, input):
19 self.input = input
20
21 def __enter__(self):
22 self.real_stdin = sys.stdin
23 sys.stdin = _FakeInput(self.input)
24
25 def __exit__(self, *exc):
26 sys.stdin = self.real_stdin
27
28
29def test_pdb_displayhook():
30 """This tests the custom displayhook for pdb.
31
32 >>> def test_function(foo, bar):
33 ... import pdb; pdb.Pdb().set_trace()
34 ... pass
35
36 >>> with PdbTestInput([
37 ... 'foo',
38 ... 'bar',
39 ... 'for i in range(5): print(i)',
40 ... 'continue',
41 ... ]):
42 ... test_function(1, None)
43 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
44 -> pass
45 (Pdb) foo
46 1
47 (Pdb) bar
48 (Pdb) for i in range(5): print(i)
49 0
50 1
51 2
52 3
53 4
54 (Pdb) continue
55 """
56
Senthil Kumaran5170c812010-11-29 12:27:45 +000057def test_pdb_breakpoint_commands():
58 """Test basic commands related to breakpoints.
59
60 >>> def test_function():
61 ... import pdb; pdb.Pdb().set_trace()
62 ... print(1)
63 ... print(2)
64 ... print(3)
65 ... print(4)
66
67 First, need to clear bdb state that might be left over from previous tests.
68 Otherwise, the new breakpoints might get assigned different numbers.
69
70 >>> from bdb import Breakpoint
71 >>> Breakpoint.next = 1
72 >>> Breakpoint.bplist = {}
73 >>> Breakpoint.bpbynumber = [None]
74
75 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
76 the breakpoint list outputs a tab for the "stop only" and "ignore next"
77 lines, which we don't want to put in here.
78
79 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
80 ... 'break 3',
81 ... 'disable 1',
82 ... 'ignore 1 10',
83 ... 'condition 1 1 < 2',
84 ... 'break 4',
85 ... 'break 4',
86 ... 'break',
87 ... 'clear 3',
88 ... 'break',
89 ... 'condition 1',
90 ... 'enable 1',
91 ... 'clear 1',
92 ... 'commands 2',
93 ... 'print 42',
94 ... 'end',
95 ... 'continue', # will stop at breakpoint 2 (line 4)
96 ... 'clear', # clear all!
97 ... 'y',
98 ... 'tbreak 5',
99 ... 'continue', # will stop at temporary breakpoint
100 ... 'break', # make sure breakpoint is gone
101 ... 'continue',
102 ... ]):
103 ... test_function()
104 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
105 -> print(1)
106 (Pdb) break 3
107 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
108 (Pdb) disable 1
109 (Pdb) ignore 1 10
110 Will ignore next 10 crossings of breakpoint 1.
111 (Pdb) condition 1 1 < 2
112 (Pdb) break 4
113 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
114 (Pdb) break 4
115 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
116 (Pdb) break
117 Num Type Disp Enb Where
118 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
119 stop only if 1 < 2
120 ignore next 10 hits
121 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
122 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
123 (Pdb) clear 3
124 Deleted breakpoint 3
125 (Pdb) break
126 Num Type Disp Enb Where
127 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
128 stop only if 1 < 2
129 ignore next 10 hits
130 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
131 (Pdb) condition 1
132 Breakpoint 1 is now unconditional.
133 (Pdb) enable 1
134 (Pdb) clear 1
135 Deleted breakpoint 1
136 (Pdb) commands 2
137 (com) print 42
138 (com) end
139 (Pdb) continue
140 1
141 42
142 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
143 -> print(2)
144 (Pdb) clear
145 Clear all breaks? y
146 (Pdb) tbreak 5
147 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
148 (Pdb) continue
149 2
150 Deleted breakpoint 4
151 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
152 -> print(3)
153 (Pdb) break
154 (Pdb) continue
155 3
156 4
157 """
Georg Brandl22fff432009-10-27 20:19:02 +0000158
Georg Brandl243ad662009-05-05 09:00:19 +0000159def test_pdb_skip_modules():
160 """This illustrates the simple case of module skipping.
161
162 >>> def skip_module():
163 ... import string
164 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
165 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000166
Georg Brandl22fff432009-10-27 20:19:02 +0000167 >>> with PdbTestInput([
168 ... 'step',
169 ... 'continue',
170 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000171 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000172 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
173 -> string.capwords('FOO')
174 (Pdb) step
175 --Return--
176 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
177 -> string.capwords('FOO')
178 (Pdb) continue
Georg Brandl22fff432009-10-27 20:19:02 +0000179 """
Georg Brandl243ad662009-05-05 09:00:19 +0000180
181
182# Module for testing skipping of module that makes a callback
183mod = imp.new_module('module_to_skip')
184exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
185
186
187def test_pdb_skip_modules_with_callback():
188 """This illustrates skipping of modules that call into other code.
189
190 >>> def skip_module():
191 ... def callback():
192 ... return None
Georg Brandl22fff432009-10-27 20:19:02 +0000193 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000194 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000195
Georg Brandl22fff432009-10-27 20:19:02 +0000196 >>> with PdbTestInput([
197 ... 'step',
198 ... 'step',
199 ... 'step',
200 ... 'step',
201 ... 'step',
202 ... 'continue',
203 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000204 ... skip_module()
Georg Brandl22fff432009-10-27 20:19:02 +0000205 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000206 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
207 -> mod.foo_pony(callback)
208 (Pdb) step
209 --Call--
210 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
211 -> def callback():
212 (Pdb) step
213 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
214 -> return None
215 (Pdb) step
216 --Return--
217 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
218 -> return None
219 (Pdb) step
220 --Return--
221 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
222 -> mod.foo_pony(callback)
223 (Pdb) step
Georg Brandl22fff432009-10-27 20:19:02 +0000224 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
225 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000226 (Pdb) continue
Georg Brandl22fff432009-10-27 20:19:02 +0000227 """
Georg Brandl243ad662009-05-05 09:00:19 +0000228
229
Georg Brandl469d3e72010-08-01 19:35:16 +0000230def test_pdb_continue_in_bottomframe():
231 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
232
233 >>> def test_function():
234 ... import pdb, sys; inst = pdb.Pdb()
235 ... inst.set_trace()
236 ... inst.botframe = sys._getframe() # hackery to get the right botframe
237 ... print(1)
238 ... print(2)
239 ... print(3)
240 ... print(4)
241
Senthil Kumaran5170c812010-11-29 12:27:45 +0000242 First, need to clear bdb state that might be left over from previous tests.
243 Otherwise, the new breakpoints might get assigned different numbers.
244
245 >>> from bdb import Breakpoint
246 >>> Breakpoint.next = 1
247 >>> Breakpoint.bplist = {}
248 >>> Breakpoint.bpbynumber = [None]
249
Georg Brandl469d3e72010-08-01 19:35:16 +0000250 >>> with PdbTestInput([
251 ... 'next',
252 ... 'break 7',
253 ... 'continue',
254 ... 'next',
255 ... 'continue',
256 ... 'continue',
257 ... ]):
258 ... test_function()
259 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
260 -> inst.botframe = sys._getframe() # hackery to get the right botframe
261 (Pdb) next
262 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
263 -> print(1)
264 (Pdb) break 7
265 Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
266 (Pdb) continue
267 1
268 2
269 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
270 -> print(3)
271 (Pdb) next
272 3
273 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
274 -> print(4)
275 (Pdb) continue
276 4
277 """
278
279
Senthil Kumaran5170c812010-11-29 12:27:45 +0000280
Georg Brandl243ad662009-05-05 09:00:19 +0000281def test_main():
282 from test import test_pdb
283 support.run_doctest(test_pdb, verbosity=True)
284
285
286if __name__ == '__main__':
287 test_main()