blob: 90bf6425cbfa33772c16ea7809cf4efbae2107c7 [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 Brandl6cccb862010-07-30 14:16:43 +00004import pdb
Georg Brandl243ad662009-05-05 09:00:19 +00005import sys
Georg Brandl6cccb862010-07-30 14:16:43 +00006import unittest
7import subprocess
Georg Brandl243ad662009-05-05 09:00:19 +00008
9from test import support
10# This little helper class is essential for testing pdb under doctest.
11from test.test_doctest import _FakeInput
12
13
Georg Brandl9fa2e022009-09-16 16:40:45 +000014class PdbTestInput(object):
15 """Context manager that makes testing Pdb in doctests easier."""
16
17 def __init__(self, input):
18 self.input = input
19
20 def __enter__(self):
21 self.real_stdin = sys.stdin
22 sys.stdin = _FakeInput(self.input)
23
24 def __exit__(self, *exc):
25 sys.stdin = self.real_stdin
26
27
28def test_pdb_displayhook():
29 """This tests the custom displayhook for pdb.
30
31 >>> def test_function(foo, bar):
32 ... import pdb; pdb.Pdb().set_trace()
33 ... pass
34
35 >>> with PdbTestInput([
36 ... 'foo',
37 ... 'bar',
38 ... 'for i in range(5): print(i)',
39 ... 'continue',
40 ... ]):
41 ... test_function(1, None)
42 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
43 -> pass
44 (Pdb) foo
45 1
46 (Pdb) bar
47 (Pdb) for i in range(5): print(i)
48 0
49 1
50 2
51 3
52 4
53 (Pdb) continue
54 """
55
56
Georg Brandl0d089622010-07-30 16:00:46 +000057def test_pdb_basic_commands():
58 """Test the basic commands of pdb.
59
60 >>> def test_function_2(foo, bar='default'):
61 ... print(foo)
62 ... for i in range(5):
63 ... print(i)
64 ... print(bar)
65 ... for i in range(10):
66 ... never_executed
67 ... print('after for')
68 ... print('...')
69 ... return foo.upper()
70
71 >>> def test_function():
72 ... import pdb; pdb.Pdb().set_trace()
73 ... ret = test_function_2('baz')
74 ... print(ret)
75
76 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
77 ... 'step', # entering the function call
78 ... 'args', # display function args
79 ... 'list', # list function source
80 ... 'bt', # display backtrace
81 ... 'up', # step up to test_function()
82 ... 'down', # step down to test_function_2() again
83 ... 'next', # stepping to print(foo)
84 ... 'next', # stepping to the for loop
85 ... 'step', # stepping into the for loop
86 ... 'until', # continuing until out of the for loop
87 ... 'next', # executing the print(bar)
88 ... 'jump 8', # jump over second for loop
89 ... 'return', # return out of function
90 ... 'retval', # display return value
91 ... 'continue',
92 ... ]):
93 ... test_function()
94 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
95 -> ret = test_function_2('baz')
96 (Pdb) step
97 --Call--
98 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
99 -> def test_function_2(foo, bar='default'):
100 (Pdb) args
101 foo = 'baz'
102 bar = 'default'
103 (Pdb) list
104 1 -> def test_function_2(foo, bar='default'):
105 2 print(foo)
106 3 for i in range(5):
107 4 print(i)
108 5 print(bar)
109 6 for i in range(10):
110 7 never_executed
111 8 print('after for')
112 9 print('...')
113 10 return foo.upper()
114 [EOF]
115 (Pdb) bt
116 ...
117 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
118 -> test_function()
119 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
120 -> ret = test_function_2('baz')
121 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
122 -> def test_function_2(foo, bar='default'):
123 (Pdb) up
124 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
125 -> ret = test_function_2('baz')
126 (Pdb) down
127 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
128 -> def test_function_2(foo, bar='default'):
129 (Pdb) next
130 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
131 -> print(foo)
132 (Pdb) next
133 baz
134 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
135 -> for i in range(5):
136 (Pdb) step
137 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
138 -> print(i)
139 (Pdb) until
140 0
141 1
142 2
143 3
144 4
145 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
146 -> print(bar)
147 (Pdb) next
148 default
149 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
150 -> for i in range(10):
151 (Pdb) jump 8
152 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
153 -> print('after for')
154 (Pdb) return
155 after for
156 ...
157 --Return--
158 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
159 -> return foo.upper()
160 (Pdb) retval
161 'BAZ'
162 (Pdb) continue
163 BAZ
164 """
165
166
167def test_pdb_breakpoint_commands():
168 """Test basic commands related to 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 (line 4)
203 ... 'clear', # clear all!
204 ... 'y',
205 ... 'tbreak 5',
206 ... 'continue', # will stop at temporary breakpoint
207 ... 'break', # make sure breakpoint is gone
208 ... 'continue',
209 ... ]):
210 ... test_function()
211 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
212 -> print(1)
213 (Pdb) break 3
214 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
215 (Pdb) disable 1
216 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
217 (Pdb) ignore 1 10
218 Will ignore next 10 crossings of breakpoint 1.
219 (Pdb) condition 1 1 < 2
220 New condition set for breakpoint 1.
221 (Pdb) break 4
222 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
223 (Pdb) break
224 Num Type Disp Enb Where
225 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
226 stop only if 1 < 2
227 ignore next 10 hits
228 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
229 (Pdb) condition 1
230 Breakpoint 1 is now unconditional.
231 (Pdb) enable 1
232 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
233 (Pdb) clear 1
234 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
235 (Pdb) commands 2
236 (com) print 42
237 (com) end
238 (Pdb) continue
239 1
240 42
241 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
242 -> print(2)
243 (Pdb) clear
244 Clear all breaks? y
245 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
246 (Pdb) tbreak 5
247 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
248 (Pdb) continue
249 2
250 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
251 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
252 -> print(3)
253 (Pdb) break
254 (Pdb) continue
255 3
256 4
257 """
258
259
Georg Brandle59ca2a2010-07-30 17:04:28 +0000260def do_nothing():
261 pass
262
263def do_something():
264 print(42)
265
266def test_list_commands():
267 """Test the list and source commands of pdb.
268
269 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000270 ... import test.test_pdb
271 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000272 ... 'some...'
273 ... 'more...'
274 ... 'code...'
275 ... 'to...'
276 ... 'make...'
277 ... 'a...'
278 ... 'long...'
279 ... 'listing...'
280 ... 'useful...'
281 ... '...'
282 ... '...'
283 ... return foo
284
285 >>> def test_function():
286 ... import pdb; pdb.Pdb().set_trace()
287 ... ret = test_function_2('baz')
288
289 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
290 ... 'list', # list first function
291 ... 'step', # step into second function
292 ... 'list', # list second function
293 ... 'list', # continue listing to EOF
294 ... 'list 1,3', # list specific lines
295 ... 'list x', # invalid argument
296 ... 'next', # step to import
297 ... 'next', # step over import
298 ... 'step', # step into do_nothing
299 ... 'longlist', # list all lines
300 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000301 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000302 ... 'continue',
303 ... ]):
304 ... test_function()
305 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
306 -> ret = test_function_2('baz')
307 (Pdb) list
308 1 def test_function():
309 2 import pdb; pdb.Pdb().set_trace()
310 3 -> ret = test_function_2('baz')
311 [EOF]
312 (Pdb) step
313 --Call--
314 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
315 -> def test_function_2(foo):
316 (Pdb) list
317 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000318 2 import test.test_pdb
319 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000320 4 'some...'
321 5 'more...'
322 6 'code...'
323 7 'to...'
324 8 'make...'
325 9 'a...'
326 10 'long...'
327 11 'listing...'
328 (Pdb) list
329 12 'useful...'
330 13 '...'
331 14 '...'
332 15 return foo
333 [EOF]
334 (Pdb) list 1,3
335 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000336 2 import test.test_pdb
337 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000338 (Pdb) list x
339 *** ...
340 (Pdb) next
341 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000342 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000343 (Pdb) next
344 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000345 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000346 (Pdb) step
347 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000348 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000349 -> def do_nothing():
350 (Pdb) longlist
351 ... -> def do_nothing():
352 ... pass
353 (Pdb) source do_something
354 ... def do_something():
355 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000356 (Pdb) source fooxxx
357 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000358 (Pdb) continue
359 """
360
361
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000362def test_post_mortem():
363 """Test post mortem traceback debugging.
364
365 >>> def test_function_2():
366 ... try:
367 ... 1/0
368 ... finally:
369 ... print('Exception!')
370
371 >>> def test_function():
372 ... import pdb; pdb.Pdb().set_trace()
373 ... test_function_2()
374 ... print('Not reached.')
375
376 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
377 ... 'next', # step over exception-raising call
378 ... 'bt', # get a backtrace
379 ... 'list', # list code of test_function()
380 ... 'down', # step into test_function_2()
381 ... 'list', # list code of test_function_2()
382 ... 'continue',
383 ... ]):
384 ... try:
385 ... test_function()
386 ... except ZeroDivisionError:
387 ... print('Correctly reraised.')
388 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
389 -> test_function_2()
390 (Pdb) next
391 Exception!
392 ZeroDivisionError: division by zero
393 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
394 -> test_function_2()
395 (Pdb) bt
396 ...
397 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
398 -> test_function()
399 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
400 -> test_function_2()
401 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
402 -> 1/0
403 (Pdb) list
404 1 def test_function():
405 2 import pdb; pdb.Pdb().set_trace()
406 3 -> test_function_2()
407 4 print('Not reached.')
408 [EOF]
409 (Pdb) down
410 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
411 -> 1/0
412 (Pdb) list
413 1 def test_function_2():
414 2 try:
415 3 >> 1/0
416 4 finally:
417 5 -> print('Exception!')
418 [EOF]
419 (Pdb) continue
420 Correctly reraised.
421 """
422
423
Georg Brandl243ad662009-05-05 09:00:19 +0000424def test_pdb_skip_modules():
425 """This illustrates the simple case of module skipping.
426
427 >>> def skip_module():
428 ... import string
429 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
430 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000431
Georg Brandl9fa2e022009-09-16 16:40:45 +0000432 >>> with PdbTestInput([
433 ... 'step',
434 ... 'continue',
435 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000436 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000437 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
438 -> string.capwords('FOO')
439 (Pdb) step
440 --Return--
441 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
442 -> string.capwords('FOO')
443 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000444 """
Georg Brandl243ad662009-05-05 09:00:19 +0000445
446
447# Module for testing skipping of module that makes a callback
448mod = imp.new_module('module_to_skip')
449exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
450
451
452def test_pdb_skip_modules_with_callback():
453 """This illustrates skipping of modules that call into other code.
454
455 >>> def skip_module():
456 ... def callback():
457 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +0000458 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000459 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000460
Georg Brandl9fa2e022009-09-16 16:40:45 +0000461 >>> with PdbTestInput([
462 ... 'step',
463 ... 'step',
464 ... 'step',
465 ... 'step',
466 ... 'step',
467 ... 'continue',
468 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000469 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000470 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000471 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
472 -> mod.foo_pony(callback)
473 (Pdb) step
474 --Call--
475 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
476 -> def callback():
477 (Pdb) step
478 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
479 -> return None
480 (Pdb) step
481 --Return--
482 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
483 -> return None
484 (Pdb) step
485 --Return--
486 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
487 -> mod.foo_pony(callback)
488 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000489 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
490 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000491 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000492 """
Georg Brandl243ad662009-05-05 09:00:19 +0000493
494
Georg Brandl3f940892010-07-30 10:29:19 +0000495def test_pdb_continue_in_bottomframe():
496 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
497
498 >>> def test_function():
499 ... import pdb, sys; inst = pdb.Pdb()
500 ... inst.set_trace()
501 ... inst.botframe = sys._getframe() # hackery to get the right botframe
502 ... print(1)
503 ... print(2)
504 ... print(3)
505 ... print(4)
506
Georg Brandl7410dd12010-07-30 12:01:20 +0000507 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000508 ... 'next',
509 ... 'break 7',
510 ... 'continue',
511 ... 'next',
512 ... 'continue',
513 ... 'continue',
514 ... ]):
515 ... test_function()
516 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
517 -> inst.botframe = sys._getframe() # hackery to get the right botframe
518 (Pdb) next
519 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
520 -> print(1)
521 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000522 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000523 (Pdb) continue
524 1
525 2
526 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
527 -> print(3)
528 (Pdb) next
529 3
530 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
531 -> print(4)
532 (Pdb) continue
533 4
534 """
535
536
Georg Brandl46b9afc2010-07-30 09:14:20 +0000537def pdb_invoke(method, arg):
538 """Run pdb.method(arg)."""
539 import pdb; getattr(pdb, method)(arg)
540
541
542def test_pdb_run_with_incorrect_argument():
543 """Testing run and runeval with incorrect first argument.
544
545 >>> pti = PdbTestInput(['continue',])
546 >>> with pti:
547 ... pdb_invoke('run', lambda x: x)
548 Traceback (most recent call last):
549 TypeError: exec() arg 1 must be a string, bytes or code object
550
551 >>> with pti:
552 ... pdb_invoke('runeval', lambda x: x)
553 Traceback (most recent call last):
554 TypeError: eval() arg 1 must be a string, bytes or code object
555 """
556
557
558def test_pdb_run_with_code_object():
559 """Testing run and runeval with code object as a first argument.
560
Georg Brandle1e8df12010-07-31 08:14:16 +0000561 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000562 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000563 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000564 (Pdb) step
565 --Return--
566 > <string>(1)<module>()->None
567 (Pdb) x
568 1
569 (Pdb) continue
570
571 >>> with PdbTestInput(['x', 'continue']):
572 ... x=0
573 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
574 > <string>(1)<module>()->None
575 (Pdb) x
576 1
577 (Pdb) continue
578 """
579
580
Georg Brandl6cccb862010-07-30 14:16:43 +0000581class PdbTestCase(unittest.TestCase):
582
583 def test_issue7964(self):
584 # open the file as binary so we can force \r\n newline
585 with open(support.TESTFN, 'wb') as f:
586 f.write(b'print("testing my pdb")\r\n')
587 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
588 proc = subprocess.Popen(cmd,
589 stdout=subprocess.PIPE,
590 stdin=subprocess.PIPE,
591 stderr=subprocess.STDOUT,
592 )
593 stdout, stderr = proc.communicate(b'quit\n')
594 self.assertNotIn(b'SyntaxError', stdout,
595 "Got a syntax error running test script under PDB")
596
597 def tearDown(self):
598 support.unlink(support.TESTFN)
599
600
Georg Brandl243ad662009-05-05 09:00:19 +0000601def test_main():
602 from test import test_pdb
603 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000604 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000605
606
607if __name__ == '__main__':
608 test_main()