blob: 56d823249544ae8e866519892631da1d890c9f9d [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
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07004import os
Georg Brandl6cccb862010-07-30 14:16:43 +00005import pdb
Georg Brandl243ad662009-05-05 09:00:19 +00006import sys
Brett Cannon9529fbf2013-06-15 17:11:25 -04007import types
Georg Brandl6cccb862010-07-30 14:16:43 +00008import unittest
9import subprocess
Senthil Kumaran42d70812012-05-01 10:07:49 +080010import textwrap
Georg Brandl243ad662009-05-05 09:00:19 +000011
Barry Warsaw35425d62017-09-22 12:29:42 -040012from contextlib import ExitStack
13from io import StringIO
Georg Brandl243ad662009-05-05 09:00:19 +000014from test import support
15# This little helper class is essential for testing pdb under doctest.
16from test.test_doctest import _FakeInput
Barry Warsaw35425d62017-09-22 12:29:42 -040017from unittest.mock import patch
Georg Brandl243ad662009-05-05 09:00:19 +000018
19
Georg Brandl9fa2e022009-09-16 16:40:45 +000020class PdbTestInput(object):
21 """Context manager that makes testing Pdb in doctests easier."""
22
23 def __init__(self, input):
24 self.input = input
25
26 def __enter__(self):
27 self.real_stdin = sys.stdin
28 sys.stdin = _FakeInput(self.input)
Brett Cannon31f59292011-02-21 19:29:56 +000029 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000030
31 def __exit__(self, *exc):
32 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000033 if self.orig_trace:
34 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000035
36
37def test_pdb_displayhook():
38 """This tests the custom displayhook for pdb.
39
40 >>> def test_function(foo, bar):
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070041 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000042 ... pass
43
44 >>> with PdbTestInput([
45 ... 'foo',
46 ... 'bar',
47 ... 'for i in range(5): print(i)',
48 ... 'continue',
49 ... ]):
50 ... test_function(1, None)
51 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
52 -> pass
53 (Pdb) foo
54 1
55 (Pdb) bar
56 (Pdb) for i in range(5): print(i)
57 0
58 1
59 2
60 3
61 4
62 (Pdb) continue
63 """
64
65
Georg Brandl0d089622010-07-30 16:00:46 +000066def test_pdb_basic_commands():
67 """Test the basic commands of pdb.
68
69 >>> def test_function_2(foo, bar='default'):
70 ... print(foo)
71 ... for i in range(5):
72 ... print(i)
73 ... print(bar)
74 ... for i in range(10):
75 ... never_executed
76 ... print('after for')
77 ... print('...')
78 ... return foo.upper()
79
80 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070081 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000082 ... ret = test_function_2('baz')
83 ... print(ret)
84
85 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
86 ... 'step', # entering the function call
87 ... 'args', # display function args
88 ... 'list', # list function source
89 ... 'bt', # display backtrace
90 ... 'up', # step up to test_function()
91 ... 'down', # step down to test_function_2() again
92 ... 'next', # stepping to print(foo)
93 ... 'next', # stepping to the for loop
94 ... 'step', # stepping into the for loop
95 ... 'until', # continuing until out of the for loop
96 ... 'next', # executing the print(bar)
97 ... 'jump 8', # jump over second for loop
98 ... 'return', # return out of function
99 ... 'retval', # display return value
100 ... 'continue',
101 ... ]):
102 ... test_function()
103 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
104 -> ret = test_function_2('baz')
105 (Pdb) step
106 --Call--
107 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
108 -> def test_function_2(foo, bar='default'):
109 (Pdb) args
110 foo = 'baz'
111 bar = 'default'
112 (Pdb) list
113 1 -> def test_function_2(foo, bar='default'):
114 2 print(foo)
115 3 for i in range(5):
116 4 print(i)
117 5 print(bar)
118 6 for i in range(10):
119 7 never_executed
120 8 print('after for')
121 9 print('...')
122 10 return foo.upper()
123 [EOF]
124 (Pdb) bt
125 ...
126 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
127 -> test_function()
128 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
129 -> ret = test_function_2('baz')
130 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
131 -> def test_function_2(foo, bar='default'):
132 (Pdb) up
133 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
134 -> ret = test_function_2('baz')
135 (Pdb) down
136 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
137 -> def test_function_2(foo, bar='default'):
138 (Pdb) next
139 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
140 -> print(foo)
141 (Pdb) next
142 baz
143 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
144 -> for i in range(5):
145 (Pdb) step
146 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
147 -> print(i)
148 (Pdb) until
149 0
150 1
151 2
152 3
153 4
154 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
155 -> print(bar)
156 (Pdb) next
157 default
158 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
159 -> for i in range(10):
160 (Pdb) jump 8
161 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
162 -> print('after for')
163 (Pdb) return
164 after for
165 ...
166 --Return--
167 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
168 -> return foo.upper()
169 (Pdb) retval
170 'BAZ'
171 (Pdb) continue
172 BAZ
173 """
174
175
176def test_pdb_breakpoint_commands():
177 """Test basic commands related to breakpoints.
178
179 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700180 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000181 ... print(1)
182 ... print(2)
183 ... print(3)
184 ... print(4)
185
186 First, need to clear bdb state that might be left over from previous tests.
187 Otherwise, the new breakpoints might get assigned different numbers.
188
189 >>> from bdb import Breakpoint
190 >>> Breakpoint.next = 1
191 >>> Breakpoint.bplist = {}
192 >>> Breakpoint.bpbynumber = [None]
193
194 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
195 the breakpoint list outputs a tab for the "stop only" and "ignore next"
196 lines, which we don't want to put in here.
197
198 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
199 ... 'break 3',
200 ... 'disable 1',
201 ... 'ignore 1 10',
202 ... 'condition 1 1 < 2',
203 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000204 ... 'break 4',
205 ... 'break',
206 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000207 ... 'break',
208 ... 'condition 1',
209 ... 'enable 1',
210 ... 'clear 1',
211 ... 'commands 2',
R David Murray78d692f2013-10-10 17:23:26 -0400212 ... 'p "42"',
213 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
Georg Brandl0d089622010-07-30 16:00:46 +0000214 ... 'end',
215 ... 'continue', # will stop at breakpoint 2 (line 4)
216 ... 'clear', # clear all!
217 ... 'y',
218 ... 'tbreak 5',
219 ... 'continue', # will stop at temporary breakpoint
220 ... 'break', # make sure breakpoint is gone
221 ... 'continue',
222 ... ]):
223 ... test_function()
224 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
225 -> print(1)
226 (Pdb) break 3
227 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
228 (Pdb) disable 1
229 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
230 (Pdb) ignore 1 10
231 Will ignore next 10 crossings of breakpoint 1.
232 (Pdb) condition 1 1 < 2
233 New condition set for breakpoint 1.
234 (Pdb) break 4
235 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000236 (Pdb) break 4
237 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
238 (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 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
245 (Pdb) clear 3
246 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000247 (Pdb) break
248 Num Type Disp Enb Where
249 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
250 stop only if 1 < 2
251 ignore next 10 hits
252 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
253 (Pdb) condition 1
254 Breakpoint 1 is now unconditional.
255 (Pdb) enable 1
256 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
257 (Pdb) clear 1
258 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
259 (Pdb) commands 2
R David Murray78d692f2013-10-10 17:23:26 -0400260 (com) p "42"
261 (com) print("42", 7*6)
Georg Brandl0d089622010-07-30 16:00:46 +0000262 (com) end
263 (Pdb) continue
264 1
R David Murray78d692f2013-10-10 17:23:26 -0400265 '42'
266 42 42
Georg Brandl0d089622010-07-30 16:00:46 +0000267 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
268 -> print(2)
269 (Pdb) clear
270 Clear all breaks? y
271 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
272 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000273 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000274 (Pdb) continue
275 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000276 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000277 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
278 -> print(3)
279 (Pdb) break
280 (Pdb) continue
281 3
282 4
283 """
284
285
Georg Brandle59ca2a2010-07-30 17:04:28 +0000286def do_nothing():
287 pass
288
289def do_something():
290 print(42)
291
292def test_list_commands():
293 """Test the list and source commands of pdb.
294
295 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000296 ... import test.test_pdb
297 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000298 ... 'some...'
299 ... 'more...'
300 ... 'code...'
301 ... 'to...'
302 ... 'make...'
303 ... 'a...'
304 ... 'long...'
305 ... 'listing...'
306 ... 'useful...'
307 ... '...'
308 ... '...'
309 ... return foo
310
311 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700312 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000313 ... ret = test_function_2('baz')
314
315 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
316 ... 'list', # list first function
317 ... 'step', # step into second function
318 ... 'list', # list second function
319 ... 'list', # continue listing to EOF
320 ... 'list 1,3', # list specific lines
321 ... 'list x', # invalid argument
322 ... 'next', # step to import
323 ... 'next', # step over import
324 ... 'step', # step into do_nothing
325 ... 'longlist', # list all lines
326 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000327 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000328 ... 'continue',
329 ... ]):
330 ... test_function()
331 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
332 -> ret = test_function_2('baz')
333 (Pdb) list
334 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700335 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000336 3 -> ret = test_function_2('baz')
337 [EOF]
338 (Pdb) step
339 --Call--
340 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
341 -> def test_function_2(foo):
342 (Pdb) list
343 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000344 2 import test.test_pdb
345 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000346 4 'some...'
347 5 'more...'
348 6 'code...'
349 7 'to...'
350 8 'make...'
351 9 'a...'
352 10 'long...'
353 11 'listing...'
354 (Pdb) list
355 12 'useful...'
356 13 '...'
357 14 '...'
358 15 return foo
359 [EOF]
360 (Pdb) list 1,3
361 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000362 2 import test.test_pdb
363 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000364 (Pdb) list x
365 *** ...
366 (Pdb) next
367 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000368 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000369 (Pdb) next
370 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000371 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000372 (Pdb) step
373 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000374 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000375 -> def do_nothing():
376 (Pdb) longlist
377 ... -> def do_nothing():
378 ... pass
379 (Pdb) source do_something
380 ... def do_something():
381 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000382 (Pdb) source fooxxx
383 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000384 (Pdb) continue
385 """
386
387
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000388def test_post_mortem():
389 """Test post mortem traceback debugging.
390
391 >>> def test_function_2():
392 ... try:
393 ... 1/0
394 ... finally:
395 ... print('Exception!')
396
397 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700398 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000399 ... test_function_2()
400 ... print('Not reached.')
401
402 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
403 ... 'next', # step over exception-raising call
404 ... 'bt', # get a backtrace
405 ... 'list', # list code of test_function()
406 ... 'down', # step into test_function_2()
407 ... 'list', # list code of test_function_2()
408 ... 'continue',
409 ... ]):
410 ... try:
411 ... test_function()
412 ... except ZeroDivisionError:
413 ... print('Correctly reraised.')
414 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
415 -> test_function_2()
416 (Pdb) next
417 Exception!
418 ZeroDivisionError: division by zero
419 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
420 -> test_function_2()
421 (Pdb) bt
422 ...
423 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
424 -> test_function()
425 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
426 -> test_function_2()
427 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
428 -> 1/0
429 (Pdb) list
430 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700431 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000432 3 -> test_function_2()
433 4 print('Not reached.')
434 [EOF]
435 (Pdb) down
436 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
437 -> 1/0
438 (Pdb) list
439 1 def test_function_2():
440 2 try:
441 3 >> 1/0
442 4 finally:
443 5 -> print('Exception!')
444 [EOF]
445 (Pdb) continue
446 Correctly reraised.
447 """
448
449
Georg Brandl243ad662009-05-05 09:00:19 +0000450def test_pdb_skip_modules():
451 """This illustrates the simple case of module skipping.
452
453 >>> def skip_module():
454 ... import string
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700455 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000456 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000457
Georg Brandl9fa2e022009-09-16 16:40:45 +0000458 >>> with PdbTestInput([
459 ... 'step',
460 ... 'continue',
461 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000462 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000463 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
464 -> string.capwords('FOO')
465 (Pdb) step
466 --Return--
467 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
468 -> string.capwords('FOO')
469 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000470 """
Georg Brandl243ad662009-05-05 09:00:19 +0000471
472
473# Module for testing skipping of module that makes a callback
Brett Cannon9529fbf2013-06-15 17:11:25 -0400474mod = types.ModuleType('module_to_skip')
Georg Brandl243ad662009-05-05 09:00:19 +0000475exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
476
477
478def test_pdb_skip_modules_with_callback():
479 """This illustrates skipping of modules that call into other code.
480
481 >>> def skip_module():
482 ... def callback():
483 ... return None
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700484 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000485 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000486
Georg Brandl9fa2e022009-09-16 16:40:45 +0000487 >>> with PdbTestInput([
488 ... 'step',
489 ... 'step',
490 ... 'step',
491 ... 'step',
492 ... 'step',
493 ... 'continue',
494 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000495 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000496 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000497 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
498 -> mod.foo_pony(callback)
499 (Pdb) step
500 --Call--
501 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
502 -> def callback():
503 (Pdb) step
504 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
505 -> return None
506 (Pdb) step
507 --Return--
508 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
509 -> return None
510 (Pdb) step
511 --Return--
512 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
513 -> mod.foo_pony(callback)
514 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000515 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
516 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000517 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000518 """
Georg Brandl243ad662009-05-05 09:00:19 +0000519
520
Georg Brandl3f940892010-07-30 10:29:19 +0000521def test_pdb_continue_in_bottomframe():
522 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
523
524 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700525 ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
Georg Brandl3f940892010-07-30 10:29:19 +0000526 ... inst.set_trace()
527 ... inst.botframe = sys._getframe() # hackery to get the right botframe
528 ... print(1)
529 ... print(2)
530 ... print(3)
531 ... print(4)
532
Georg Brandl7410dd12010-07-30 12:01:20 +0000533 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000534 ... 'next',
535 ... 'break 7',
536 ... 'continue',
537 ... 'next',
538 ... 'continue',
539 ... 'continue',
540 ... ]):
541 ... test_function()
542 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
543 -> inst.botframe = sys._getframe() # hackery to get the right botframe
544 (Pdb) next
545 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
546 -> print(1)
547 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000548 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000549 (Pdb) continue
550 1
551 2
552 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
553 -> print(3)
554 (Pdb) next
555 3
556 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
557 -> print(4)
558 (Pdb) continue
559 4
560 """
561
562
Georg Brandl46b9afc2010-07-30 09:14:20 +0000563def pdb_invoke(method, arg):
564 """Run pdb.method(arg)."""
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700565 getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000566
567
568def test_pdb_run_with_incorrect_argument():
569 """Testing run and runeval with incorrect first argument.
570
571 >>> pti = PdbTestInput(['continue',])
572 >>> with pti:
573 ... pdb_invoke('run', lambda x: x)
574 Traceback (most recent call last):
575 TypeError: exec() arg 1 must be a string, bytes or code object
576
577 >>> with pti:
578 ... pdb_invoke('runeval', lambda x: x)
579 Traceback (most recent call last):
580 TypeError: eval() arg 1 must be a string, bytes or code object
581 """
582
583
584def test_pdb_run_with_code_object():
585 """Testing run and runeval with code object as a first argument.
586
Georg Brandle1e8df12010-07-31 08:14:16 +0000587 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000588 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000589 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000590 (Pdb) step
591 --Return--
592 > <string>(1)<module>()->None
593 (Pdb) x
594 1
595 (Pdb) continue
596
597 >>> with PdbTestInput(['x', 'continue']):
598 ... x=0
599 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
600 > <string>(1)<module>()->None
601 (Pdb) x
602 1
603 (Pdb) continue
604 """
605
Guido van Rossum8820c232013-11-21 11:30:06 -0800606def test_next_until_return_at_return_event():
607 """Test that pdb stops after a next/until/return issued at a return debug event.
608
609 >>> def test_function_2():
610 ... x = 1
611 ... x = 2
612
613 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700614 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800615 ... test_function_2()
616 ... test_function_2()
617 ... test_function_2()
618 ... end = 1
619
Antoine Pitrouc04d4682014-08-11 21:40:38 -0400620 >>> from bdb import Breakpoint
621 >>> Breakpoint.next = 1
Guido van Rossum8820c232013-11-21 11:30:06 -0800622 >>> with PdbTestInput(['break test_function_2',
623 ... 'continue',
624 ... 'return',
625 ... 'next',
626 ... 'continue',
627 ... 'return',
628 ... 'until',
629 ... 'continue',
630 ... 'return',
631 ... 'return',
632 ... 'continue']):
633 ... test_function()
634 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
635 -> test_function_2()
636 (Pdb) break test_function_2
637 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
638 (Pdb) continue
639 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
640 -> x = 1
641 (Pdb) return
642 --Return--
643 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
644 -> x = 2
645 (Pdb) next
646 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
647 -> test_function_2()
648 (Pdb) continue
649 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
650 -> x = 1
651 (Pdb) return
652 --Return--
653 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
654 -> x = 2
655 (Pdb) until
656 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
657 -> test_function_2()
658 (Pdb) continue
659 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
660 -> x = 1
661 (Pdb) return
662 --Return--
663 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
664 -> x = 2
665 (Pdb) return
666 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
667 -> end = 1
668 (Pdb) continue
669 """
670
671def test_pdb_next_command_for_generator():
672 """Testing skip unwindng stack on yield for generators for "next" command
673
674 >>> def test_gen():
675 ... yield 0
676 ... return 1
677 ... yield 2
678
679 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700680 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800681 ... it = test_gen()
682 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300683 ... if next(it) != 0:
684 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800685 ... next(it)
686 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300687 ... if ex.value != 1:
688 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800689 ... print("finished")
690
691 >>> with PdbTestInput(['step',
692 ... 'step',
693 ... 'step',
694 ... 'next',
695 ... 'next',
696 ... 'step',
697 ... 'step',
698 ... 'continue']):
699 ... test_function()
700 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
701 -> it = test_gen()
702 (Pdb) step
703 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
704 -> try:
705 (Pdb) step
706 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300707 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800708 (Pdb) step
709 --Call--
710 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
711 -> def test_gen():
712 (Pdb) next
713 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
714 -> yield 0
715 (Pdb) next
716 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
717 -> return 1
718 (Pdb) step
719 --Return--
720 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
721 -> return 1
722 (Pdb) step
723 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300724 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800725 -> next(it)
726 (Pdb) continue
727 finished
728 """
729
Pablo Galindo46877022018-01-29 00:25:05 +0000730def test_pdb_next_command_for_coroutine():
731 """Testing skip unwindng stack on yield for coroutines for "next" command
732
733 >>> import asyncio
734
735 >>> async def test_coro():
736 ... await asyncio.sleep(0)
737 ... await asyncio.sleep(0)
738 ... await asyncio.sleep(0)
739
740 >>> async def test_main():
741 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
742 ... await test_coro()
743
744 >>> def test_function():
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000745 ... loop = asyncio.new_event_loop()
Pablo Galindo46877022018-01-29 00:25:05 +0000746 ... loop.run_until_complete(test_main())
747 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700748 ... asyncio.set_event_loop_policy(None)
Pablo Galindo46877022018-01-29 00:25:05 +0000749 ... print("finished")
750
751 >>> with PdbTestInput(['step',
752 ... 'step',
753 ... 'next',
754 ... 'next',
755 ... 'next',
756 ... 'step',
757 ... 'continue']):
758 ... test_function()
759 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
760 -> await test_coro()
761 (Pdb) step
762 --Call--
763 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
764 -> async def test_coro():
765 (Pdb) step
766 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
767 -> await asyncio.sleep(0)
768 (Pdb) next
769 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
770 -> await asyncio.sleep(0)
771 (Pdb) next
772 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
773 -> await asyncio.sleep(0)
774 (Pdb) next
775 Internal StopIteration
776 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
777 -> await test_coro()
778 (Pdb) step
779 --Return--
780 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
781 -> await test_coro()
782 (Pdb) continue
783 finished
784 """
785
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500786def test_pdb_next_command_for_asyncgen():
787 """Testing skip unwindng stack on yield for coroutines for "next" command
788
789 >>> import asyncio
790
791 >>> async def agen():
792 ... yield 1
793 ... await asyncio.sleep(0)
794 ... yield 2
795
796 >>> async def test_coro():
797 ... async for x in agen():
798 ... print(x)
799
800 >>> async def test_main():
801 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
802 ... await test_coro()
803
804 >>> def test_function():
805 ... loop = asyncio.new_event_loop()
806 ... loop.run_until_complete(test_main())
807 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700808 ... asyncio.set_event_loop_policy(None)
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500809 ... print("finished")
810
811 >>> with PdbTestInput(['step',
812 ... 'step',
813 ... 'next',
814 ... 'next',
815 ... 'step',
816 ... 'next',
817 ... 'continue']):
818 ... test_function()
819 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main()
820 -> await test_coro()
821 (Pdb) step
822 --Call--
823 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro()
824 -> async def test_coro():
825 (Pdb) step
826 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
827 -> async for x in agen():
828 (Pdb) next
829 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro()
830 -> print(x)
831 (Pdb) next
832 1
833 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
834 -> async for x in agen():
835 (Pdb) step
836 --Call--
837 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen()
838 -> yield 1
839 (Pdb) next
840 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen()
841 -> await asyncio.sleep(0)
842 (Pdb) continue
843 2
844 finished
845 """
846
Guido van Rossum8820c232013-11-21 11:30:06 -0800847def test_pdb_return_command_for_generator():
848 """Testing no unwindng stack on yield for generators
849 for "return" command
850
851 >>> def test_gen():
852 ... yield 0
853 ... return 1
854 ... yield 2
855
856 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700857 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800858 ... it = test_gen()
859 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300860 ... if next(it) != 0:
861 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800862 ... next(it)
863 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300864 ... if ex.value != 1:
865 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800866 ... print("finished")
867
868 >>> with PdbTestInput(['step',
869 ... 'step',
870 ... 'step',
871 ... 'return',
872 ... 'step',
873 ... 'step',
874 ... 'continue']):
875 ... test_function()
876 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
877 -> it = test_gen()
878 (Pdb) step
879 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
880 -> try:
881 (Pdb) step
882 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300883 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800884 (Pdb) step
885 --Call--
886 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
887 -> def test_gen():
888 (Pdb) return
889 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300890 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800891 -> next(it)
892 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300893 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800894 -> except StopIteration as ex:
895 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300896 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
897 -> if ex.value != 1:
Guido van Rossum8820c232013-11-21 11:30:06 -0800898 (Pdb) continue
899 finished
900 """
901
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000902def test_pdb_return_command_for_coroutine():
903 """Testing no unwindng stack on yield for coroutines for "return" command
904
905 >>> import asyncio
906
907 >>> async def test_coro():
908 ... await asyncio.sleep(0)
909 ... await asyncio.sleep(0)
910 ... await asyncio.sleep(0)
911
912 >>> async def test_main():
913 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
914 ... await test_coro()
915
916 >>> def test_function():
917 ... loop = asyncio.new_event_loop()
918 ... loop.run_until_complete(test_main())
919 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700920 ... asyncio.set_event_loop_policy(None)
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000921 ... print("finished")
922
923 >>> with PdbTestInput(['step',
924 ... 'step',
925 ... 'next',
926 ... 'continue']):
927 ... test_function()
928 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main()
929 -> await test_coro()
930 (Pdb) step
931 --Call--
932 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro()
933 -> async def test_coro():
934 (Pdb) step
935 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro()
936 -> await asyncio.sleep(0)
937 (Pdb) next
938 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro()
939 -> await asyncio.sleep(0)
940 (Pdb) continue
941 finished
942 """
943
Guido van Rossum8820c232013-11-21 11:30:06 -0800944def test_pdb_until_command_for_generator():
945 """Testing no unwindng stack on yield for generators
946 for "until" command if target breakpoing is not reached
947
948 >>> def test_gen():
949 ... yield 0
950 ... yield 1
951 ... yield 2
952
953 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700954 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800955 ... for i in test_gen():
956 ... print(i)
957 ... print("finished")
958
959 >>> with PdbTestInput(['step',
960 ... 'until 4',
961 ... 'step',
962 ... 'step',
963 ... 'continue']):
964 ... test_function()
965 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
966 -> for i in test_gen():
967 (Pdb) step
968 --Call--
969 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
970 -> def test_gen():
971 (Pdb) until 4
972 0
973 1
974 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
975 -> yield 2
976 (Pdb) step
977 --Return--
978 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
979 -> yield 2
980 (Pdb) step
981 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
982 -> print(i)
983 (Pdb) continue
984 2
985 finished
986 """
987
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +0200988def test_pdb_until_command_for_coroutine():
989 """Testing no unwindng stack for coroutines
990 for "until" command if target breakpoing is not reached
991
992 >>> import asyncio
993
994 >>> async def test_coro():
995 ... print(0)
996 ... await asyncio.sleep(0)
997 ... print(1)
998 ... await asyncio.sleep(0)
999 ... print(2)
1000 ... await asyncio.sleep(0)
1001 ... print(3)
1002
1003 >>> async def test_main():
1004 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1005 ... await test_coro()
1006
1007 >>> def test_function():
1008 ... loop = asyncio.new_event_loop()
1009 ... loop.run_until_complete(test_main())
1010 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -07001011 ... asyncio.set_event_loop_policy(None)
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001012 ... print("finished")
1013
1014 >>> with PdbTestInput(['step',
1015 ... 'until 8',
1016 ... 'continue']):
1017 ... test_function()
1018 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main()
1019 -> await test_coro()
1020 (Pdb) step
1021 --Call--
1022 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro()
1023 -> async def test_coro():
1024 (Pdb) until 8
1025 0
1026 1
1027 2
1028 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro()
1029 -> print(3)
1030 (Pdb) continue
1031 3
1032 finished
1033 """
1034
Guido van Rossum8820c232013-11-21 11:30:06 -08001035def test_pdb_next_command_in_generator_for_loop():
Martin Panter46f50722016-05-26 05:35:26 +00001036 """The next command on returning from a generator controlled by a for loop.
Guido van Rossum8820c232013-11-21 11:30:06 -08001037
1038 >>> def test_gen():
1039 ... yield 0
1040 ... return 1
1041
1042 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001043 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001044 ... for i in test_gen():
1045 ... print('value', i)
1046 ... x = 123
1047
1048 >>> with PdbTestInput(['break test_gen',
1049 ... 'continue',
1050 ... 'next',
1051 ... 'next',
1052 ... 'next',
1053 ... 'continue']):
1054 ... test_function()
1055 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1056 -> for i in test_gen():
1057 (Pdb) break test_gen
1058 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
1059 (Pdb) continue
1060 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
1061 -> yield 0
1062 (Pdb) next
1063 value 0
1064 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
1065 -> return 1
1066 (Pdb) next
1067 Internal StopIteration: 1
1068 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1069 -> for i in test_gen():
1070 (Pdb) next
1071 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
1072 -> x = 123
1073 (Pdb) continue
1074 """
1075
1076def test_pdb_next_command_subiterator():
1077 """The next command in a generator with a subiterator.
1078
1079 >>> def test_subgenerator():
1080 ... yield 0
1081 ... return 1
1082
1083 >>> def test_gen():
1084 ... x = yield from test_subgenerator()
1085 ... return x
1086
1087 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001088 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001089 ... for i in test_gen():
1090 ... print('value', i)
1091 ... x = 123
1092
1093 >>> with PdbTestInput(['step',
1094 ... 'step',
1095 ... 'next',
1096 ... 'next',
1097 ... 'next',
1098 ... 'continue']):
1099 ... test_function()
1100 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1101 -> for i in test_gen():
1102 (Pdb) step
1103 --Call--
1104 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
1105 -> def test_gen():
1106 (Pdb) step
1107 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
1108 -> x = yield from test_subgenerator()
1109 (Pdb) next
1110 value 0
1111 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
1112 -> return x
1113 (Pdb) next
1114 Internal StopIteration: 1
1115 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1116 -> for i in test_gen():
1117 (Pdb) next
1118 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
1119 -> x = 123
1120 (Pdb) continue
1121 """
1122
Xavier de Gaye10e54ae2016-10-12 20:13:24 +02001123def test_pdb_issue_20766():
1124 """Test for reference leaks when the SIGINT handler is set.
1125
1126 >>> def test_function():
1127 ... i = 1
1128 ... while i <= 2:
1129 ... sess = pdb.Pdb()
1130 ... sess.set_trace(sys._getframe())
1131 ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1132 ... i += 1
1133
1134 >>> with PdbTestInput(['continue',
1135 ... 'continue']):
1136 ... test_function()
1137 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
1138 -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1139 (Pdb) continue
1140 pdb 1: <built-in function default_int_handler>
1141 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
1142 -> sess.set_trace(sys._getframe())
1143 (Pdb) continue
1144 pdb 2: <built-in function default_int_handler>
1145 """
Georg Brandl46b9afc2010-07-30 09:14:20 +00001146
Georg Brandl6cccb862010-07-30 14:16:43 +00001147
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001148class PdbTestCase(unittest.TestCase):
1149 def tearDown(self):
1150 support.unlink(support.TESTFN)
1151
1152 def _run_pdb(self, pdb_args, commands):
1153 self.addCleanup(support.rmtree, '__pycache__')
1154 cmd = [sys.executable, '-m', 'pdb'] + pdb_args
1155 with subprocess.Popen(
1156 cmd,
1157 stdout=subprocess.PIPE,
1158 stdin=subprocess.PIPE,
1159 stderr=subprocess.STDOUT,
1160 ) as proc:
1161 stdout, stderr = proc.communicate(str.encode(commands))
1162 stdout = stdout and bytes.decode(stdout)
1163 stderr = stderr and bytes.decode(stderr)
1164 return stdout, stderr
1165
1166 def run_pdb_script(self, script, commands):
Senthil Kumaran42d70812012-05-01 10:07:49 +08001167 """Run 'script' lines with pdb and the pdb 'commands'."""
1168 filename = 'main.py'
1169 with open(filename, 'w') as f:
1170 f.write(textwrap.dedent(script))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001171 self.addCleanup(support.unlink, filename)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001172 return self._run_pdb([filename], commands)
1173
1174 def run_pdb_module(self, script, commands):
1175 """Runs the script code as part of a module"""
1176 self.module_name = 't_main'
1177 support.rmtree(self.module_name)
1178 main_file = self.module_name + '/__main__.py'
1179 init_file = self.module_name + '/__init__.py'
1180 os.mkdir(self.module_name)
1181 with open(init_file, 'w') as f:
1182 pass
1183 with open(main_file, 'w') as f:
1184 f.write(textwrap.dedent(script))
1185 self.addCleanup(support.rmtree, self.module_name)
1186 return self._run_pdb(['-m', self.module_name], commands)
Senthil Kumaran42d70812012-05-01 10:07:49 +08001187
Georg Brandl6e220552013-10-13 20:51:47 +02001188 def _assert_find_function(self, file_content, func_name, expected):
1189 file_content = textwrap.dedent(file_content)
1190
1191 with open(support.TESTFN, 'w') as f:
1192 f.write(file_content)
1193
1194 expected = None if not expected else (
1195 expected[0], support.TESTFN, expected[1])
1196 self.assertEqual(
1197 expected, pdb.find_function(func_name, support.TESTFN))
1198
1199 def test_find_function_empty_file(self):
1200 self._assert_find_function('', 'foo', None)
1201
1202 def test_find_function_found(self):
1203 self._assert_find_function(
1204 """\
1205 def foo():
1206 pass
1207
1208 def bar():
1209 pass
1210
1211 def quux():
1212 pass
1213 """,
1214 'bar',
1215 ('bar', 4),
1216 )
1217
Georg Brandl6cccb862010-07-30 14:16:43 +00001218 def test_issue7964(self):
1219 # open the file as binary so we can force \r\n newline
1220 with open(support.TESTFN, 'wb') as f:
1221 f.write(b'print("testing my pdb")\r\n')
1222 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
1223 proc = subprocess.Popen(cmd,
1224 stdout=subprocess.PIPE,
1225 stdin=subprocess.PIPE,
1226 stderr=subprocess.STDOUT,
1227 )
Brian Curtin994ad6c2010-11-05 15:38:47 +00001228 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +00001229 stdout, stderr = proc.communicate(b'quit\n')
1230 self.assertNotIn(b'SyntaxError', stdout,
1231 "Got a syntax error running test script under PDB")
1232
Senthil Kumaran42d70812012-05-01 10:07:49 +08001233 def test_issue13183(self):
1234 script = """
1235 from bar import bar
1236
1237 def foo():
1238 bar()
1239
1240 def nope():
1241 pass
1242
1243 def foobar():
1244 foo()
1245 nope()
1246
1247 foobar()
1248 """
1249 commands = """
1250 from bar import bar
1251 break bar
1252 continue
1253 step
1254 step
1255 quit
1256 """
1257 bar = """
1258 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +08001259 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +08001260 """
1261 with open('bar.py', 'w') as f:
1262 f.write(textwrap.dedent(bar))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001263 self.addCleanup(support.unlink, 'bar.py')
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001264 stdout, stderr = self.run_pdb_script(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001265 self.assertTrue(
1266 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1267 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001268
Daniel Hahler9139f922019-04-01 23:59:50 +02001269 def test_issue13120(self):
1270 # Invoking "continue" on a non-main thread triggered an exception
1271 # inside signal.signal.
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001272
1273 with open(support.TESTFN, 'wb') as f:
1274 f.write(textwrap.dedent("""
1275 import threading
1276 import pdb
1277
1278 def start_pdb():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001279 pdb.Pdb(readrc=False).set_trace()
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001280 x = 1
1281 y = 1
1282
1283 t = threading.Thread(target=start_pdb)
1284 t.start()""").encode('ascii'))
1285 cmd = [sys.executable, '-u', support.TESTFN]
1286 proc = subprocess.Popen(cmd,
1287 stdout=subprocess.PIPE,
1288 stdin=subprocess.PIPE,
1289 stderr=subprocess.STDOUT,
1290 )
1291 self.addCleanup(proc.stdout.close)
1292 stdout, stderr = proc.communicate(b'cont\n')
1293 self.assertNotIn('Error', stdout.decode(),
1294 "Got an error running test script under PDB")
1295
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001296 def test_issue16180(self):
1297 # A syntax error in the debuggee.
1298 script = "def f: pass\n"
1299 commands = ''
1300 expected = "SyntaxError:"
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001301 stdout, stderr = self.run_pdb_script(script, commands)
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001302 self.assertIn(expected, stdout,
1303 '\n\nExpected:\n{}\nGot:\n{}\n'
1304 'Fail to handle a syntax error in the debuggee.'
1305 .format(expected, stdout))
1306
1307
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001308 def test_readrc_kwarg(self):
Victor Stinner11ea0442016-09-09 22:56:54 -07001309 script = textwrap.dedent("""
1310 import pdb; pdb.Pdb(readrc=False).set_trace()
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001311
Victor Stinner11ea0442016-09-09 22:56:54 -07001312 print('hello')
1313 """)
Victor Stinner11ea0442016-09-09 22:56:54 -07001314
Victor Stinnerbc626262016-09-09 23:22:09 -07001315 save_home = os.environ.pop('HOME', None)
1316 try:
1317 with support.temp_cwd():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001318 with open('.pdbrc', 'w') as f:
1319 f.write("invalid\n")
1320
1321 with open('main.py', 'w') as f:
1322 f.write(script)
1323
1324 cmd = [sys.executable, 'main.py']
1325 proc = subprocess.Popen(
1326 cmd,
1327 stdout=subprocess.PIPE,
1328 stdin=subprocess.PIPE,
1329 stderr=subprocess.PIPE,
1330 )
Victor Stinnerbc626262016-09-09 23:22:09 -07001331 with proc:
1332 stdout, stderr = proc.communicate(b'q\n')
1333 self.assertNotIn("NameError: name 'invalid' is not defined",
1334 stdout.decode())
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001335
1336 finally:
Victor Stinner11ea0442016-09-09 22:56:54 -07001337 if save_home is not None:
1338 os.environ['HOME'] = save_home
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001339
Barry Warsaw35425d62017-09-22 12:29:42 -04001340 def test_header(self):
1341 stdout = StringIO()
1342 header = 'Nobody expects... blah, blah, blah'
1343 with ExitStack() as resources:
1344 resources.enter_context(patch('sys.stdout', stdout))
1345 resources.enter_context(patch.object(pdb.Pdb, 'set_trace'))
1346 pdb.set_trace(header=header)
1347 self.assertEqual(stdout.getvalue(), header + '\n')
1348
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001349 def test_run_module(self):
1350 script = """print("SUCCESS")"""
1351 commands = """
1352 continue
1353 quit
1354 """
1355 stdout, stderr = self.run_pdb_module(script, commands)
1356 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1357
1358 def test_module_is_run_as_main(self):
1359 script = """
1360 if __name__ == '__main__':
1361 print("SUCCESS")
1362 """
1363 commands = """
1364 continue
1365 quit
1366 """
1367 stdout, stderr = self.run_pdb_module(script, commands)
1368 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1369
1370 def test_breakpoint(self):
1371 script = """
1372 if __name__ == '__main__':
1373 pass
1374 print("SUCCESS")
1375 pass
1376 """
1377 commands = """
1378 b 3
1379 quit
1380 """
1381 stdout, stderr = self.run_pdb_module(script, commands)
1382 self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout)
1383 self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout)
1384
1385 def test_run_pdb_with_pdb(self):
1386 commands = """
1387 c
1388 quit
1389 """
1390 stdout, stderr = self._run_pdb(["-m", "pdb"], commands)
Mario Corcherofcf8b4c2018-01-28 04:58:47 +00001391 self.assertIn(
1392 pdb._usage,
1393 stdout.replace('\r', '') # remove \r for windows
1394 )
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001395
1396 def test_module_without_a_main(self):
1397 module_name = 't_main'
1398 support.rmtree(module_name)
1399 init_file = module_name + '/__init__.py'
1400 os.mkdir(module_name)
1401 with open(init_file, 'w') as f:
1402 pass
1403 self.addCleanup(support.rmtree, module_name)
1404 stdout, stderr = self._run_pdb(['-m', module_name], "")
1405 self.assertIn("ImportError: No module named t_main.__main__",
1406 stdout.splitlines())
1407
1408 def test_blocks_at_first_code_line(self):
1409 script = """
1410 #This is a comment, on line 2
1411
1412 print("SUCCESS")
1413 """
1414 commands = """
1415 quit
1416 """
1417 stdout, stderr = self.run_pdb_module(script, commands)
1418 self.assertTrue(any("__main__.py(4)<module>()"
1419 in l for l in stdout.splitlines()), stdout)
1420
1421 def test_relative_imports(self):
1422 self.module_name = 't_main'
1423 support.rmtree(self.module_name)
1424 main_file = self.module_name + '/__main__.py'
1425 init_file = self.module_name + '/__init__.py'
1426 module_file = self.module_name + '/module.py'
1427 self.addCleanup(support.rmtree, self.module_name)
1428 os.mkdir(self.module_name)
1429 with open(init_file, 'w') as f:
1430 f.write(textwrap.dedent("""
1431 top_var = "VAR from top"
1432 """))
1433 with open(main_file, 'w') as f:
1434 f.write(textwrap.dedent("""
1435 from . import top_var
1436 from .module import var
1437 from . import module
1438 pass # We'll stop here and print the vars
1439 """))
1440 with open(module_file, 'w') as f:
1441 f.write(textwrap.dedent("""
1442 var = "VAR from module"
1443 var2 = "second var"
1444 """))
1445 commands = """
1446 b 5
1447 c
1448 p top_var
1449 p var
1450 p module.var2
1451 quit
1452 """
1453 stdout, _ = self._run_pdb(['-m', self.module_name], commands)
Mario Corchero38bfa842018-02-03 06:40:11 +00001454 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001455 self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
1456 self.assertTrue(any("second var" in l for l in stdout.splitlines()))
Georg Brandl6cccb862010-07-30 14:16:43 +00001457
Mario Corchero38bfa842018-02-03 06:40:11 +00001458 def test_relative_imports_on_plain_module(self):
1459 # Validates running a plain module. See bpo32691
1460 self.module_name = 't_main'
1461 support.rmtree(self.module_name)
1462 main_file = self.module_name + '/runme.py'
1463 init_file = self.module_name + '/__init__.py'
1464 module_file = self.module_name + '/module.py'
1465 self.addCleanup(support.rmtree, self.module_name)
1466 os.mkdir(self.module_name)
1467 with open(init_file, 'w') as f:
1468 f.write(textwrap.dedent("""
1469 top_var = "VAR from top"
1470 """))
1471 with open(main_file, 'w') as f:
1472 f.write(textwrap.dedent("""
1473 from . import module
1474 pass # We'll stop here and print the vars
1475 """))
1476 with open(module_file, 'w') as f:
1477 f.write(textwrap.dedent("""
1478 var = "VAR from module"
1479 """))
1480 commands = """
1481 b 3
1482 c
1483 p module.var
1484 quit
1485 """
1486 stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
1487 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
1488
Daniel Hahler3e936432019-03-12 04:29:04 +01001489 def test_errors_in_command(self):
1490 commands = "\n".join([
1491 'print(',
1492 'debug print(',
1493 'debug doesnotexist',
1494 'c',
1495 ])
1496 stdout, _ = self.run_pdb_script('', commands + '\n')
1497
Daniel Hahler43277052019-02-15 21:52:53 +01001498 self.assertEqual(stdout.splitlines()[1:], [
1499 '(Pdb) *** SyntaxError: unexpected EOF while parsing',
Daniel Hahler3e936432019-03-12 04:29:04 +01001500
1501 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1502 '*** SyntaxError: unexpected EOF while parsing',
1503 'LEAVING RECURSIVE DEBUGGER',
1504
1505 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1506 '> <string>(1)<module>()',
1507 "((Pdb)) *** NameError: name 'doesnotexist' is not defined",
1508 'LEAVING RECURSIVE DEBUGGER',
Daniel Hahler43277052019-02-15 21:52:53 +01001509 '(Pdb) ',
1510 ])
Georg Brandl6cccb862010-07-30 14:16:43 +00001511
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001512def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +00001513 from test import test_pdb
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001514 suites = [
1515 unittest.makeSuite(PdbTestCase),
1516 doctest.DocTestSuite(test_pdb)
1517 ]
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001518 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +00001519
1520
1521if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001522 unittest.main()