blob: ac929312e3e4e7c6b852a8f65016ff6f196fafb5 [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)
Brett Cannon31f59292011-02-21 19:29:56 +000024 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000025
26 def __exit__(self, *exc):
27 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000028 if self.orig_trace:
29 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000030
31
32def test_pdb_displayhook():
33 """This tests the custom displayhook for pdb.
34
35 >>> def test_function(foo, bar):
Georg Brandl34748cd2010-12-04 17:11:36 +000036 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000037 ... pass
38
39 >>> with PdbTestInput([
40 ... 'foo',
41 ... 'bar',
42 ... 'for i in range(5): print(i)',
43 ... 'continue',
44 ... ]):
45 ... test_function(1, None)
46 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
47 -> pass
48 (Pdb) foo
49 1
50 (Pdb) bar
51 (Pdb) for i in range(5): print(i)
52 0
53 1
54 2
55 3
56 4
57 (Pdb) continue
58 """
59
60
Georg Brandl0d089622010-07-30 16:00:46 +000061def test_pdb_basic_commands():
62 """Test the basic commands of pdb.
63
64 >>> def test_function_2(foo, bar='default'):
65 ... print(foo)
66 ... for i in range(5):
67 ... print(i)
68 ... print(bar)
69 ... for i in range(10):
70 ... never_executed
71 ... print('after for')
72 ... print('...')
73 ... return foo.upper()
74
75 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +000076 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000077 ... ret = test_function_2('baz')
78 ... print(ret)
79
80 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
81 ... 'step', # entering the function call
82 ... 'args', # display function args
83 ... 'list', # list function source
84 ... 'bt', # display backtrace
85 ... 'up', # step up to test_function()
86 ... 'down', # step down to test_function_2() again
87 ... 'next', # stepping to print(foo)
88 ... 'next', # stepping to the for loop
89 ... 'step', # stepping into the for loop
90 ... 'until', # continuing until out of the for loop
91 ... 'next', # executing the print(bar)
92 ... 'jump 8', # jump over second for loop
93 ... 'return', # return out of function
94 ... 'retval', # display return value
95 ... 'continue',
96 ... ]):
97 ... test_function()
98 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
99 -> ret = test_function_2('baz')
100 (Pdb) step
101 --Call--
102 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
103 -> def test_function_2(foo, bar='default'):
104 (Pdb) args
105 foo = 'baz'
106 bar = 'default'
107 (Pdb) list
108 1 -> def test_function_2(foo, bar='default'):
109 2 print(foo)
110 3 for i in range(5):
111 4 print(i)
112 5 print(bar)
113 6 for i in range(10):
114 7 never_executed
115 8 print('after for')
116 9 print('...')
117 10 return foo.upper()
118 [EOF]
119 (Pdb) bt
120 ...
121 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
122 -> test_function()
123 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
124 -> ret = test_function_2('baz')
125 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
126 -> def test_function_2(foo, bar='default'):
127 (Pdb) up
128 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
129 -> ret = test_function_2('baz')
130 (Pdb) down
131 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
132 -> def test_function_2(foo, bar='default'):
133 (Pdb) next
134 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
135 -> print(foo)
136 (Pdb) next
137 baz
138 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
139 -> for i in range(5):
140 (Pdb) step
141 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
142 -> print(i)
143 (Pdb) until
144 0
145 1
146 2
147 3
148 4
149 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
150 -> print(bar)
151 (Pdb) next
152 default
153 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
154 -> for i in range(10):
155 (Pdb) jump 8
156 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
157 -> print('after for')
158 (Pdb) return
159 after for
160 ...
161 --Return--
162 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
163 -> return foo.upper()
164 (Pdb) retval
165 'BAZ'
166 (Pdb) continue
167 BAZ
168 """
169
170
171def test_pdb_breakpoint_commands():
172 """Test basic commands related to breakpoints.
173
174 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000175 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000176 ... print(1)
177 ... print(2)
178 ... print(3)
179 ... print(4)
180
181 First, need to clear bdb state that might be left over from previous tests.
182 Otherwise, the new breakpoints might get assigned different numbers.
183
184 >>> from bdb import Breakpoint
185 >>> Breakpoint.next = 1
186 >>> Breakpoint.bplist = {}
187 >>> Breakpoint.bpbynumber = [None]
188
189 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
190 the breakpoint list outputs a tab for the "stop only" and "ignore next"
191 lines, which we don't want to put in here.
192
193 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
194 ... 'break 3',
195 ... 'disable 1',
196 ... 'ignore 1 10',
197 ... 'condition 1 1 < 2',
198 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000199 ... 'break 4',
200 ... 'break',
201 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000202 ... 'break',
203 ... 'condition 1',
204 ... 'enable 1',
205 ... 'clear 1',
206 ... 'commands 2',
207 ... 'print 42',
208 ... 'end',
209 ... 'continue', # will stop at breakpoint 2 (line 4)
210 ... 'clear', # clear all!
211 ... 'y',
212 ... 'tbreak 5',
213 ... 'continue', # will stop at temporary breakpoint
214 ... 'break', # make sure breakpoint is gone
215 ... 'continue',
216 ... ]):
217 ... test_function()
218 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
219 -> print(1)
220 (Pdb) break 3
221 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
222 (Pdb) disable 1
223 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
224 (Pdb) ignore 1 10
225 Will ignore next 10 crossings of breakpoint 1.
226 (Pdb) condition 1 1 < 2
227 New condition set for breakpoint 1.
228 (Pdb) break 4
229 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000230 (Pdb) break 4
231 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
232 (Pdb) break
233 Num Type Disp Enb Where
234 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
235 stop only if 1 < 2
236 ignore next 10 hits
237 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
238 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
239 (Pdb) clear 3
240 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000241 (Pdb) break
242 Num Type Disp Enb Where
243 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
244 stop only if 1 < 2
245 ignore next 10 hits
246 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
247 (Pdb) condition 1
248 Breakpoint 1 is now unconditional.
249 (Pdb) enable 1
250 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
251 (Pdb) clear 1
252 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
253 (Pdb) commands 2
254 (com) print 42
255 (com) end
256 (Pdb) continue
257 1
258 42
259 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
260 -> print(2)
261 (Pdb) clear
262 Clear all breaks? y
263 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
264 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000265 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000266 (Pdb) continue
267 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000268 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000269 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
270 -> print(3)
271 (Pdb) break
272 (Pdb) continue
273 3
274 4
275 """
276
277
Georg Brandle59ca2a2010-07-30 17:04:28 +0000278def do_nothing():
279 pass
280
281def do_something():
282 print(42)
283
284def test_list_commands():
285 """Test the list and source commands of pdb.
286
287 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000288 ... import test.test_pdb
289 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000290 ... 'some...'
291 ... 'more...'
292 ... 'code...'
293 ... 'to...'
294 ... 'make...'
295 ... 'a...'
296 ... 'long...'
297 ... 'listing...'
298 ... 'useful...'
299 ... '...'
300 ... '...'
301 ... return foo
302
303 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000304 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000305 ... ret = test_function_2('baz')
306
307 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
308 ... 'list', # list first function
309 ... 'step', # step into second function
310 ... 'list', # list second function
311 ... 'list', # continue listing to EOF
312 ... 'list 1,3', # list specific lines
313 ... 'list x', # invalid argument
314 ... 'next', # step to import
315 ... 'next', # step over import
316 ... 'step', # step into do_nothing
317 ... 'longlist', # list all lines
318 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000319 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000320 ... 'continue',
321 ... ]):
322 ... test_function()
323 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
324 -> ret = test_function_2('baz')
325 (Pdb) list
326 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000327 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000328 3 -> ret = test_function_2('baz')
329 [EOF]
330 (Pdb) step
331 --Call--
332 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
333 -> def test_function_2(foo):
334 (Pdb) list
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 4 'some...'
339 5 'more...'
340 6 'code...'
341 7 'to...'
342 8 'make...'
343 9 'a...'
344 10 'long...'
345 11 'listing...'
346 (Pdb) list
347 12 'useful...'
348 13 '...'
349 14 '...'
350 15 return foo
351 [EOF]
352 (Pdb) list 1,3
353 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000354 2 import test.test_pdb
355 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000356 (Pdb) list x
357 *** ...
358 (Pdb) next
359 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000360 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000361 (Pdb) next
362 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000363 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000364 (Pdb) step
365 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000366 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000367 -> def do_nothing():
368 (Pdb) longlist
369 ... -> def do_nothing():
370 ... pass
371 (Pdb) source do_something
372 ... def do_something():
373 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000374 (Pdb) source fooxxx
375 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000376 (Pdb) continue
377 """
378
379
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000380def test_post_mortem():
381 """Test post mortem traceback debugging.
382
383 >>> def test_function_2():
384 ... try:
385 ... 1/0
386 ... finally:
387 ... print('Exception!')
388
389 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000390 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000391 ... test_function_2()
392 ... print('Not reached.')
393
394 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
395 ... 'next', # step over exception-raising call
396 ... 'bt', # get a backtrace
397 ... 'list', # list code of test_function()
398 ... 'down', # step into test_function_2()
399 ... 'list', # list code of test_function_2()
400 ... 'continue',
401 ... ]):
402 ... try:
403 ... test_function()
404 ... except ZeroDivisionError:
405 ... print('Correctly reraised.')
406 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
407 -> test_function_2()
408 (Pdb) next
409 Exception!
410 ZeroDivisionError: division by zero
411 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
412 -> test_function_2()
413 (Pdb) bt
414 ...
415 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
416 -> test_function()
417 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
418 -> test_function_2()
419 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
420 -> 1/0
421 (Pdb) list
422 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000423 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000424 3 -> test_function_2()
425 4 print('Not reached.')
426 [EOF]
427 (Pdb) down
428 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
429 -> 1/0
430 (Pdb) list
431 1 def test_function_2():
432 2 try:
433 3 >> 1/0
434 4 finally:
435 5 -> print('Exception!')
436 [EOF]
437 (Pdb) continue
438 Correctly reraised.
439 """
440
441
Georg Brandl243ad662009-05-05 09:00:19 +0000442def test_pdb_skip_modules():
443 """This illustrates the simple case of module skipping.
444
445 >>> def skip_module():
446 ... import string
Georg Brandl34748cd2010-12-04 17:11:36 +0000447 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000448 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000449
Georg Brandl9fa2e022009-09-16 16:40:45 +0000450 >>> with PdbTestInput([
451 ... 'step',
452 ... 'continue',
453 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000454 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000455 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
456 -> string.capwords('FOO')
457 (Pdb) step
458 --Return--
459 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
460 -> string.capwords('FOO')
461 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000462 """
Georg Brandl243ad662009-05-05 09:00:19 +0000463
464
465# Module for testing skipping of module that makes a callback
466mod = imp.new_module('module_to_skip')
467exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
468
469
470def test_pdb_skip_modules_with_callback():
471 """This illustrates skipping of modules that call into other code.
472
473 >>> def skip_module():
474 ... def callback():
475 ... return None
Georg Brandl34748cd2010-12-04 17:11:36 +0000476 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000477 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000478
Georg Brandl9fa2e022009-09-16 16:40:45 +0000479 >>> with PdbTestInput([
480 ... 'step',
481 ... 'step',
482 ... 'step',
483 ... 'step',
484 ... 'step',
485 ... 'continue',
486 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000487 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000488 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000489 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
490 -> mod.foo_pony(callback)
491 (Pdb) step
492 --Call--
493 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
494 -> def callback():
495 (Pdb) step
496 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
497 -> return None
498 (Pdb) step
499 --Return--
500 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
501 -> return None
502 (Pdb) step
503 --Return--
504 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
505 -> mod.foo_pony(callback)
506 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000507 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
508 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000509 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000510 """
Georg Brandl243ad662009-05-05 09:00:19 +0000511
512
Georg Brandl3f940892010-07-30 10:29:19 +0000513def test_pdb_continue_in_bottomframe():
514 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
515
516 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000517 ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
Georg Brandl3f940892010-07-30 10:29:19 +0000518 ... inst.set_trace()
519 ... inst.botframe = sys._getframe() # hackery to get the right botframe
520 ... print(1)
521 ... print(2)
522 ... print(3)
523 ... print(4)
524
Georg Brandl7410dd12010-07-30 12:01:20 +0000525 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000526 ... 'next',
527 ... 'break 7',
528 ... 'continue',
529 ... 'next',
530 ... 'continue',
531 ... 'continue',
532 ... ]):
533 ... test_function()
534 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
535 -> inst.botframe = sys._getframe() # hackery to get the right botframe
536 (Pdb) next
537 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
538 -> print(1)
539 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000540 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000541 (Pdb) continue
542 1
543 2
544 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
545 -> print(3)
546 (Pdb) next
547 3
548 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
549 -> print(4)
550 (Pdb) continue
551 4
552 """
553
554
Georg Brandl46b9afc2010-07-30 09:14:20 +0000555def pdb_invoke(method, arg):
556 """Run pdb.method(arg)."""
Georg Brandl34748cd2010-12-04 17:11:36 +0000557 import pdb
558 getattr(pdb.Pdb(nosigint=True), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000559
560
561def test_pdb_run_with_incorrect_argument():
562 """Testing run and runeval with incorrect first argument.
563
564 >>> pti = PdbTestInput(['continue',])
565 >>> with pti:
566 ... pdb_invoke('run', lambda x: x)
567 Traceback (most recent call last):
568 TypeError: exec() arg 1 must be a string, bytes or code object
569
570 >>> with pti:
571 ... pdb_invoke('runeval', lambda x: x)
572 Traceback (most recent call last):
573 TypeError: eval() arg 1 must be a string, bytes or code object
574 """
575
576
577def test_pdb_run_with_code_object():
578 """Testing run and runeval with code object as a first argument.
579
Georg Brandle1e8df12010-07-31 08:14:16 +0000580 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000581 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000582 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000583 (Pdb) step
584 --Return--
585 > <string>(1)<module>()->None
586 (Pdb) x
587 1
588 (Pdb) continue
589
590 >>> with PdbTestInput(['x', 'continue']):
591 ... x=0
592 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
593 > <string>(1)<module>()->None
594 (Pdb) x
595 1
596 (Pdb) continue
597 """
598
599
Georg Brandl6cccb862010-07-30 14:16:43 +0000600class PdbTestCase(unittest.TestCase):
601
Senthil Kumaran42d70812012-05-01 10:07:49 +0800602 def run_pdb(self, script, commands):
603 """Run 'script' lines with pdb and the pdb 'commands'."""
604 filename = 'main.py'
605 with open(filename, 'w') as f:
606 f.write(textwrap.dedent(script))
607 cmd = [sys.executable, '-m', 'pdb', filename]
608 stdout = stderr = None
609 with subprocess.Popen(cmd, stdout=subprocess.PIPE,
610 stdin=subprocess.PIPE,
611 stderr=subprocess.STDOUT,
612 ) as proc:
613 stdout, stderr = proc.communicate(str.encode(commands))
614 stdout = stdout and bytes.decode(stdout)
615 stderr = stderr and bytes.decode(stderr)
616 return stdout, stderr
617
Georg Brandl6cccb862010-07-30 14:16:43 +0000618 def test_issue7964(self):
619 # open the file as binary so we can force \r\n newline
620 with open(support.TESTFN, 'wb') as f:
621 f.write(b'print("testing my pdb")\r\n')
622 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
623 proc = subprocess.Popen(cmd,
624 stdout=subprocess.PIPE,
625 stdin=subprocess.PIPE,
626 stderr=subprocess.STDOUT,
627 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000628 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000629 stdout, stderr = proc.communicate(b'quit\n')
630 self.assertNotIn(b'SyntaxError', stdout,
631 "Got a syntax error running test script under PDB")
632
Senthil Kumaran42d70812012-05-01 10:07:49 +0800633 def test_issue13183(self):
634 script = """
635 from bar import bar
636
637 def foo():
638 bar()
639
640 def nope():
641 pass
642
643 def foobar():
644 foo()
645 nope()
646
647 foobar()
648 """
649 commands = """
650 from bar import bar
651 break bar
652 continue
653 step
654 step
655 quit
656 """
657 bar = """
658 def bar():
659 print('1')
660 """
661 with open('bar.py', 'w') as f:
662 f.write(textwrap.dedent(bar))
663 stdout, stderr = self.run_pdb(script, commands)
664 self.assertIn('main.py(5)foo()->None', stdout.split('\n')[-3],
665 'Fail to step into the caller after a return')
666
Georg Brandl6cccb862010-07-30 14:16:43 +0000667 def tearDown(self):
668 support.unlink(support.TESTFN)
669
670
Georg Brandl243ad662009-05-05 09:00:19 +0000671def test_main():
672 from test import test_pdb
673 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000674 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000675
676
677if __name__ == '__main__':
678 test_main()