blob: b2a441db7488f73f813bd0b04aa5826d604d4eb0 [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
301 ... 'continue',
302 ... ]):
303 ... test_function()
304 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
305 -> ret = test_function_2('baz')
306 (Pdb) list
307 1 def test_function():
308 2 import pdb; pdb.Pdb().set_trace()
309 3 -> ret = test_function_2('baz')
310 [EOF]
311 (Pdb) step
312 --Call--
313 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
314 -> def test_function_2(foo):
315 (Pdb) list
316 1 -> def test_function_2(foo):
317 2 import test_pdb
318 3 test_pdb.do_nothing()
319 4 'some...'
320 5 'more...'
321 6 'code...'
322 7 'to...'
323 8 'make...'
324 9 'a...'
325 10 'long...'
326 11 'listing...'
327 (Pdb) list
328 12 'useful...'
329 13 '...'
330 14 '...'
331 15 return foo
332 [EOF]
333 (Pdb) list 1,3
334 1 -> def test_function_2(foo):
335 2 import test_pdb
336 3 test_pdb.do_nothing()
337 (Pdb) list x
338 *** ...
339 (Pdb) next
340 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
341 -> import test_pdb
342 (Pdb) next
343 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
344 -> test_pdb.do_nothing()
345 (Pdb) step
346 --Call--
347 > /home/gbr/devel/python/Lib/test/test_pdb.py(260)do_nothing()
348 -> def do_nothing():
349 (Pdb) longlist
350 ... -> def do_nothing():
351 ... pass
352 (Pdb) source do_something
353 ... def do_something():
354 ... print(42)
355 (Pdb) continue
356 """
357
358
Georg Brandl243ad662009-05-05 09:00:19 +0000359def test_pdb_skip_modules():
360 """This illustrates the simple case of module skipping.
361
362 >>> def skip_module():
363 ... import string
364 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
365 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000366
Georg Brandl9fa2e022009-09-16 16:40:45 +0000367 >>> with PdbTestInput([
368 ... 'step',
369 ... 'continue',
370 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000371 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000372 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
373 -> string.capwords('FOO')
374 (Pdb) step
375 --Return--
376 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
377 -> string.capwords('FOO')
378 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000379 """
Georg Brandl243ad662009-05-05 09:00:19 +0000380
381
382# Module for testing skipping of module that makes a callback
383mod = imp.new_module('module_to_skip')
384exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
385
386
387def test_pdb_skip_modules_with_callback():
388 """This illustrates skipping of modules that call into other code.
389
390 >>> def skip_module():
391 ... def callback():
392 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +0000393 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000394 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000395
Georg Brandl9fa2e022009-09-16 16:40:45 +0000396 >>> with PdbTestInput([
397 ... 'step',
398 ... 'step',
399 ... 'step',
400 ... 'step',
401 ... 'step',
402 ... 'continue',
403 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000404 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000405 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000406 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
407 -> mod.foo_pony(callback)
408 (Pdb) step
409 --Call--
410 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
411 -> def callback():
412 (Pdb) step
413 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
414 -> return None
415 (Pdb) step
416 --Return--
417 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
418 -> return None
419 (Pdb) step
420 --Return--
421 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
422 -> mod.foo_pony(callback)
423 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000424 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
425 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000426 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000427 """
Georg Brandl243ad662009-05-05 09:00:19 +0000428
429
Georg Brandl3f940892010-07-30 10:29:19 +0000430def test_pdb_continue_in_bottomframe():
431 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
432
433 >>> def test_function():
434 ... import pdb, sys; inst = pdb.Pdb()
435 ... inst.set_trace()
436 ... inst.botframe = sys._getframe() # hackery to get the right botframe
437 ... print(1)
438 ... print(2)
439 ... print(3)
440 ... print(4)
441
Georg Brandl7410dd12010-07-30 12:01:20 +0000442 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000443 ... 'next',
444 ... 'break 7',
445 ... 'continue',
446 ... 'next',
447 ... 'continue',
448 ... 'continue',
449 ... ]):
450 ... test_function()
451 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
452 -> inst.botframe = sys._getframe() # hackery to get the right botframe
453 (Pdb) next
454 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
455 -> print(1)
456 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000457 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000458 (Pdb) continue
459 1
460 2
461 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
462 -> print(3)
463 (Pdb) next
464 3
465 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
466 -> print(4)
467 (Pdb) continue
468 4
469 """
470
471
Georg Brandl46b9afc2010-07-30 09:14:20 +0000472def pdb_invoke(method, arg):
473 """Run pdb.method(arg)."""
474 import pdb; getattr(pdb, method)(arg)
475
476
477def test_pdb_run_with_incorrect_argument():
478 """Testing run and runeval with incorrect first argument.
479
480 >>> pti = PdbTestInput(['continue',])
481 >>> with pti:
482 ... pdb_invoke('run', lambda x: x)
483 Traceback (most recent call last):
484 TypeError: exec() arg 1 must be a string, bytes or code object
485
486 >>> with pti:
487 ... pdb_invoke('runeval', lambda x: x)
488 Traceback (most recent call last):
489 TypeError: eval() arg 1 must be a string, bytes or code object
490 """
491
492
493def test_pdb_run_with_code_object():
494 """Testing run and runeval with code object as a first argument.
495
496 >>> with PdbTestInput(['step','x', 'continue']):
497 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
498 > <string>(1)<module>()
499 (Pdb) step
500 --Return--
501 > <string>(1)<module>()->None
502 (Pdb) x
503 1
504 (Pdb) continue
505
506 >>> with PdbTestInput(['x', 'continue']):
507 ... x=0
508 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
509 > <string>(1)<module>()->None
510 (Pdb) x
511 1
512 (Pdb) continue
513 """
514
515
Georg Brandl6cccb862010-07-30 14:16:43 +0000516class PdbTestCase(unittest.TestCase):
517
518 def test_issue7964(self):
519 # open the file as binary so we can force \r\n newline
520 with open(support.TESTFN, 'wb') as f:
521 f.write(b'print("testing my pdb")\r\n')
522 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
523 proc = subprocess.Popen(cmd,
524 stdout=subprocess.PIPE,
525 stdin=subprocess.PIPE,
526 stderr=subprocess.STDOUT,
527 )
528 stdout, stderr = proc.communicate(b'quit\n')
529 self.assertNotIn(b'SyntaxError', stdout,
530 "Got a syntax error running test script under PDB")
531
532 def tearDown(self):
533 support.unlink(support.TESTFN)
534
535
Georg Brandl243ad662009-05-05 09:00:19 +0000536def test_main():
537 from test import test_pdb
538 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000539 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000540
541
542if __name__ == '__main__':
543 test_main()