blob: c197aff31838887e86fce83522cf93e6be6a307f [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)
Brett Cannon31f59292011-02-21 19:29:56 +000023 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000024
25 def __exit__(self, *exc):
26 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000027 if self.orig_trace:
28 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000029
30
31def test_pdb_displayhook():
32 """This tests the custom displayhook for pdb.
33
34 >>> def test_function(foo, bar):
Georg Brandl34748cd2010-12-04 17:11:36 +000035 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000036 ... pass
37
38 >>> with PdbTestInput([
39 ... 'foo',
40 ... 'bar',
41 ... 'for i in range(5): print(i)',
42 ... 'continue',
43 ... ]):
44 ... test_function(1, None)
45 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
46 -> pass
47 (Pdb) foo
48 1
49 (Pdb) bar
50 (Pdb) for i in range(5): print(i)
51 0
52 1
53 2
54 3
55 4
56 (Pdb) continue
57 """
58
59
Georg Brandl0d089622010-07-30 16:00:46 +000060def test_pdb_basic_commands():
61 """Test the basic commands of pdb.
62
63 >>> def test_function_2(foo, bar='default'):
64 ... print(foo)
65 ... for i in range(5):
66 ... print(i)
67 ... print(bar)
68 ... for i in range(10):
69 ... never_executed
70 ... print('after for')
71 ... print('...')
72 ... return foo.upper()
73
74 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +000075 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000076 ... ret = test_function_2('baz')
77 ... print(ret)
78
79 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
80 ... 'step', # entering the function call
81 ... 'args', # display function args
82 ... 'list', # list function source
83 ... 'bt', # display backtrace
84 ... 'up', # step up to test_function()
85 ... 'down', # step down to test_function_2() again
86 ... 'next', # stepping to print(foo)
87 ... 'next', # stepping to the for loop
88 ... 'step', # stepping into the for loop
89 ... 'until', # continuing until out of the for loop
90 ... 'next', # executing the print(bar)
91 ... 'jump 8', # jump over second for loop
92 ... 'return', # return out of function
93 ... 'retval', # display return value
94 ... 'continue',
95 ... ]):
96 ... test_function()
97 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
98 -> ret = test_function_2('baz')
99 (Pdb) step
100 --Call--
101 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
102 -> def test_function_2(foo, bar='default'):
103 (Pdb) args
104 foo = 'baz'
105 bar = 'default'
106 (Pdb) list
107 1 -> def test_function_2(foo, bar='default'):
108 2 print(foo)
109 3 for i in range(5):
110 4 print(i)
111 5 print(bar)
112 6 for i in range(10):
113 7 never_executed
114 8 print('after for')
115 9 print('...')
116 10 return foo.upper()
117 [EOF]
118 (Pdb) bt
119 ...
120 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
121 -> test_function()
122 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
123 -> ret = test_function_2('baz')
124 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
125 -> def test_function_2(foo, bar='default'):
126 (Pdb) up
127 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
128 -> ret = test_function_2('baz')
129 (Pdb) down
130 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
131 -> def test_function_2(foo, bar='default'):
132 (Pdb) next
133 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
134 -> print(foo)
135 (Pdb) next
136 baz
137 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
138 -> for i in range(5):
139 (Pdb) step
140 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
141 -> print(i)
142 (Pdb) until
143 0
144 1
145 2
146 3
147 4
148 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
149 -> print(bar)
150 (Pdb) next
151 default
152 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
153 -> for i in range(10):
154 (Pdb) jump 8
155 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
156 -> print('after for')
157 (Pdb) return
158 after for
159 ...
160 --Return--
161 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
162 -> return foo.upper()
163 (Pdb) retval
164 'BAZ'
165 (Pdb) continue
166 BAZ
167 """
168
169
170def test_pdb_breakpoint_commands():
171 """Test basic commands related to breakpoints.
172
173 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000174 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000175 ... print(1)
176 ... print(2)
177 ... print(3)
178 ... print(4)
179
180 First, need to clear bdb state that might be left over from previous tests.
181 Otherwise, the new breakpoints might get assigned different numbers.
182
183 >>> from bdb import Breakpoint
184 >>> Breakpoint.next = 1
185 >>> Breakpoint.bplist = {}
186 >>> Breakpoint.bpbynumber = [None]
187
188 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
189 the breakpoint list outputs a tab for the "stop only" and "ignore next"
190 lines, which we don't want to put in here.
191
192 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
193 ... 'break 3',
194 ... 'disable 1',
195 ... 'ignore 1 10',
196 ... 'condition 1 1 < 2',
197 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000198 ... 'break 4',
199 ... 'break',
200 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000201 ... 'break',
202 ... 'condition 1',
203 ... 'enable 1',
204 ... 'clear 1',
205 ... 'commands 2',
206 ... 'print 42',
207 ... 'end',
208 ... 'continue', # will stop at breakpoint 2 (line 4)
209 ... 'clear', # clear all!
210 ... 'y',
211 ... 'tbreak 5',
212 ... 'continue', # will stop at temporary breakpoint
213 ... 'break', # make sure breakpoint is gone
214 ... 'continue',
215 ... ]):
216 ... test_function()
217 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
218 -> print(1)
219 (Pdb) break 3
220 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
221 (Pdb) disable 1
222 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
223 (Pdb) ignore 1 10
224 Will ignore next 10 crossings of breakpoint 1.
225 (Pdb) condition 1 1 < 2
226 New condition set for breakpoint 1.
227 (Pdb) break 4
228 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000229 (Pdb) break 4
230 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
231 (Pdb) break
232 Num Type Disp Enb Where
233 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
234 stop only if 1 < 2
235 ignore next 10 hits
236 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
237 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
238 (Pdb) clear 3
239 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000240 (Pdb) break
241 Num Type Disp Enb Where
242 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
243 stop only if 1 < 2
244 ignore next 10 hits
245 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
246 (Pdb) condition 1
247 Breakpoint 1 is now unconditional.
248 (Pdb) enable 1
249 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
250 (Pdb) clear 1
251 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
252 (Pdb) commands 2
253 (com) print 42
254 (com) end
255 (Pdb) continue
256 1
257 42
258 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
259 -> print(2)
260 (Pdb) clear
261 Clear all breaks? y
262 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
263 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000264 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000265 (Pdb) continue
266 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000267 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000268 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
269 -> print(3)
270 (Pdb) break
271 (Pdb) continue
272 3
273 4
274 """
275
276
Georg Brandle59ca2a2010-07-30 17:04:28 +0000277def do_nothing():
278 pass
279
280def do_something():
281 print(42)
282
283def test_list_commands():
284 """Test the list and source commands of pdb.
285
286 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000287 ... import test.test_pdb
288 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000289 ... 'some...'
290 ... 'more...'
291 ... 'code...'
292 ... 'to...'
293 ... 'make...'
294 ... 'a...'
295 ... 'long...'
296 ... 'listing...'
297 ... 'useful...'
298 ... '...'
299 ... '...'
300 ... return foo
301
302 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000303 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000304 ... ret = test_function_2('baz')
305
306 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
307 ... 'list', # list first function
308 ... 'step', # step into second function
309 ... 'list', # list second function
310 ... 'list', # continue listing to EOF
311 ... 'list 1,3', # list specific lines
312 ... 'list x', # invalid argument
313 ... 'next', # step to import
314 ... 'next', # step over import
315 ... 'step', # step into do_nothing
316 ... 'longlist', # list all lines
317 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000318 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000319 ... 'continue',
320 ... ]):
321 ... test_function()
322 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
323 -> ret = test_function_2('baz')
324 (Pdb) list
325 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000326 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000327 3 -> ret = test_function_2('baz')
328 [EOF]
329 (Pdb) step
330 --Call--
331 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
332 -> def test_function_2(foo):
333 (Pdb) list
334 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000335 2 import test.test_pdb
336 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000337 4 'some...'
338 5 'more...'
339 6 'code...'
340 7 'to...'
341 8 'make...'
342 9 'a...'
343 10 'long...'
344 11 'listing...'
345 (Pdb) list
346 12 'useful...'
347 13 '...'
348 14 '...'
349 15 return foo
350 [EOF]
351 (Pdb) list 1,3
352 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000353 2 import test.test_pdb
354 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000355 (Pdb) list x
356 *** ...
357 (Pdb) next
358 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000359 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000360 (Pdb) next
361 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000362 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000363 (Pdb) step
364 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000365 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000366 -> def do_nothing():
367 (Pdb) longlist
368 ... -> def do_nothing():
369 ... pass
370 (Pdb) source do_something
371 ... def do_something():
372 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000373 (Pdb) source fooxxx
374 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000375 (Pdb) continue
376 """
377
378
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000379def test_post_mortem():
380 """Test post mortem traceback debugging.
381
382 >>> def test_function_2():
383 ... try:
384 ... 1/0
385 ... finally:
386 ... print('Exception!')
387
388 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000389 ... import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000390 ... test_function_2()
391 ... print('Not reached.')
392
393 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
394 ... 'next', # step over exception-raising call
395 ... 'bt', # get a backtrace
396 ... 'list', # list code of test_function()
397 ... 'down', # step into test_function_2()
398 ... 'list', # list code of test_function_2()
399 ... 'continue',
400 ... ]):
401 ... try:
402 ... test_function()
403 ... except ZeroDivisionError:
404 ... print('Correctly reraised.')
405 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
406 -> test_function_2()
407 (Pdb) next
408 Exception!
409 ZeroDivisionError: division by zero
410 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
411 -> test_function_2()
412 (Pdb) bt
413 ...
414 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
415 -> test_function()
416 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
417 -> test_function_2()
418 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
419 -> 1/0
420 (Pdb) list
421 1 def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000422 2 import pdb; pdb.Pdb(nosigint=True).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000423 3 -> test_function_2()
424 4 print('Not reached.')
425 [EOF]
426 (Pdb) down
427 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
428 -> 1/0
429 (Pdb) list
430 1 def test_function_2():
431 2 try:
432 3 >> 1/0
433 4 finally:
434 5 -> print('Exception!')
435 [EOF]
436 (Pdb) continue
437 Correctly reraised.
438 """
439
440
Georg Brandl243ad662009-05-05 09:00:19 +0000441def test_pdb_skip_modules():
442 """This illustrates the simple case of module skipping.
443
444 >>> def skip_module():
445 ... import string
Georg Brandl34748cd2010-12-04 17:11:36 +0000446 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000447 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000448
Georg Brandl9fa2e022009-09-16 16:40:45 +0000449 >>> with PdbTestInput([
450 ... 'step',
451 ... 'continue',
452 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000453 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000454 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
455 -> string.capwords('FOO')
456 (Pdb) step
457 --Return--
458 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
459 -> string.capwords('FOO')
460 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000461 """
Georg Brandl243ad662009-05-05 09:00:19 +0000462
463
464# Module for testing skipping of module that makes a callback
465mod = imp.new_module('module_to_skip')
466exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
467
468
469def test_pdb_skip_modules_with_callback():
470 """This illustrates skipping of modules that call into other code.
471
472 >>> def skip_module():
473 ... def callback():
474 ... return None
Georg Brandl34748cd2010-12-04 17:11:36 +0000475 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000476 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000477
Georg Brandl9fa2e022009-09-16 16:40:45 +0000478 >>> with PdbTestInput([
479 ... 'step',
480 ... 'step',
481 ... 'step',
482 ... 'step',
483 ... 'step',
484 ... 'continue',
485 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000486 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000487 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000488 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
489 -> mod.foo_pony(callback)
490 (Pdb) step
491 --Call--
492 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
493 -> def callback():
494 (Pdb) step
495 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
496 -> return None
497 (Pdb) step
498 --Return--
499 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
500 -> return None
501 (Pdb) step
502 --Return--
503 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
504 -> mod.foo_pony(callback)
505 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000506 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
507 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000508 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000509 """
Georg Brandl243ad662009-05-05 09:00:19 +0000510
511
Georg Brandl3f940892010-07-30 10:29:19 +0000512def test_pdb_continue_in_bottomframe():
513 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
514
515 >>> def test_function():
Georg Brandl34748cd2010-12-04 17:11:36 +0000516 ... import pdb, sys; inst = pdb.Pdb(nosigint=True)
Georg Brandl3f940892010-07-30 10:29:19 +0000517 ... inst.set_trace()
518 ... inst.botframe = sys._getframe() # hackery to get the right botframe
519 ... print(1)
520 ... print(2)
521 ... print(3)
522 ... print(4)
523
Georg Brandl7410dd12010-07-30 12:01:20 +0000524 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000525 ... 'next',
526 ... 'break 7',
527 ... 'continue',
528 ... 'next',
529 ... 'continue',
530 ... 'continue',
531 ... ]):
532 ... test_function()
533 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
534 -> inst.botframe = sys._getframe() # hackery to get the right botframe
535 (Pdb) next
536 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
537 -> print(1)
538 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000539 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000540 (Pdb) continue
541 1
542 2
543 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
544 -> print(3)
545 (Pdb) next
546 3
547 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
548 -> print(4)
549 (Pdb) continue
550 4
551 """
552
553
Georg Brandl46b9afc2010-07-30 09:14:20 +0000554def pdb_invoke(method, arg):
555 """Run pdb.method(arg)."""
Georg Brandl34748cd2010-12-04 17:11:36 +0000556 import pdb
557 getattr(pdb.Pdb(nosigint=True), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000558
559
560def test_pdb_run_with_incorrect_argument():
561 """Testing run and runeval with incorrect first argument.
562
563 >>> pti = PdbTestInput(['continue',])
564 >>> with pti:
565 ... pdb_invoke('run', lambda x: x)
566 Traceback (most recent call last):
567 TypeError: exec() arg 1 must be a string, bytes or code object
568
569 >>> with pti:
570 ... pdb_invoke('runeval', lambda x: x)
571 Traceback (most recent call last):
572 TypeError: eval() arg 1 must be a string, bytes or code object
573 """
574
575
576def test_pdb_run_with_code_object():
577 """Testing run and runeval with code object as a first argument.
578
Georg Brandle1e8df12010-07-31 08:14:16 +0000579 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000580 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000581 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000582 (Pdb) step
583 --Return--
584 > <string>(1)<module>()->None
585 (Pdb) x
586 1
587 (Pdb) continue
588
589 >>> with PdbTestInput(['x', 'continue']):
590 ... x=0
591 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
592 > <string>(1)<module>()->None
593 (Pdb) x
594 1
595 (Pdb) continue
596 """
597
598
Georg Brandl6cccb862010-07-30 14:16:43 +0000599class PdbTestCase(unittest.TestCase):
600
601 def test_issue7964(self):
602 # open the file as binary so we can force \r\n newline
603 with open(support.TESTFN, 'wb') as f:
604 f.write(b'print("testing my pdb")\r\n')
605 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
606 proc = subprocess.Popen(cmd,
607 stdout=subprocess.PIPE,
608 stdin=subprocess.PIPE,
609 stderr=subprocess.STDOUT,
610 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000611 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000612 stdout, stderr = proc.communicate(b'quit\n')
613 self.assertNotIn(b'SyntaxError', stdout,
614 "Got a syntax error running test script under PDB")
615
616 def tearDown(self):
617 support.unlink(support.TESTFN)
618
619
Georg Brandl243ad662009-05-05 09:00:19 +0000620def test_main():
621 from test import test_pdb
622 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000623 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000624
625
626if __name__ == '__main__':
627 test_main()