blob: 1e3cecd7bf89ae282355ba036d783dd8a787595a [file] [log] [blame]
Georg Brandl46b9afc2010-07-30 09:14:20 +00001# A test suite for pdb; not very comprehensive at the moment.
Georg Brandl243ad662009-05-05 09:00:19 +00002
3import imp
Georg Brandl243ad662009-05-05 09:00:19 +00004import sys
Georg Brandl243ad662009-05-05 09:00:19 +00005
6from test import support
7# This little helper class is essential for testing pdb under doctest.
8from test.test_doctest import _FakeInput
9
10
Georg Brandl9fa2e022009-09-16 16:40:45 +000011class PdbTestInput(object):
12 """Context manager that makes testing Pdb in doctests easier."""
13
14 def __init__(self, input):
15 self.input = input
16
17 def __enter__(self):
18 self.real_stdin = sys.stdin
19 sys.stdin = _FakeInput(self.input)
20
21 def __exit__(self, *exc):
22 sys.stdin = self.real_stdin
23
24
25def test_pdb_displayhook():
26 """This tests the custom displayhook for pdb.
27
28 >>> def test_function(foo, bar):
29 ... import pdb; pdb.Pdb().set_trace()
30 ... pass
31
32 >>> with PdbTestInput([
33 ... 'foo',
34 ... 'bar',
35 ... 'for i in range(5): print(i)',
36 ... 'continue',
37 ... ]):
38 ... test_function(1, None)
39 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
40 -> pass
41 (Pdb) foo
42 1
43 (Pdb) bar
44 (Pdb) for i in range(5): print(i)
45 0
46 1
47 2
48 3
49 4
50 (Pdb) continue
51 """
52
53
Georg Brandl243ad662009-05-05 09:00:19 +000054def test_pdb_skip_modules():
55 """This illustrates the simple case of module skipping.
56
57 >>> def skip_module():
58 ... import string
59 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
60 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +000061
Georg Brandl9fa2e022009-09-16 16:40:45 +000062 >>> with PdbTestInput([
63 ... 'step',
64 ... 'continue',
65 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000066 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +000067 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
68 -> string.capwords('FOO')
69 (Pdb) step
70 --Return--
71 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
72 -> string.capwords('FOO')
73 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +000074 """
Georg Brandl243ad662009-05-05 09:00:19 +000075
76
77# Module for testing skipping of module that makes a callback
78mod = imp.new_module('module_to_skip')
79exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
80
81
82def test_pdb_skip_modules_with_callback():
83 """This illustrates skipping of modules that call into other code.
84
85 >>> def skip_module():
86 ... def callback():
87 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +000088 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +000089 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +000090
Georg Brandl9fa2e022009-09-16 16:40:45 +000091 >>> with PdbTestInput([
92 ... 'step',
93 ... 'step',
94 ... 'step',
95 ... 'step',
96 ... 'step',
97 ... 'continue',
98 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000099 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000100 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000101 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
102 -> mod.foo_pony(callback)
103 (Pdb) step
104 --Call--
105 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
106 -> def callback():
107 (Pdb) step
108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
109 -> return None
110 (Pdb) step
111 --Return--
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
113 -> return None
114 (Pdb) step
115 --Return--
116 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
117 -> mod.foo_pony(callback)
118 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
120 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000121 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000122 """
Georg Brandl243ad662009-05-05 09:00:19 +0000123
124
Georg Brandl3f940892010-07-30 10:29:19 +0000125def test_pdb_continue_in_bottomframe():
126 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
127
128 >>> def test_function():
129 ... import pdb, sys; inst = pdb.Pdb()
130 ... inst.set_trace()
131 ... inst.botframe = sys._getframe() # hackery to get the right botframe
132 ... print(1)
133 ... print(2)
134 ... print(3)
135 ... print(4)
136
Georg Brandl7410dd12010-07-30 12:01:20 +0000137 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000138 ... 'next',
139 ... 'break 7',
140 ... 'continue',
141 ... 'next',
142 ... 'continue',
143 ... 'continue',
144 ... ]):
145 ... test_function()
146 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
147 -> inst.botframe = sys._getframe() # hackery to get the right botframe
148 (Pdb) next
149 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
150 -> print(1)
151 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000152 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000153 (Pdb) continue
154 1
155 2
156 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
157 -> print(3)
158 (Pdb) next
159 3
160 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
161 -> print(4)
162 (Pdb) continue
163 4
164 """
165
166
Georg Brandl7410dd12010-07-30 12:01:20 +0000167def test_pdb_breakpoints():
168 """Test handling of breakpoints.
169
170 >>> def test_function():
171 ... import pdb; pdb.Pdb().set_trace()
172 ... print(1)
173 ... print(2)
174 ... print(3)
175 ... print(4)
176
177 First, need to clear bdb state that might be left over from previous tests.
178 Otherwise, the new breakpoints might get assigned different numbers.
179
180 >>> from bdb import Breakpoint
181 >>> Breakpoint.next = 1
182 >>> Breakpoint.bplist = {}
183 >>> Breakpoint.bpbynumber = [None]
184
185 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
186 the breakpoint list outputs a tab for the "stop only" and "ignore next"
187 lines, which we don't want to put in here.
188
189 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
190 ... 'break 3',
191 ... 'disable 1',
192 ... 'ignore 1 10',
193 ... 'condition 1 1 < 2',
194 ... 'break 4',
195 ... 'break',
196 ... 'condition 1',
197 ... 'enable 1',
198 ... 'clear 1',
199 ... 'commands 2',
200 ... 'print 42',
201 ... 'end',
202 ... 'continue', # will stop at breakpoint 2
203 ... 'continue',
204 ... ]):
205 ... test_function()
206 > <doctest test.test_pdb.test_pdb_breakpoints[0]>(3)test_function()
207 -> print(1)
208 (Pdb) break 3
209 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
210 (Pdb) disable 1
211 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
212 (Pdb) ignore 1 10
213 Will ignore next 10 crossings of breakpoint 1.
214 (Pdb) condition 1 1 < 2
215 New condition set for breakpoint 1.
216 (Pdb) break 4
217 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4
218 (Pdb) break
219 Num Type Disp Enb Where
220 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
221 stop only if 1 < 2
222 ignore next 10 hits
223 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4
224 (Pdb) condition 1
225 Breakpoint 1 is now unconditional.
226 (Pdb) enable 1
227 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
228 (Pdb) clear 1
229 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
230 (Pdb) commands 2
231 (com) print 42
232 (com) end
233 (Pdb) continue
234 1
235 42
236 > <doctest test.test_pdb.test_pdb_breakpoints[0]>(4)test_function()
237 -> print(2)
238 (Pdb) continue
239 2
240 3
241 4
242 """
243
244
Georg Brandl46b9afc2010-07-30 09:14:20 +0000245def pdb_invoke(method, arg):
246 """Run pdb.method(arg)."""
247 import pdb; getattr(pdb, method)(arg)
248
249
250def test_pdb_run_with_incorrect_argument():
251 """Testing run and runeval with incorrect first argument.
252
253 >>> pti = PdbTestInput(['continue',])
254 >>> with pti:
255 ... pdb_invoke('run', lambda x: x)
256 Traceback (most recent call last):
257 TypeError: exec() arg 1 must be a string, bytes or code object
258
259 >>> with pti:
260 ... pdb_invoke('runeval', lambda x: x)
261 Traceback (most recent call last):
262 TypeError: eval() arg 1 must be a string, bytes or code object
263 """
264
265
266def test_pdb_run_with_code_object():
267 """Testing run and runeval with code object as a first argument.
268
269 >>> with PdbTestInput(['step','x', 'continue']):
270 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
271 > <string>(1)<module>()
272 (Pdb) step
273 --Return--
274 > <string>(1)<module>()->None
275 (Pdb) x
276 1
277 (Pdb) continue
278
279 >>> with PdbTestInput(['x', 'continue']):
280 ... x=0
281 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
282 > <string>(1)<module>()->None
283 (Pdb) x
284 1
285 (Pdb) continue
286 """
287
288
Georg Brandl243ad662009-05-05 09:00:19 +0000289def test_main():
290 from test import test_pdb
291 support.run_doctest(test_pdb, verbosity=True)
292
293
294if __name__ == '__main__':
295 test_main()