blob: 0861e1edb332804519a251850eb5b27f44c07822 [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):
270 ... import test_pdb
271 ... test_pdb.do_nothing()
272 ... '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):
318 2 import test_pdb
319 3 test_pdb.do_nothing()
320 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):
336 2 import test_pdb
337 3 test_pdb.do_nothing()
338 (Pdb) list x
339 *** ...
340 (Pdb) next
341 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
342 -> import test_pdb
343 (Pdb) next
344 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
345 -> test_pdb.do_nothing()
346 (Pdb) step
347 --Call--
348 > /home/gbr/devel/python/Lib/test/test_pdb.py(260)do_nothing()
349 -> 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 Brandl243ad662009-05-05 09:00:19 +0000362def test_pdb_skip_modules():
363 """This illustrates the simple case of module skipping.
364
365 >>> def skip_module():
366 ... import string
367 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
368 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000369
Georg Brandl9fa2e022009-09-16 16:40:45 +0000370 >>> with PdbTestInput([
371 ... 'step',
372 ... 'continue',
373 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000374 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000375 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
376 -> string.capwords('FOO')
377 (Pdb) step
378 --Return--
379 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
380 -> string.capwords('FOO')
381 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000382 """
Georg Brandl243ad662009-05-05 09:00:19 +0000383
384
385# Module for testing skipping of module that makes a callback
386mod = imp.new_module('module_to_skip')
387exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
388
389
390def test_pdb_skip_modules_with_callback():
391 """This illustrates skipping of modules that call into other code.
392
393 >>> def skip_module():
394 ... def callback():
395 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +0000396 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000397 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000398
Georg Brandl9fa2e022009-09-16 16:40:45 +0000399 >>> with PdbTestInput([
400 ... 'step',
401 ... 'step',
402 ... 'step',
403 ... 'step',
404 ... 'step',
405 ... 'continue',
406 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000407 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000408 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000409 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
410 -> mod.foo_pony(callback)
411 (Pdb) step
412 --Call--
413 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
414 -> def callback():
415 (Pdb) step
416 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
417 -> return None
418 (Pdb) step
419 --Return--
420 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
421 -> return None
422 (Pdb) step
423 --Return--
424 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
425 -> mod.foo_pony(callback)
426 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000427 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
428 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000429 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000430 """
Georg Brandl243ad662009-05-05 09:00:19 +0000431
432
Georg Brandl3f940892010-07-30 10:29:19 +0000433def test_pdb_continue_in_bottomframe():
434 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
435
436 >>> def test_function():
437 ... import pdb, sys; inst = pdb.Pdb()
438 ... inst.set_trace()
439 ... inst.botframe = sys._getframe() # hackery to get the right botframe
440 ... print(1)
441 ... print(2)
442 ... print(3)
443 ... print(4)
444
Georg Brandl7410dd12010-07-30 12:01:20 +0000445 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000446 ... 'next',
447 ... 'break 7',
448 ... 'continue',
449 ... 'next',
450 ... 'continue',
451 ... 'continue',
452 ... ]):
453 ... test_function()
454 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
455 -> inst.botframe = sys._getframe() # hackery to get the right botframe
456 (Pdb) next
457 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
458 -> print(1)
459 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000460 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000461 (Pdb) continue
462 1
463 2
464 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
465 -> print(3)
466 (Pdb) next
467 3
468 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
469 -> print(4)
470 (Pdb) continue
471 4
472 """
473
474
Georg Brandl46b9afc2010-07-30 09:14:20 +0000475def pdb_invoke(method, arg):
476 """Run pdb.method(arg)."""
477 import pdb; getattr(pdb, method)(arg)
478
479
480def test_pdb_run_with_incorrect_argument():
481 """Testing run and runeval with incorrect first argument.
482
483 >>> pti = PdbTestInput(['continue',])
484 >>> with pti:
485 ... pdb_invoke('run', lambda x: x)
486 Traceback (most recent call last):
487 TypeError: exec() arg 1 must be a string, bytes or code object
488
489 >>> with pti:
490 ... pdb_invoke('runeval', lambda x: x)
491 Traceback (most recent call last):
492 TypeError: eval() arg 1 must be a string, bytes or code object
493 """
494
495
496def test_pdb_run_with_code_object():
497 """Testing run and runeval with code object as a first argument.
498
499 >>> with PdbTestInput(['step','x', 'continue']):
500 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
501 > <string>(1)<module>()
502 (Pdb) step
503 --Return--
504 > <string>(1)<module>()->None
505 (Pdb) x
506 1
507 (Pdb) continue
508
509 >>> with PdbTestInput(['x', 'continue']):
510 ... x=0
511 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
512 > <string>(1)<module>()->None
513 (Pdb) x
514 1
515 (Pdb) continue
516 """
517
518
Georg Brandl6cccb862010-07-30 14:16:43 +0000519class PdbTestCase(unittest.TestCase):
520
521 def test_issue7964(self):
522 # open the file as binary so we can force \r\n newline
523 with open(support.TESTFN, 'wb') as f:
524 f.write(b'print("testing my pdb")\r\n')
525 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
526 proc = subprocess.Popen(cmd,
527 stdout=subprocess.PIPE,
528 stdin=subprocess.PIPE,
529 stderr=subprocess.STDOUT,
530 )
531 stdout, stderr = proc.communicate(b'quit\n')
532 self.assertNotIn(b'SyntaxError', stdout,
533 "Got a syntax error running test script under PDB")
534
535 def tearDown(self):
536 support.unlink(support.TESTFN)
537
538
Georg Brandl243ad662009-05-05 09:00:19 +0000539def test_main():
540 from test import test_pdb
541 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000542 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000543
544
545if __name__ == '__main__':
546 test_main()