blob: 03084e4d122ed2828653110a32f801092a2abb3c [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 Brandl243ad662009-05-05 09:00:19 +00004import imp
Georg Brandl6cccb862010-07-30 14:16:43 +00005import pdb
Georg Brandl243ad662009-05-05 09:00:19 +00006import sys
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',
208 ... 'print 42',
209 ... 'end',
210 ... 'continue', # will stop at breakpoint 2 (line 4)
211 ... 'clear', # clear all!
212 ... 'y',
213 ... 'tbreak 5',
214 ... 'continue', # will stop at temporary breakpoint
215 ... 'break', # make sure breakpoint is gone
216 ... 'continue',
217 ... ]):
218 ... test_function()
219 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
220 -> print(1)
221 (Pdb) break 3
222 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
223 (Pdb) disable 1
224 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
225 (Pdb) ignore 1 10
226 Will ignore next 10 crossings of breakpoint 1.
227 (Pdb) condition 1 1 < 2
228 New condition set for breakpoint 1.
229 (Pdb) break 4
230 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000231 (Pdb) break 4
232 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
233 (Pdb) break
234 Num Type Disp Enb Where
235 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
236 stop only if 1 < 2
237 ignore next 10 hits
238 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
239 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
240 (Pdb) clear 3
241 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000242 (Pdb) break
243 Num Type Disp Enb Where
244 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
245 stop only if 1 < 2
246 ignore next 10 hits
247 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
248 (Pdb) condition 1
249 Breakpoint 1 is now unconditional.
250 (Pdb) enable 1
251 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
252 (Pdb) clear 1
253 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
254 (Pdb) commands 2
255 (com) print 42
256 (com) end
257 (Pdb) continue
258 1
259 42
260 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
261 -> print(2)
262 (Pdb) clear
263 Clear all breaks? y
264 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
265 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000266 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000267 (Pdb) continue
268 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000269 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000270 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
271 -> print(3)
272 (Pdb) break
273 (Pdb) continue
274 3
275 4
276 """
277
278
Georg Brandle59ca2a2010-07-30 17:04:28 +0000279def do_nothing():
280 pass
281
282def do_something():
283 print(42)
284
285def test_list_commands():
286 """Test the list and source commands of pdb.
287
288 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000289 ... import test.test_pdb
290 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000291 ... 'some...'
292 ... 'more...'
293 ... 'code...'
294 ... 'to...'
295 ... 'make...'
296 ... 'a...'
297 ... 'long...'
298 ... 'listing...'
299 ... 'useful...'
300 ... '...'
301 ... '...'
302 ... return foo
303
304 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000305 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000306 ... ret = test_function_2('baz')
307
308 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
309 ... 'list', # list first function
310 ... 'step', # step into second function
311 ... 'list', # list second function
312 ... 'list', # continue listing to EOF
313 ... 'list 1,3', # list specific lines
314 ... 'list x', # invalid argument
315 ... 'next', # step to import
316 ... 'next', # step over import
317 ... 'step', # step into do_nothing
318 ... 'longlist', # list all lines
319 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000320 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000321 ... 'continue',
322 ... ]):
323 ... test_function()
324 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
325 -> ret = test_function_2('baz')
326 (Pdb) list
327 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000328 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000329 3 -> ret = test_function_2('baz')
330 [EOF]
331 (Pdb) step
332 --Call--
333 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
334 -> def test_function_2(foo):
335 (Pdb) list
336 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000337 2 import test.test_pdb
338 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000339 4 'some...'
340 5 'more...'
341 6 'code...'
342 7 'to...'
343 8 'make...'
344 9 'a...'
345 10 'long...'
346 11 'listing...'
347 (Pdb) list
348 12 'useful...'
349 13 '...'
350 14 '...'
351 15 return foo
352 [EOF]
353 (Pdb) list 1,3
354 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000355 2 import test.test_pdb
356 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000357 (Pdb) list x
358 *** ...
359 (Pdb) next
360 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000361 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000362 (Pdb) next
363 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000364 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000365 (Pdb) step
366 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000367 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000368 -> def do_nothing():
369 (Pdb) longlist
370 ... -> def do_nothing():
371 ... pass
372 (Pdb) source do_something
373 ... def do_something():
374 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000375 (Pdb) source fooxxx
376 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000377 (Pdb) continue
378 """
379
380
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000381def test_post_mortem():
382 """Test post mortem traceback debugging.
383
384 >>> def test_function_2():
385 ... try:
386 ... 1/0
387 ... finally:
388 ... print('Exception!')
389
390 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000391 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000392 ... test_function_2()
393 ... print('Not reached.')
394
395 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
396 ... 'next', # step over exception-raising call
397 ... 'bt', # get a backtrace
398 ... 'list', # list code of test_function()
399 ... 'down', # step into test_function_2()
400 ... 'list', # list code of test_function_2()
401 ... 'continue',
402 ... ]):
403 ... try:
404 ... test_function()
405 ... except ZeroDivisionError:
406 ... print('Correctly reraised.')
407 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
408 -> test_function_2()
409 (Pdb) next
410 Exception!
411 ZeroDivisionError: division by zero
412 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
413 -> test_function_2()
414 (Pdb) bt
415 ...
416 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
417 -> test_function()
418 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
419 -> test_function_2()
420 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
421 -> 1/0
422 (Pdb) list
423 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000424 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000425 3 -> test_function_2()
426 4 print('Not reached.')
427 [EOF]
428 (Pdb) down
429 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
430 -> 1/0
431 (Pdb) list
432 1 def test_function_2():
433 2 try:
434 3 >> 1/0
435 4 finally:
436 5 -> print('Exception!')
437 [EOF]
438 (Pdb) continue
439 Correctly reraised.
440 """
441
442
Georg Brandl243ad662009-05-05 09:00:19 +0000443def test_pdb_skip_modules():
444 """This illustrates the simple case of module skipping.
445
446 >>> def skip_module():
447 ... import string
Georg Brandl34748cd2010-12-04 17:11:36 +0000448 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000449 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000450
Georg Brandl9fa2e022009-09-16 16:40:45 +0000451 >>> with PdbTestInput([
452 ... 'step',
453 ... 'continue',
454 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000455 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000456 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
457 -> string.capwords('FOO')
458 (Pdb) step
459 --Return--
460 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
461 -> string.capwords('FOO')
462 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000463 """
Georg Brandl243ad662009-05-05 09:00:19 +0000464
465
466# Module for testing skipping of module that makes a callback
467mod = imp.new_module('module_to_skip')
468exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
469
470
471def test_pdb_skip_modules_with_callback():
472 """This illustrates skipping of modules that call into other code.
473
474 >>> def skip_module():
475 ... def callback():
476 ... return None
Georg Brandl34748cd2010-12-04 17:11:36 +0000477 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000478 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000479
Georg Brandl9fa2e022009-09-16 16:40:45 +0000480 >>> with PdbTestInput([
481 ... 'step',
482 ... 'step',
483 ... 'step',
484 ... 'step',
485 ... 'step',
486 ... 'continue',
487 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000488 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000489 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000490 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
491 -> mod.foo_pony(callback)
492 (Pdb) step
493 --Call--
494 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
495 -> def callback():
496 (Pdb) step
497 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
498 -> return None
499 (Pdb) step
500 --Return--
501 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
502 -> return None
503 (Pdb) step
504 --Return--
505 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
506 -> mod.foo_pony(callback)
507 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000508 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
509 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000510 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000511 """
Georg Brandl243ad662009-05-05 09:00:19 +0000512
513
Georg Brandl3f940892010-07-30 10:29:19 +0000514def test_pdb_continue_in_bottomframe():
515 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
516
517 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000518 ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
Georg Brandl3f940892010-07-30 10:29:19 +0000519 ... inst.set_trace()
520 ... inst.botframe = sys._getframe() # hackery to get the right botframe
521 ... print(1)
522 ... print(2)
523 ... print(3)
524 ... print(4)
525
Georg Brandl7410dd12010-07-30 12:01:20 +0000526 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000527 ... 'next',
528 ... 'break 7',
529 ... 'continue',
530 ... 'next',
531 ... 'continue',
532 ... 'continue',
533 ... ]):
534 ... test_function()
535 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
536 -> inst.botframe = sys._getframe() # hackery to get the right botframe
537 (Pdb) next
538 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
539 -> print(1)
540 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000541 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000542 (Pdb) continue
543 1
544 2
545 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
546 -> print(3)
547 (Pdb) next
548 3
549 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
550 -> print(4)
551 (Pdb) continue
552 4
553 """
554
555
Georg Brandl46b9afc2010-07-30 09:14:20 +0000556def pdb_invoke(method, arg):
557 """Run pdb.method(arg)."""
Georg Brandl34748cd2010-12-04 17:11:36 +0000558 import pdb
559 getattr(pdb.Pdb(nosigint=True), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000560
561
562def test_pdb_run_with_incorrect_argument():
563 """Testing run and runeval with incorrect first argument.
564
565 >>> pti = PdbTestInput(['continue',])
566 >>> with pti:
567 ... pdb_invoke('run', lambda x: x)
568 Traceback (most recent call last):
569 TypeError: exec() arg 1 must be a string, bytes or code object
570
571 >>> with pti:
572 ... pdb_invoke('runeval', lambda x: x)
573 Traceback (most recent call last):
574 TypeError: eval() arg 1 must be a string, bytes or code object
575 """
576
577
578def test_pdb_run_with_code_object():
579 """Testing run and runeval with code object as a first argument.
580
Georg Brandle1e8df12010-07-31 08:14:16 +0000581 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000582 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000583 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000584 (Pdb) step
585 --Return--
586 > <string>(1)<module>()->None
587 (Pdb) x
588 1
589 (Pdb) continue
590
591 >>> with PdbTestInput(['x', 'continue']):
592 ... x=0
593 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
594 > <string>(1)<module>()->None
595 (Pdb) x
596 1
597 (Pdb) continue
598 """
599
600
Georg Brandl6cccb862010-07-30 14:16:43 +0000601class PdbTestCase(unittest.TestCase):
602
Senthil Kumaran42d70812012-05-01 10:07:49 +0800603 def run_pdb(self, script, commands):
604 """Run 'script' lines with pdb and the pdb 'commands'."""
605 filename = 'main.py'
606 with open(filename, 'w') as f:
607 f.write(textwrap.dedent(script))
Georg Brandl4bde9ca2012-05-01 09:21:16 +0200608 self.addCleanup(support.unlink, filename)
Senthil Kumaran42d70812012-05-01 10:07:49 +0800609 cmd = [sys.executable, '-m', 'pdb', filename]
610 stdout = stderr = None
611 with subprocess.Popen(cmd, stdout=subprocess.PIPE,
612 stdin=subprocess.PIPE,
613 stderr=subprocess.STDOUT,
614 ) as proc:
615 stdout, stderr = proc.communicate(str.encode(commands))
616 stdout = stdout and bytes.decode(stdout)
617 stderr = stderr and bytes.decode(stderr)
618 return stdout, stderr
619
Georg Brandl6cccb862010-07-30 14:16:43 +0000620 def test_issue7964(self):
621 # open the file as binary so we can force \r\n newline
622 with open(support.TESTFN, 'wb') as f:
623 f.write(b'print("testing my pdb")\r\n')
624 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
625 proc = subprocess.Popen(cmd,
626 stdout=subprocess.PIPE,
627 stdin=subprocess.PIPE,
628 stderr=subprocess.STDOUT,
629 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000630 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000631 stdout, stderr = proc.communicate(b'quit\n')
632 self.assertNotIn(b'SyntaxError', stdout,
633 "Got a syntax error running test script under PDB")
634
Senthil Kumaran42d70812012-05-01 10:07:49 +0800635 def test_issue13183(self):
636 script = """
637 from bar import bar
638
639 def foo():
640 bar()
641
642 def nope():
643 pass
644
645 def foobar():
646 foo()
647 nope()
648
649 foobar()
650 """
651 commands = """
652 from bar import bar
653 break bar
654 continue
655 step
656 step
657 quit
658 """
659 bar = """
660 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +0800661 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +0800662 """
663 with open('bar.py', 'w') as f:
664 f.write(textwrap.dedent(bar))
Georg Brandl4bde9ca2012-05-01 09:21:16 +0200665 self.addCleanup(support.unlink, 'bar.py')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800666 stdout, stderr = self.run_pdb(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +0200667 self.assertTrue(
668 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
669 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800670
Andrew Svetlov539ee5d2012-12-04 21:08:28 +0200671 def test_issue13210(self):
672 # invoking "continue" on a non-main thread triggered an exception
673 # inside signal.signal
674
Andrew Svetlov96bc0432012-12-05 15:06:23 +0200675 # raises SkipTest if python was built without threads
676 support.import_module('threading')
677
Andrew Svetlov539ee5d2012-12-04 21:08:28 +0200678 with open(support.TESTFN, 'wb') as f:
679 f.write(textwrap.dedent("""
680 import threading
681 import pdb
682
683 def start_pdb():
684 pdb.Pdb().set_trace()
685 x = 1
686 y = 1
687
688 t = threading.Thread(target=start_pdb)
689 t.start()""").encode('ascii'))
690 cmd = [sys.executable, '-u', support.TESTFN]
691 proc = subprocess.Popen(cmd,
692 stdout=subprocess.PIPE,
693 stdin=subprocess.PIPE,
694 stderr=subprocess.STDOUT,
695 )
696 self.addCleanup(proc.stdout.close)
697 stdout, stderr = proc.communicate(b'cont\n')
698 self.assertNotIn('Error', stdout.decode(),
699 "Got an error running test script under PDB")
700
Georg Brandl6cccb862010-07-30 14:16:43 +0000701 def tearDown(self):
702 support.unlink(support.TESTFN)
703
704
Andrew Svetlovf0efea02013-03-18 10:09:50 -0700705def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +0000706 from test import test_pdb
Andrew Svetlovf0efea02013-03-18 10:09:50 -0700707 suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
708 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +0000709
710
711if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -0700712 unittest.main()