blob: dc2609e0c56b8b253f81b2299cf0a61c81628e22 [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
Senthil Kumaran42d70812012-05-01 10:07:49 +08008import textwrap
Georg Brandl243ad662009-05-05 09:00:19 +00009
10from test import support
11# This little helper class is essential for testing pdb under doctest.
12from test.test_doctest import _FakeInput
13
14
Georg Brandl9fa2e022009-09-16 16:40:45 +000015class PdbTestInput(object):
16 """Context manager that makes testing Pdb in doctests easier."""
17
18 def __init__(self, input):
19 self.input = input
20
21 def __enter__(self):
22 self.real_stdin = sys.stdin
23 sys.stdin = _FakeInput(self.input)
24
25 def __exit__(self, *exc):
26 sys.stdin = self.real_stdin
27
28
29def test_pdb_displayhook():
30 """This tests the custom displayhook for pdb.
31
32 >>> def test_function(foo, bar):
Georg Brandl34748cd2010-12-04 17:11:36 +000033 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000034 ... pass
35
36 >>> with PdbTestInput([
37 ... 'foo',
38 ... 'bar',
39 ... 'for i in range(5): print(i)',
40 ... 'continue',
41 ... ]):
42 ... test_function(1, None)
43 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
44 -> pass
45 (Pdb) foo
46 1
47 (Pdb) bar
48 (Pdb) for i in range(5): print(i)
49 0
50 1
51 2
52 3
53 4
54 (Pdb) continue
55 """
56
57
Georg Brandl0d089622010-07-30 16:00:46 +000058def test_pdb_basic_commands():
59 """Test the basic commands of pdb.
60
61 >>> def test_function_2(foo, bar='default'):
62 ... print(foo)
63 ... for i in range(5):
64 ... print(i)
65 ... print(bar)
66 ... for i in range(10):
67 ... never_executed
68 ... print('after for')
69 ... print('...')
70 ... return foo.upper()
71
72 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +000073 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000074 ... ret = test_function_2('baz')
75 ... print(ret)
76
77 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
78 ... 'step', # entering the function call
79 ... 'args', # display function args
80 ... 'list', # list function source
81 ... 'bt', # display backtrace
82 ... 'up', # step up to test_function()
83 ... 'down', # step down to test_function_2() again
84 ... 'next', # stepping to print(foo)
85 ... 'next', # stepping to the for loop
86 ... 'step', # stepping into the for loop
87 ... 'until', # continuing until out of the for loop
88 ... 'next', # executing the print(bar)
89 ... 'jump 8', # jump over second for loop
90 ... 'return', # return out of function
91 ... 'retval', # display return value
92 ... 'continue',
93 ... ]):
94 ... test_function()
95 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
96 -> ret = test_function_2('baz')
97 (Pdb) step
98 --Call--
99 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
100 -> def test_function_2(foo, bar='default'):
101 (Pdb) args
102 foo = 'baz'
103 bar = 'default'
104 (Pdb) list
105 1 -> def test_function_2(foo, bar='default'):
106 2 print(foo)
107 3 for i in range(5):
108 4 print(i)
109 5 print(bar)
110 6 for i in range(10):
111 7 never_executed
112 8 print('after for')
113 9 print('...')
114 10 return foo.upper()
115 [EOF]
116 (Pdb) bt
117 ...
118 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
119 -> test_function()
120 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
121 -> ret = test_function_2('baz')
122 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
123 -> def test_function_2(foo, bar='default'):
124 (Pdb) up
125 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
126 -> ret = test_function_2('baz')
127 (Pdb) down
128 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
129 -> def test_function_2(foo, bar='default'):
130 (Pdb) next
131 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
132 -> print(foo)
133 (Pdb) next
134 baz
135 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
136 -> for i in range(5):
137 (Pdb) step
138 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
139 -> print(i)
140 (Pdb) until
141 0
142 1
143 2
144 3
145 4
146 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
147 -> print(bar)
148 (Pdb) next
149 default
150 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
151 -> for i in range(10):
152 (Pdb) jump 8
153 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
154 -> print('after for')
155 (Pdb) return
156 after for
157 ...
158 --Return--
159 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
160 -> return foo.upper()
161 (Pdb) retval
162 'BAZ'
163 (Pdb) continue
164 BAZ
165 """
166
167
168def test_pdb_breakpoint_commands():
169 """Test basic commands related to breakpoints.
170
171 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000172 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000173 ... print(1)
174 ... print(2)
175 ... print(3)
176 ... print(4)
177
178 First, need to clear bdb state that might be left over from previous tests.
179 Otherwise, the new breakpoints might get assigned different numbers.
180
181 >>> from bdb import Breakpoint
182 >>> Breakpoint.next = 1
183 >>> Breakpoint.bplist = {}
184 >>> Breakpoint.bpbynumber = [None]
185
186 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
187 the breakpoint list outputs a tab for the "stop only" and "ignore next"
188 lines, which we don't want to put in here.
189
190 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
191 ... 'break 3',
192 ... 'disable 1',
193 ... 'ignore 1 10',
194 ... 'condition 1 1 < 2',
195 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000196 ... 'break 4',
197 ... 'break',
198 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000199 ... 'break',
200 ... 'condition 1',
201 ... 'enable 1',
202 ... 'clear 1',
203 ... 'commands 2',
204 ... 'print 42',
205 ... 'end',
206 ... 'continue', # will stop at breakpoint 2 (line 4)
207 ... 'clear', # clear all!
208 ... 'y',
209 ... 'tbreak 5',
210 ... 'continue', # will stop at temporary breakpoint
211 ... 'break', # make sure breakpoint is gone
212 ... 'continue',
213 ... ]):
214 ... test_function()
215 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
216 -> print(1)
217 (Pdb) break 3
218 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
219 (Pdb) disable 1
220 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
221 (Pdb) ignore 1 10
222 Will ignore next 10 crossings of breakpoint 1.
223 (Pdb) condition 1 1 < 2
224 New condition set for breakpoint 1.
225 (Pdb) break 4
226 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000227 (Pdb) break 4
228 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
229 (Pdb) break
230 Num Type Disp Enb Where
231 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
232 stop only if 1 < 2
233 ignore next 10 hits
234 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
235 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
236 (Pdb) clear 3
237 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000238 (Pdb) break
239 Num Type Disp Enb Where
240 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
241 stop only if 1 < 2
242 ignore next 10 hits
243 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
244 (Pdb) condition 1
245 Breakpoint 1 is now unconditional.
246 (Pdb) enable 1
247 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
248 (Pdb) clear 1
249 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
250 (Pdb) commands 2
251 (com) print 42
252 (com) end
253 (Pdb) continue
254 1
255 42
256 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
257 -> print(2)
258 (Pdb) clear
259 Clear all breaks? y
260 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
261 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000262 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000263 (Pdb) continue
264 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000265 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000266 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
267 -> print(3)
268 (Pdb) break
269 (Pdb) continue
270 3
271 4
272 """
273
274
Georg Brandle59ca2a2010-07-30 17:04:28 +0000275def do_nothing():
276 pass
277
278def do_something():
279 print(42)
280
281def test_list_commands():
282 """Test the list and source commands of pdb.
283
284 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000285 ... import test.test_pdb
286 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000287 ... 'some...'
288 ... 'more...'
289 ... 'code...'
290 ... 'to...'
291 ... 'make...'
292 ... 'a...'
293 ... 'long...'
294 ... 'listing...'
295 ... 'useful...'
296 ... '...'
297 ... '...'
298 ... return foo
299
300 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000301 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000302 ... ret = test_function_2('baz')
303
304 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
305 ... 'list', # list first function
306 ... 'step', # step into second function
307 ... 'list', # list second function
308 ... 'list', # continue listing to EOF
309 ... 'list 1,3', # list specific lines
310 ... 'list x', # invalid argument
311 ... 'next', # step to import
312 ... 'next', # step over import
313 ... 'step', # step into do_nothing
314 ... 'longlist', # list all lines
315 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000316 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000317 ... 'continue',
318 ... ]):
319 ... test_function()
320 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
321 -> ret = test_function_2('baz')
322 (Pdb) list
323 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000324 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000325 3 -> ret = test_function_2('baz')
326 [EOF]
327 (Pdb) step
328 --Call--
329 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
330 -> def test_function_2(foo):
331 (Pdb) list
332 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000333 2 import test.test_pdb
334 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000335 4 'some...'
336 5 'more...'
337 6 'code...'
338 7 'to...'
339 8 'make...'
340 9 'a...'
341 10 'long...'
342 11 'listing...'
343 (Pdb) list
344 12 'useful...'
345 13 '...'
346 14 '...'
347 15 return foo
348 [EOF]
349 (Pdb) list 1,3
350 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000351 2 import test.test_pdb
352 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000353 (Pdb) list x
354 *** ...
355 (Pdb) next
356 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000357 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000358 (Pdb) next
359 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000360 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000361 (Pdb) step
362 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000363 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000364 -> def do_nothing():
365 (Pdb) longlist
366 ... -> def do_nothing():
367 ... pass
368 (Pdb) source do_something
369 ... def do_something():
370 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000371 (Pdb) source fooxxx
372 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000373 (Pdb) continue
374 """
375
376
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000377def test_post_mortem():
378 """Test post mortem traceback debugging.
379
380 >>> def test_function_2():
381 ... try:
382 ... 1/0
383 ... finally:
384 ... print('Exception!')
385
386 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000387 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000388 ... test_function_2()
389 ... print('Not reached.')
390
391 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
392 ... 'next', # step over exception-raising call
393 ... 'bt', # get a backtrace
394 ... 'list', # list code of test_function()
395 ... 'down', # step into test_function_2()
396 ... 'list', # list code of test_function_2()
397 ... 'continue',
398 ... ]):
399 ... try:
400 ... test_function()
401 ... except ZeroDivisionError:
402 ... print('Correctly reraised.')
403 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
404 -> test_function_2()
405 (Pdb) next
406 Exception!
407 ZeroDivisionError: division by zero
408 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
409 -> test_function_2()
410 (Pdb) bt
411 ...
412 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
413 -> test_function()
414 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
415 -> test_function_2()
416 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
417 -> 1/0
418 (Pdb) list
419 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000420 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000421 3 -> test_function_2()
422 4 print('Not reached.')
423 [EOF]
424 (Pdb) down
425 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
426 -> 1/0
427 (Pdb) list
428 1 def test_function_2():
429 2 try:
430 3 >> 1/0
431 4 finally:
432 5 -> print('Exception!')
433 [EOF]
434 (Pdb) continue
435 Correctly reraised.
436 """
437
438
Georg Brandl243ad662009-05-05 09:00:19 +0000439def test_pdb_skip_modules():
440 """This illustrates the simple case of module skipping.
441
442 >>> def skip_module():
443 ... import string
Georg Brandl34748cd2010-12-04 17:11:36 +0000444 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000445 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000446
Georg Brandl9fa2e022009-09-16 16:40:45 +0000447 >>> with PdbTestInput([
448 ... 'step',
449 ... 'continue',
450 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000451 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000452 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
453 -> string.capwords('FOO')
454 (Pdb) step
455 --Return--
456 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
457 -> string.capwords('FOO')
458 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000459 """
Georg Brandl243ad662009-05-05 09:00:19 +0000460
461
462# Module for testing skipping of module that makes a callback
463mod = imp.new_module('module_to_skip')
464exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
465
466
467def test_pdb_skip_modules_with_callback():
468 """This illustrates skipping of modules that call into other code.
469
470 >>> def skip_module():
471 ... def callback():
472 ... return None
Georg Brandl34748cd2010-12-04 17:11:36 +0000473 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000474 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000475
Georg Brandl9fa2e022009-09-16 16:40:45 +0000476 >>> with PdbTestInput([
477 ... 'step',
478 ... 'step',
479 ... 'step',
480 ... 'step',
481 ... 'step',
482 ... 'continue',
483 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000484 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000485 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000486 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
487 -> mod.foo_pony(callback)
488 (Pdb) step
489 --Call--
490 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
491 -> def callback():
492 (Pdb) step
493 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
494 -> return None
495 (Pdb) step
496 --Return--
497 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
498 -> return None
499 (Pdb) step
500 --Return--
501 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
502 -> mod.foo_pony(callback)
503 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000504 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
505 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000506 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000507 """
Georg Brandl243ad662009-05-05 09:00:19 +0000508
509
Georg Brandl3f940892010-07-30 10:29:19 +0000510def test_pdb_continue_in_bottomframe():
511 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
512
513 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000514 ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
Georg Brandl3f940892010-07-30 10:29:19 +0000515 ... inst.set_trace()
516 ... inst.botframe = sys._getframe() # hackery to get the right botframe
517 ... print(1)
518 ... print(2)
519 ... print(3)
520 ... print(4)
521
Georg Brandl7410dd12010-07-30 12:01:20 +0000522 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000523 ... 'next',
524 ... 'break 7',
525 ... 'continue',
526 ... 'next',
527 ... 'continue',
528 ... 'continue',
529 ... ]):
530 ... test_function()
531 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
532 -> inst.botframe = sys._getframe() # hackery to get the right botframe
533 (Pdb) next
534 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
535 -> print(1)
536 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000537 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000538 (Pdb) continue
539 1
540 2
541 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
542 -> print(3)
543 (Pdb) next
544 3
545 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
546 -> print(4)
547 (Pdb) continue
548 4
549 """
550
551
Georg Brandl46b9afc2010-07-30 09:14:20 +0000552def pdb_invoke(method, arg):
553 """Run pdb.method(arg)."""
Georg Brandl34748cd2010-12-04 17:11:36 +0000554 import pdb
555 getattr(pdb.Pdb(nosigint=True), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000556
557
558def test_pdb_run_with_incorrect_argument():
559 """Testing run and runeval with incorrect first argument.
560
561 >>> pti = PdbTestInput(['continue',])
562 >>> with pti:
563 ... pdb_invoke('run', lambda x: x)
564 Traceback (most recent call last):
565 TypeError: exec() arg 1 must be a string, bytes or code object
566
567 >>> with pti:
568 ... pdb_invoke('runeval', lambda x: x)
569 Traceback (most recent call last):
570 TypeError: eval() arg 1 must be a string, bytes or code object
571 """
572
573
574def test_pdb_run_with_code_object():
575 """Testing run and runeval with code object as a first argument.
576
Georg Brandle1e8df12010-07-31 08:14:16 +0000577 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000578 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000579 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000580 (Pdb) step
581 --Return--
582 > <string>(1)<module>()->None
583 (Pdb) x
584 1
585 (Pdb) continue
586
587 >>> with PdbTestInput(['x', 'continue']):
588 ... x=0
589 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
590 > <string>(1)<module>()->None
591 (Pdb) x
592 1
593 (Pdb) continue
594 """
595
596
Georg Brandl6cccb862010-07-30 14:16:43 +0000597class PdbTestCase(unittest.TestCase):
598
Senthil Kumaran42d70812012-05-01 10:07:49 +0800599 def run_pdb(self, script, commands):
600 """Run 'script' lines with pdb and the pdb 'commands'."""
601 filename = 'main.py'
602 with open(filename, 'w') as f:
603 f.write(textwrap.dedent(script))
Georg Brandlb4c89022012-05-06 11:50:00 +0200604 self.addCleanup(support.unlink, filename)
Senthil Kumaran42d70812012-05-01 10:07:49 +0800605 cmd = [sys.executable, '-m', 'pdb', filename]
606 stdout = stderr = None
607 with subprocess.Popen(cmd, stdout=subprocess.PIPE,
608 stdin=subprocess.PIPE,
609 stderr=subprocess.STDOUT,
610 ) as proc:
611 stdout, stderr = proc.communicate(str.encode(commands))
612 stdout = stdout and bytes.decode(stdout)
613 stderr = stderr and bytes.decode(stderr)
614 return stdout, stderr
615
Georg Brandl6cccb862010-07-30 14:16:43 +0000616 def test_issue7964(self):
617 # open the file as binary so we can force \r\n newline
618 with open(support.TESTFN, 'wb') as f:
619 f.write(b'print("testing my pdb")\r\n')
620 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
621 proc = subprocess.Popen(cmd,
622 stdout=subprocess.PIPE,
623 stdin=subprocess.PIPE,
624 stderr=subprocess.STDOUT,
625 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000626 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000627 stdout, stderr = proc.communicate(b'quit\n')
628 self.assertNotIn(b'SyntaxError', stdout,
629 "Got a syntax error running test script under PDB")
630
Senthil Kumaran42d70812012-05-01 10:07:49 +0800631 def test_issue13183(self):
632 script = """
633 from bar import bar
634
635 def foo():
636 bar()
637
638 def nope():
639 pass
640
641 def foobar():
642 foo()
643 nope()
644
645 foobar()
646 """
647 commands = """
648 from bar import bar
649 break bar
650 continue
651 step
652 step
653 quit
654 """
655 bar = """
656 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +0800657 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +0800658 """
659 with open('bar.py', 'w') as f:
660 f.write(textwrap.dedent(bar))
Georg Brandlb4c89022012-05-06 11:50:00 +0200661 self.addCleanup(support.unlink, 'bar.py')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800662 stdout, stderr = self.run_pdb(script, commands)
Georg Brandlb4c89022012-05-06 11:50:00 +0200663 self.assertTrue(
664 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
665 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800666
Andrew Svetlov539ee5d2012-12-04 21:08:28 +0200667 def test_issue13210(self):
668 # invoking "continue" on a non-main thread triggered an exception
669 # inside signal.signal
670
671 with open(support.TESTFN, 'wb') as f:
672 f.write(textwrap.dedent("""
673 import threading
674 import pdb
675
676 def start_pdb():
677 pdb.Pdb().set_trace()
678 x = 1
679 y = 1
680
681 t = threading.Thread(target=start_pdb)
682 t.start()""").encode('ascii'))
683 cmd = [sys.executable, '-u', support.TESTFN]
684 proc = subprocess.Popen(cmd,
685 stdout=subprocess.PIPE,
686 stdin=subprocess.PIPE,
687 stderr=subprocess.STDOUT,
688 )
689 self.addCleanup(proc.stdout.close)
690 stdout, stderr = proc.communicate(b'cont\n')
691 self.assertNotIn('Error', stdout.decode(),
692 "Got an error running test script under PDB")
693
Georg Brandl6cccb862010-07-30 14:16:43 +0000694 def tearDown(self):
695 support.unlink(support.TESTFN)
696
697
Georg Brandl243ad662009-05-05 09:00:19 +0000698def test_main():
699 from test import test_pdb
700 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000701 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000702
703
704if __name__ == '__main__':
705 test_main()