blob: a63ccd83ca89c2460e6f780ff555f77b98f3d7d5 [file] [log] [blame]
Georg Brandl46b9afc2010-07-30 09:14:20 +00001# A test suite for pdb; not very comprehensive at the moment.
Martin v. Löwis67880cc2012-05-02 07:41:22 +02002
Andrew Svetlovf0efea02013-03-18 10:09:50 -07003import doctest
Georg Brandl6cccb862010-07-30 14:16:43 +00004import pdb
Georg Brandl243ad662009-05-05 09:00:19 +00005import sys
Brett Cannon9529fbf2013-06-15 17:11:25 -04006import types
Georg Brandl6cccb862010-07-30 14:16:43 +00007import unittest
8import subprocess
Senthil Kumaran42d70812012-05-01 10:07:49 +08009import textwrap
Georg Brandl243ad662009-05-05 09:00:19 +000010
11from test import support
12# This little helper class is essential for testing pdb under doctest.
13from test.test_doctest import _FakeInput
14
15
Georg Brandl9fa2e022009-09-16 16:40:45 +000016class PdbTestInput(object):
17 """Context manager that makes testing Pdb in doctests easier."""
18
19 def __init__(self, input):
20 self.input = input
21
22 def __enter__(self):
23 self.real_stdin = sys.stdin
24 sys.stdin = _FakeInput(self.input)
Brett Cannon31f59292011-02-21 19:29:56 +000025 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000026
27 def __exit__(self, *exc):
28 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000029 if self.orig_trace:
30 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000031
32
33def test_pdb_displayhook():
34 """This tests the custom displayhook for pdb.
35
36 >>> def test_function(foo, bar):
Georg Brandl34748cd2010-12-04 17:11:36 +000037 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000038 ... pass
39
40 >>> with PdbTestInput([
41 ... 'foo',
42 ... 'bar',
43 ... 'for i in range(5): print(i)',
44 ... 'continue',
45 ... ]):
46 ... test_function(1, None)
47 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
48 -> pass
49 (Pdb) foo
50 1
51 (Pdb) bar
52 (Pdb) for i in range(5): print(i)
53 0
54 1
55 2
56 3
57 4
58 (Pdb) continue
59 """
60
61
Georg Brandl0d089622010-07-30 16:00:46 +000062def test_pdb_basic_commands():
63 """Test the basic commands of pdb.
64
65 >>> def test_function_2(foo, bar='default'):
66 ... print(foo)
67 ... for i in range(5):
68 ... print(i)
69 ... print(bar)
70 ... for i in range(10):
71 ... never_executed
72 ... print('after for')
73 ... print('...')
74 ... return foo.upper()
75
76 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +000077 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000078 ... ret = test_function_2('baz')
79 ... print(ret)
80
81 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
82 ... 'step', # entering the function call
83 ... 'args', # display function args
84 ... 'list', # list function source
85 ... 'bt', # display backtrace
86 ... 'up', # step up to test_function()
87 ... 'down', # step down to test_function_2() again
88 ... 'next', # stepping to print(foo)
89 ... 'next', # stepping to the for loop
90 ... 'step', # stepping into the for loop
91 ... 'until', # continuing until out of the for loop
92 ... 'next', # executing the print(bar)
93 ... 'jump 8', # jump over second for loop
94 ... 'return', # return out of function
95 ... 'retval', # display return value
96 ... 'continue',
97 ... ]):
98 ... test_function()
99 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
100 -> ret = test_function_2('baz')
101 (Pdb) step
102 --Call--
103 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
104 -> def test_function_2(foo, bar='default'):
105 (Pdb) args
106 foo = 'baz'
107 bar = 'default'
108 (Pdb) list
109 1 -> def test_function_2(foo, bar='default'):
110 2 print(foo)
111 3 for i in range(5):
112 4 print(i)
113 5 print(bar)
114 6 for i in range(10):
115 7 never_executed
116 8 print('after for')
117 9 print('...')
118 10 return foo.upper()
119 [EOF]
120 (Pdb) bt
121 ...
122 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
123 -> test_function()
124 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
125 -> ret = test_function_2('baz')
126 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
127 -> def test_function_2(foo, bar='default'):
128 (Pdb) up
129 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
130 -> ret = test_function_2('baz')
131 (Pdb) down
132 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
133 -> def test_function_2(foo, bar='default'):
134 (Pdb) next
135 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
136 -> print(foo)
137 (Pdb) next
138 baz
139 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
140 -> for i in range(5):
141 (Pdb) step
142 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
143 -> print(i)
144 (Pdb) until
145 0
146 1
147 2
148 3
149 4
150 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
151 -> print(bar)
152 (Pdb) next
153 default
154 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
155 -> for i in range(10):
156 (Pdb) jump 8
157 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
158 -> print('after for')
159 (Pdb) return
160 after for
161 ...
162 --Return--
163 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
164 -> return foo.upper()
165 (Pdb) retval
166 'BAZ'
167 (Pdb) continue
168 BAZ
169 """
170
171
172def test_pdb_breakpoint_commands():
173 """Test basic commands related to breakpoints.
174
175 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000176 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000177 ... print(1)
178 ... print(2)
179 ... print(3)
180 ... print(4)
181
182 First, need to clear bdb state that might be left over from previous tests.
183 Otherwise, the new breakpoints might get assigned different numbers.
184
185 >>> from bdb import Breakpoint
186 >>> Breakpoint.next = 1
187 >>> Breakpoint.bplist = {}
188 >>> Breakpoint.bpbynumber = [None]
189
190 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
191 the breakpoint list outputs a tab for the "stop only" and "ignore next"
192 lines, which we don't want to put in here.
193
194 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
195 ... 'break 3',
196 ... 'disable 1',
197 ... 'ignore 1 10',
198 ... 'condition 1 1 < 2',
199 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000200 ... 'break 4',
201 ... 'break',
202 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000203 ... 'break',
204 ... 'condition 1',
205 ... 'enable 1',
206 ... 'clear 1',
207 ... 'commands 2',
R David Murray78d692f2013-10-10 17:23:26 -0400208 ... 'p "42"',
209 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
Georg Brandl0d089622010-07-30 16:00:46 +0000210 ... 'end',
211 ... 'continue', # will stop at breakpoint 2 (line 4)
212 ... 'clear', # clear all!
213 ... 'y',
214 ... 'tbreak 5',
215 ... 'continue', # will stop at temporary breakpoint
216 ... 'break', # make sure breakpoint is gone
217 ... 'continue',
218 ... ]):
219 ... test_function()
220 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
221 -> print(1)
222 (Pdb) break 3
223 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
224 (Pdb) disable 1
225 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
226 (Pdb) ignore 1 10
227 Will ignore next 10 crossings of breakpoint 1.
228 (Pdb) condition 1 1 < 2
229 New condition set for breakpoint 1.
230 (Pdb) break 4
231 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000232 (Pdb) break 4
233 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
234 (Pdb) break
235 Num Type Disp Enb Where
236 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
237 stop only if 1 < 2
238 ignore next 10 hits
239 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
240 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
241 (Pdb) clear 3
242 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000243 (Pdb) break
244 Num Type Disp Enb Where
245 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
246 stop only if 1 < 2
247 ignore next 10 hits
248 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
249 (Pdb) condition 1
250 Breakpoint 1 is now unconditional.
251 (Pdb) enable 1
252 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
253 (Pdb) clear 1
254 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
255 (Pdb) commands 2
R David Murray78d692f2013-10-10 17:23:26 -0400256 (com) p "42"
257 (com) print("42", 7*6)
Georg Brandl0d089622010-07-30 16:00:46 +0000258 (com) end
259 (Pdb) continue
260 1
R David Murray78d692f2013-10-10 17:23:26 -0400261 '42'
262 42 42
Georg Brandl0d089622010-07-30 16:00:46 +0000263 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
264 -> print(2)
265 (Pdb) clear
266 Clear all breaks? y
267 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
268 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000269 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000270 (Pdb) continue
271 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000272 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000273 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
274 -> print(3)
275 (Pdb) break
276 (Pdb) continue
277 3
278 4
279 """
280
281
Georg Brandle59ca2a2010-07-30 17:04:28 +0000282def do_nothing():
283 pass
284
285def do_something():
286 print(42)
287
288def test_list_commands():
289 """Test the list and source commands of pdb.
290
291 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000292 ... import test.test_pdb
293 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000294 ... 'some...'
295 ... 'more...'
296 ... 'code...'
297 ... 'to...'
298 ... 'make...'
299 ... 'a...'
300 ... 'long...'
301 ... 'listing...'
302 ... 'useful...'
303 ... '...'
304 ... '...'
305 ... return foo
306
307 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000308 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000309 ... ret = test_function_2('baz')
310
311 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
312 ... 'list', # list first function
313 ... 'step', # step into second function
314 ... 'list', # list second function
315 ... 'list', # continue listing to EOF
316 ... 'list 1,3', # list specific lines
317 ... 'list x', # invalid argument
318 ... 'next', # step to import
319 ... 'next', # step over import
320 ... 'step', # step into do_nothing
321 ... 'longlist', # list all lines
322 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000323 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000324 ... 'continue',
325 ... ]):
326 ... test_function()
327 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
328 -> ret = test_function_2('baz')
329 (Pdb) list
330 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000331 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000332 3 -> ret = test_function_2('baz')
333 [EOF]
334 (Pdb) step
335 --Call--
336 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
337 -> def test_function_2(foo):
338 (Pdb) list
339 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000340 2 import test.test_pdb
341 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000342 4 'some...'
343 5 'more...'
344 6 'code...'
345 7 'to...'
346 8 'make...'
347 9 'a...'
348 10 'long...'
349 11 'listing...'
350 (Pdb) list
351 12 'useful...'
352 13 '...'
353 14 '...'
354 15 return foo
355 [EOF]
356 (Pdb) list 1,3
357 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000358 2 import test.test_pdb
359 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000360 (Pdb) list x
361 *** ...
362 (Pdb) next
363 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000364 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000365 (Pdb) next
366 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000367 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000368 (Pdb) step
369 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000370 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000371 -> def do_nothing():
372 (Pdb) longlist
373 ... -> def do_nothing():
374 ... pass
375 (Pdb) source do_something
376 ... def do_something():
377 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000378 (Pdb) source fooxxx
379 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000380 (Pdb) continue
381 """
382
383
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000384def test_post_mortem():
385 """Test post mortem traceback debugging.
386
387 >>> def test_function_2():
388 ... try:
389 ... 1/0
390 ... finally:
391 ... print('Exception!')
392
393 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000394 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000395 ... test_function_2()
396 ... print('Not reached.')
397
398 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
399 ... 'next', # step over exception-raising call
400 ... 'bt', # get a backtrace
401 ... 'list', # list code of test_function()
402 ... 'down', # step into test_function_2()
403 ... 'list', # list code of test_function_2()
404 ... 'continue',
405 ... ]):
406 ... try:
407 ... test_function()
408 ... except ZeroDivisionError:
409 ... print('Correctly reraised.')
410 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
411 -> test_function_2()
412 (Pdb) next
413 Exception!
414 ZeroDivisionError: division by zero
415 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
416 -> test_function_2()
417 (Pdb) bt
418 ...
419 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
420 -> test_function()
421 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
422 -> test_function_2()
423 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
424 -> 1/0
425 (Pdb) list
426 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000427 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000428 3 -> test_function_2()
429 4 print('Not reached.')
430 [EOF]
431 (Pdb) down
432 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
433 -> 1/0
434 (Pdb) list
435 1 def test_function_2():
436 2 try:
437 3 >> 1/0
438 4 finally:
439 5 -> print('Exception!')
440 [EOF]
441 (Pdb) continue
442 Correctly reraised.
443 """
444
445
Georg Brandl243ad662009-05-05 09:00:19 +0000446def test_pdb_skip_modules():
447 """This illustrates the simple case of module skipping.
448
449 >>> def skip_module():
450 ... import string
Georg Brandl34748cd2010-12-04 17:11:36 +0000451 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000452 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000453
Georg Brandl9fa2e022009-09-16 16:40:45 +0000454 >>> with PdbTestInput([
455 ... 'step',
456 ... 'continue',
457 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000458 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000459 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
460 -> string.capwords('FOO')
461 (Pdb) step
462 --Return--
463 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
464 -> string.capwords('FOO')
465 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000466 """
Georg Brandl243ad662009-05-05 09:00:19 +0000467
468
469# Module for testing skipping of module that makes a callback
Brett Cannon9529fbf2013-06-15 17:11:25 -0400470mod = types.ModuleType('module_to_skip')
Georg Brandl243ad662009-05-05 09:00:19 +0000471exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
472
473
474def test_pdb_skip_modules_with_callback():
475 """This illustrates skipping of modules that call into other code.
476
477 >>> def skip_module():
478 ... def callback():
479 ... return None
Georg Brandl34748cd2010-12-04 17:11:36 +0000480 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000481 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000482
Georg Brandl9fa2e022009-09-16 16:40:45 +0000483 >>> with PdbTestInput([
484 ... 'step',
485 ... 'step',
486 ... 'step',
487 ... 'step',
488 ... 'step',
489 ... 'continue',
490 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000491 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000492 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000493 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
494 -> mod.foo_pony(callback)
495 (Pdb) step
496 --Call--
497 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
498 -> def callback():
499 (Pdb) step
500 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
501 -> return None
502 (Pdb) step
503 --Return--
504 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
505 -> return None
506 (Pdb) step
507 --Return--
508 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
509 -> mod.foo_pony(callback)
510 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000511 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
512 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000513 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000514 """
Georg Brandl243ad662009-05-05 09:00:19 +0000515
516
Georg Brandl3f940892010-07-30 10:29:19 +0000517def test_pdb_continue_in_bottomframe():
518 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
519
520 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000521 ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
Georg Brandl3f940892010-07-30 10:29:19 +0000522 ... inst.set_trace()
523 ... inst.botframe = sys._getframe() # hackery to get the right botframe
524 ... print(1)
525 ... print(2)
526 ... print(3)
527 ... print(4)
528
Georg Brandl7410dd12010-07-30 12:01:20 +0000529 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000530 ... 'next',
531 ... 'break 7',
532 ... 'continue',
533 ... 'next',
534 ... 'continue',
535 ... 'continue',
536 ... ]):
537 ... test_function()
538 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
539 -> inst.botframe = sys._getframe() # hackery to get the right botframe
540 (Pdb) next
541 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
542 -> print(1)
543 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000544 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000545 (Pdb) continue
546 1
547 2
548 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
549 -> print(3)
550 (Pdb) next
551 3
552 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
553 -> print(4)
554 (Pdb) continue
555 4
556 """
557
558
Georg Brandl46b9afc2010-07-30 09:14:20 +0000559def pdb_invoke(method, arg):
560 """Run pdb.method(arg)."""
Georg Brandl34748cd2010-12-04 17:11:36 +0000561 getattr(pdb.Pdb(nosigint=True), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000562
563
564def test_pdb_run_with_incorrect_argument():
565 """Testing run and runeval with incorrect first argument.
566
567 >>> pti = PdbTestInput(['continue',])
568 >>> with pti:
569 ... pdb_invoke('run', lambda x: x)
570 Traceback (most recent call last):
571 TypeError: exec() arg 1 must be a string, bytes or code object
572
573 >>> with pti:
574 ... pdb_invoke('runeval', lambda x: x)
575 Traceback (most recent call last):
576 TypeError: eval() arg 1 must be a string, bytes or code object
577 """
578
579
580def test_pdb_run_with_code_object():
581 """Testing run and runeval with code object as a first argument.
582
Georg Brandle1e8df12010-07-31 08:14:16 +0000583 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000584 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000585 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000586 (Pdb) step
587 --Return--
588 > <string>(1)<module>()->None
589 (Pdb) x
590 1
591 (Pdb) continue
592
593 >>> with PdbTestInput(['x', 'continue']):
594 ... x=0
595 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
596 > <string>(1)<module>()->None
597 (Pdb) x
598 1
599 (Pdb) continue
600 """
601
Guido van Rossum8820c232013-11-21 11:30:06 -0800602def test_next_until_return_at_return_event():
603 """Test that pdb stops after a next/until/return issued at a return debug event.
604
605 >>> def test_function_2():
606 ... x = 1
607 ... x = 2
608
609 >>> def test_function():
610 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
611 ... test_function_2()
612 ... test_function_2()
613 ... test_function_2()
614 ... end = 1
615
Antoine Pitrouc04d4682014-08-11 21:40:38 -0400616 >>> from bdb import Breakpoint
617 >>> Breakpoint.next = 1
Guido van Rossum8820c232013-11-21 11:30:06 -0800618 >>> with PdbTestInput(['break test_function_2',
619 ... 'continue',
620 ... 'return',
621 ... 'next',
622 ... 'continue',
623 ... 'return',
624 ... 'until',
625 ... 'continue',
626 ... 'return',
627 ... 'return',
628 ... 'continue']):
629 ... test_function()
630 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
631 -> test_function_2()
632 (Pdb) break test_function_2
633 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
634 (Pdb) continue
635 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
636 -> x = 1
637 (Pdb) return
638 --Return--
639 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
640 -> x = 2
641 (Pdb) next
642 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
643 -> test_function_2()
644 (Pdb) continue
645 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
646 -> x = 1
647 (Pdb) return
648 --Return--
649 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
650 -> x = 2
651 (Pdb) until
652 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
653 -> test_function_2()
654 (Pdb) continue
655 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
656 -> x = 1
657 (Pdb) return
658 --Return--
659 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
660 -> x = 2
661 (Pdb) return
662 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
663 -> end = 1
664 (Pdb) continue
665 """
666
667def test_pdb_next_command_for_generator():
668 """Testing skip unwindng stack on yield for generators for "next" command
669
670 >>> def test_gen():
671 ... yield 0
672 ... return 1
673 ... yield 2
674
675 >>> def test_function():
676 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
677 ... it = test_gen()
678 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300679 ... if next(it) != 0:
680 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800681 ... next(it)
682 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300683 ... if ex.value != 1:
684 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800685 ... print("finished")
686
687 >>> with PdbTestInput(['step',
688 ... 'step',
689 ... 'step',
690 ... 'next',
691 ... 'next',
692 ... 'step',
693 ... 'step',
694 ... 'continue']):
695 ... test_function()
696 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
697 -> it = test_gen()
698 (Pdb) step
699 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
700 -> try:
701 (Pdb) step
702 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300703 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800704 (Pdb) step
705 --Call--
706 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
707 -> def test_gen():
708 (Pdb) next
709 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
710 -> yield 0
711 (Pdb) next
712 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
713 -> return 1
714 (Pdb) step
715 --Return--
716 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
717 -> return 1
718 (Pdb) step
719 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300720 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800721 -> next(it)
722 (Pdb) continue
723 finished
724 """
725
726def test_pdb_return_command_for_generator():
727 """Testing no unwindng stack on yield for generators
728 for "return" command
729
730 >>> def test_gen():
731 ... yield 0
732 ... return 1
733 ... yield 2
734
735 >>> def test_function():
736 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
737 ... it = test_gen()
738 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300739 ... if next(it) != 0:
740 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800741 ... next(it)
742 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300743 ... if ex.value != 1:
744 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800745 ... print("finished")
746
747 >>> with PdbTestInput(['step',
748 ... 'step',
749 ... 'step',
750 ... 'return',
751 ... 'step',
752 ... 'step',
753 ... 'continue']):
754 ... test_function()
755 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
756 -> it = test_gen()
757 (Pdb) step
758 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
759 -> try:
760 (Pdb) step
761 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300762 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800763 (Pdb) step
764 --Call--
765 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
766 -> def test_gen():
767 (Pdb) return
768 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300769 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800770 -> next(it)
771 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300772 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800773 -> except StopIteration as ex:
774 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300775 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
776 -> if ex.value != 1:
Guido van Rossum8820c232013-11-21 11:30:06 -0800777 (Pdb) continue
778 finished
779 """
780
781def test_pdb_until_command_for_generator():
782 """Testing no unwindng stack on yield for generators
783 for "until" command if target breakpoing is not reached
784
785 >>> def test_gen():
786 ... yield 0
787 ... yield 1
788 ... yield 2
789
790 >>> def test_function():
791 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
792 ... for i in test_gen():
793 ... print(i)
794 ... print("finished")
795
796 >>> with PdbTestInput(['step',
797 ... 'until 4',
798 ... 'step',
799 ... 'step',
800 ... 'continue']):
801 ... test_function()
802 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
803 -> for i in test_gen():
804 (Pdb) step
805 --Call--
806 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
807 -> def test_gen():
808 (Pdb) until 4
809 0
810 1
811 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
812 -> yield 2
813 (Pdb) step
814 --Return--
815 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
816 -> yield 2
817 (Pdb) step
818 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
819 -> print(i)
820 (Pdb) continue
821 2
822 finished
823 """
824
825def test_pdb_next_command_in_generator_for_loop():
Martin Panter46f50722016-05-26 05:35:26 +0000826 """The next command on returning from a generator controlled by a for loop.
Guido van Rossum8820c232013-11-21 11:30:06 -0800827
828 >>> def test_gen():
829 ... yield 0
830 ... return 1
831
832 >>> def test_function():
833 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
834 ... for i in test_gen():
835 ... print('value', i)
836 ... x = 123
837
838 >>> with PdbTestInput(['break test_gen',
839 ... 'continue',
840 ... 'next',
841 ... 'next',
842 ... 'next',
843 ... 'continue']):
844 ... test_function()
845 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
846 -> for i in test_gen():
847 (Pdb) break test_gen
848 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
849 (Pdb) continue
850 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
851 -> yield 0
852 (Pdb) next
853 value 0
854 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
855 -> return 1
856 (Pdb) next
857 Internal StopIteration: 1
858 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
859 -> for i in test_gen():
860 (Pdb) next
861 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
862 -> x = 123
863 (Pdb) continue
864 """
865
866def test_pdb_next_command_subiterator():
867 """The next command in a generator with a subiterator.
868
869 >>> def test_subgenerator():
870 ... yield 0
871 ... return 1
872
873 >>> def test_gen():
874 ... x = yield from test_subgenerator()
875 ... return x
876
877 >>> def test_function():
878 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
879 ... for i in test_gen():
880 ... print('value', i)
881 ... x = 123
882
883 >>> with PdbTestInput(['step',
884 ... 'step',
885 ... 'next',
886 ... 'next',
887 ... 'next',
888 ... 'continue']):
889 ... test_function()
890 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
891 -> for i in test_gen():
892 (Pdb) step
893 --Call--
894 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
895 -> def test_gen():
896 (Pdb) step
897 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
898 -> x = yield from test_subgenerator()
899 (Pdb) next
900 value 0
901 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
902 -> return x
903 (Pdb) next
904 Internal StopIteration: 1
905 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
906 -> for i in test_gen():
907 (Pdb) next
908 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
909 -> x = 123
910 (Pdb) continue
911 """
912
Georg Brandl46b9afc2010-07-30 09:14:20 +0000913
Georg Brandl6cccb862010-07-30 14:16:43 +0000914class PdbTestCase(unittest.TestCase):
915
Senthil Kumaran42d70812012-05-01 10:07:49 +0800916 def run_pdb(self, script, commands):
917 """Run 'script' lines with pdb and the pdb 'commands'."""
918 filename = 'main.py'
919 with open(filename, 'w') as f:
920 f.write(textwrap.dedent(script))
Georg Brandl4bde9ca2012-05-01 09:21:16 +0200921 self.addCleanup(support.unlink, filename)
Victor Stinner047b7ae2014-10-05 17:37:41 +0200922 self.addCleanup(support.rmtree, '__pycache__')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800923 cmd = [sys.executable, '-m', 'pdb', filename]
924 stdout = stderr = None
925 with subprocess.Popen(cmd, stdout=subprocess.PIPE,
926 stdin=subprocess.PIPE,
927 stderr=subprocess.STDOUT,
928 ) as proc:
929 stdout, stderr = proc.communicate(str.encode(commands))
930 stdout = stdout and bytes.decode(stdout)
931 stderr = stderr and bytes.decode(stderr)
932 return stdout, stderr
933
Georg Brandl6e220552013-10-13 20:51:47 +0200934 def _assert_find_function(self, file_content, func_name, expected):
935 file_content = textwrap.dedent(file_content)
936
937 with open(support.TESTFN, 'w') as f:
938 f.write(file_content)
939
940 expected = None if not expected else (
941 expected[0], support.TESTFN, expected[1])
942 self.assertEqual(
943 expected, pdb.find_function(func_name, support.TESTFN))
944
945 def test_find_function_empty_file(self):
946 self._assert_find_function('', 'foo', None)
947
948 def test_find_function_found(self):
949 self._assert_find_function(
950 """\
951 def foo():
952 pass
953
954 def bar():
955 pass
956
957 def quux():
958 pass
959 """,
960 'bar',
961 ('bar', 4),
962 )
963
Georg Brandl6cccb862010-07-30 14:16:43 +0000964 def test_issue7964(self):
965 # open the file as binary so we can force \r\n newline
966 with open(support.TESTFN, 'wb') as f:
967 f.write(b'print("testing my pdb")\r\n')
968 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
969 proc = subprocess.Popen(cmd,
970 stdout=subprocess.PIPE,
971 stdin=subprocess.PIPE,
972 stderr=subprocess.STDOUT,
973 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000974 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000975 stdout, stderr = proc.communicate(b'quit\n')
976 self.assertNotIn(b'SyntaxError', stdout,
977 "Got a syntax error running test script under PDB")
978
Senthil Kumaran42d70812012-05-01 10:07:49 +0800979 def test_issue13183(self):
980 script = """
981 from bar import bar
982
983 def foo():
984 bar()
985
986 def nope():
987 pass
988
989 def foobar():
990 foo()
991 nope()
992
993 foobar()
994 """
995 commands = """
996 from bar import bar
997 break bar
998 continue
999 step
1000 step
1001 quit
1002 """
1003 bar = """
1004 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +08001005 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +08001006 """
1007 with open('bar.py', 'w') as f:
1008 f.write(textwrap.dedent(bar))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001009 self.addCleanup(support.unlink, 'bar.py')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001010 stdout, stderr = self.run_pdb(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001011 self.assertTrue(
1012 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1013 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001014
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001015 def test_issue13210(self):
1016 # invoking "continue" on a non-main thread triggered an exception
1017 # inside signal.signal
1018
Andrew Svetlov96bc0432012-12-05 15:06:23 +02001019 # raises SkipTest if python was built without threads
1020 support.import_module('threading')
1021
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001022 with open(support.TESTFN, 'wb') as f:
1023 f.write(textwrap.dedent("""
1024 import threading
1025 import pdb
1026
1027 def start_pdb():
1028 pdb.Pdb().set_trace()
1029 x = 1
1030 y = 1
1031
1032 t = threading.Thread(target=start_pdb)
1033 t.start()""").encode('ascii'))
1034 cmd = [sys.executable, '-u', support.TESTFN]
1035 proc = subprocess.Popen(cmd,
1036 stdout=subprocess.PIPE,
1037 stdin=subprocess.PIPE,
1038 stderr=subprocess.STDOUT,
1039 )
1040 self.addCleanup(proc.stdout.close)
1041 stdout, stderr = proc.communicate(b'cont\n')
1042 self.assertNotIn('Error', stdout.decode(),
1043 "Got an error running test script under PDB")
1044
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001045 def test_issue16180(self):
1046 # A syntax error in the debuggee.
1047 script = "def f: pass\n"
1048 commands = ''
1049 expected = "SyntaxError:"
1050 stdout, stderr = self.run_pdb(script, commands)
1051 self.assertIn(expected, stdout,
1052 '\n\nExpected:\n{}\nGot:\n{}\n'
1053 'Fail to handle a syntax error in the debuggee.'
1054 .format(expected, stdout))
1055
1056
Georg Brandl6cccb862010-07-30 14:16:43 +00001057 def tearDown(self):
1058 support.unlink(support.TESTFN)
1059
1060
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001061def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +00001062 from test import test_pdb
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001063 suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
1064 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +00001065
1066
1067if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001068 unittest.main()