blob: 1e464df28340516402b4a0b3dc5c31d0fa7082cb [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
Rémi Lapeyrebf457c72019-05-21 00:17:30 +020080 >>> def test_function3(arg=None, *, kwonly=None):
81 ... pass
82
Rémi Lapeyre45856032019-05-24 22:44:31 +020083 >>> def test_function4(a, b, c, /):
84 ... pass
85
Georg Brandl0d089622010-07-30 16:00:46 +000086 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070087 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000088 ... ret = test_function_2('baz')
Rémi Lapeyrebf457c72019-05-21 00:17:30 +020089 ... test_function3(kwonly=True)
Rémi Lapeyre45856032019-05-24 22:44:31 +020090 ... test_function4(1, 2, 3)
Georg Brandl0d089622010-07-30 16:00:46 +000091 ... print(ret)
92
93 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
94 ... 'step', # entering the function call
95 ... 'args', # display function args
96 ... 'list', # list function source
97 ... 'bt', # display backtrace
98 ... 'up', # step up to test_function()
99 ... 'down', # step down to test_function_2() again
100 ... 'next', # stepping to print(foo)
101 ... 'next', # stepping to the for loop
102 ... 'step', # stepping into the for loop
103 ... 'until', # continuing until out of the for loop
104 ... 'next', # executing the print(bar)
105 ... 'jump 8', # jump over second for loop
106 ... 'return', # return out of function
107 ... 'retval', # display return value
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200108 ... 'next', # step to test_function3()
109 ... 'step', # stepping into test_function3()
110 ... 'args', # display function args
Rémi Lapeyre45856032019-05-24 22:44:31 +0200111 ... 'return', # return out of function
112 ... 'next', # step to test_function4()
113 ... 'step', # stepping to test_function4()
114 ... 'args', # display function args
Georg Brandl0d089622010-07-30 16:00:46 +0000115 ... 'continue',
116 ... ]):
117 ... test_function()
Rémi Lapeyre45856032019-05-24 22:44:31 +0200118 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000119 -> ret = test_function_2('baz')
120 (Pdb) step
121 --Call--
122 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
123 -> def test_function_2(foo, bar='default'):
124 (Pdb) args
125 foo = 'baz'
126 bar = 'default'
127 (Pdb) list
128 1 -> def test_function_2(foo, bar='default'):
129 2 print(foo)
130 3 for i in range(5):
131 4 print(i)
132 5 print(bar)
133 6 for i in range(10):
134 7 never_executed
135 8 print('after for')
136 9 print('...')
137 10 return foo.upper()
138 [EOF]
139 (Pdb) bt
140 ...
Rémi Lapeyre45856032019-05-24 22:44:31 +0200141 <doctest test.test_pdb.test_pdb_basic_commands[4]>(25)<module>()
Georg Brandl0d089622010-07-30 16:00:46 +0000142 -> test_function()
Rémi Lapeyre45856032019-05-24 22:44:31 +0200143 <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000144 -> ret = test_function_2('baz')
145 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
146 -> def test_function_2(foo, bar='default'):
147 (Pdb) up
Rémi Lapeyre45856032019-05-24 22:44:31 +0200148 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000149 -> ret = test_function_2('baz')
150 (Pdb) down
151 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
152 -> def test_function_2(foo, bar='default'):
153 (Pdb) next
154 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
155 -> print(foo)
156 (Pdb) next
157 baz
158 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
159 -> for i in range(5):
160 (Pdb) step
161 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
162 -> print(i)
163 (Pdb) until
164 0
165 1
166 2
167 3
168 4
169 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
170 -> print(bar)
171 (Pdb) next
172 default
173 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
174 -> for i in range(10):
175 (Pdb) jump 8
176 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
177 -> print('after for')
178 (Pdb) return
179 after for
180 ...
181 --Return--
182 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
183 -> return foo.upper()
184 (Pdb) retval
185 'BAZ'
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200186 (Pdb) next
Rémi Lapeyre45856032019-05-24 22:44:31 +0200187 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(4)test_function()
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200188 -> test_function3(kwonly=True)
189 (Pdb) step
190 --Call--
191 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(1)test_function3()
192 -> def test_function3(arg=None, *, kwonly=None):
193 (Pdb) args
194 arg = None
195 kwonly = True
Rémi Lapeyre45856032019-05-24 22:44:31 +0200196 (Pdb) return
197 --Return--
198 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(2)test_function3()->None
199 -> pass
200 (Pdb) next
201 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(5)test_function()
202 -> test_function4(1, 2, 3)
203 (Pdb) step
204 --Call--
205 > <doctest test.test_pdb.test_pdb_basic_commands[2]>(1)test_function4()
206 -> def test_function4(a, b, c, /):
207 (Pdb) args
208 a = 1
209 b = 2
210 c = 3
Georg Brandl0d089622010-07-30 16:00:46 +0000211 (Pdb) continue
212 BAZ
213 """
214
215
216def test_pdb_breakpoint_commands():
217 """Test basic commands related to breakpoints.
218
219 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700220 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000221 ... print(1)
222 ... print(2)
223 ... print(3)
224 ... print(4)
225
226 First, need to clear bdb state that might be left over from previous tests.
227 Otherwise, the new breakpoints might get assigned different numbers.
228
229 >>> from bdb import Breakpoint
230 >>> Breakpoint.next = 1
231 >>> Breakpoint.bplist = {}
232 >>> Breakpoint.bpbynumber = [None]
233
234 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
235 the breakpoint list outputs a tab for the "stop only" and "ignore next"
236 lines, which we don't want to put in here.
237
238 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
239 ... 'break 3',
240 ... 'disable 1',
241 ... 'ignore 1 10',
242 ... 'condition 1 1 < 2',
243 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000244 ... 'break 4',
245 ... 'break',
246 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000247 ... 'break',
248 ... 'condition 1',
249 ... 'enable 1',
250 ... 'clear 1',
251 ... 'commands 2',
R David Murray78d692f2013-10-10 17:23:26 -0400252 ... 'p "42"',
253 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
Georg Brandl0d089622010-07-30 16:00:46 +0000254 ... 'end',
255 ... 'continue', # will stop at breakpoint 2 (line 4)
256 ... 'clear', # clear all!
257 ... 'y',
258 ... 'tbreak 5',
259 ... 'continue', # will stop at temporary breakpoint
260 ... 'break', # make sure breakpoint is gone
261 ... 'continue',
262 ... ]):
263 ... test_function()
264 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
265 -> print(1)
266 (Pdb) break 3
267 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
268 (Pdb) disable 1
269 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
270 (Pdb) ignore 1 10
271 Will ignore next 10 crossings of breakpoint 1.
272 (Pdb) condition 1 1 < 2
273 New condition set for breakpoint 1.
274 (Pdb) break 4
275 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000276 (Pdb) break 4
277 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
278 (Pdb) break
279 Num Type Disp Enb Where
280 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
281 stop only if 1 < 2
282 ignore next 10 hits
283 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
284 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
285 (Pdb) clear 3
286 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000287 (Pdb) break
288 Num Type Disp Enb Where
289 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
290 stop only if 1 < 2
291 ignore next 10 hits
292 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
293 (Pdb) condition 1
294 Breakpoint 1 is now unconditional.
295 (Pdb) enable 1
296 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
297 (Pdb) clear 1
298 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
299 (Pdb) commands 2
R David Murray78d692f2013-10-10 17:23:26 -0400300 (com) p "42"
301 (com) print("42", 7*6)
Georg Brandl0d089622010-07-30 16:00:46 +0000302 (com) end
303 (Pdb) continue
304 1
R David Murray78d692f2013-10-10 17:23:26 -0400305 '42'
306 42 42
Georg Brandl0d089622010-07-30 16:00:46 +0000307 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
308 -> print(2)
309 (Pdb) clear
310 Clear all breaks? y
311 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
312 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000313 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000314 (Pdb) continue
315 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000316 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000317 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
318 -> print(3)
319 (Pdb) break
320 (Pdb) continue
321 3
322 4
323 """
324
325
Georg Brandle59ca2a2010-07-30 17:04:28 +0000326def do_nothing():
327 pass
328
329def do_something():
330 print(42)
331
332def test_list_commands():
333 """Test the list and source commands of pdb.
334
335 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000336 ... import test.test_pdb
337 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000338 ... 'some...'
339 ... 'more...'
340 ... 'code...'
341 ... 'to...'
342 ... 'make...'
343 ... 'a...'
344 ... 'long...'
345 ... 'listing...'
346 ... 'useful...'
347 ... '...'
348 ... '...'
349 ... return foo
350
351 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700352 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000353 ... ret = test_function_2('baz')
354
355 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
356 ... 'list', # list first function
357 ... 'step', # step into second function
358 ... 'list', # list second function
359 ... 'list', # continue listing to EOF
360 ... 'list 1,3', # list specific lines
361 ... 'list x', # invalid argument
362 ... 'next', # step to import
363 ... 'next', # step over import
364 ... 'step', # step into do_nothing
365 ... 'longlist', # list all lines
366 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000367 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000368 ... 'continue',
369 ... ]):
370 ... test_function()
371 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
372 -> ret = test_function_2('baz')
373 (Pdb) list
374 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700375 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000376 3 -> ret = test_function_2('baz')
377 [EOF]
378 (Pdb) step
379 --Call--
380 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
381 -> def test_function_2(foo):
382 (Pdb) list
383 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000384 2 import test.test_pdb
385 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000386 4 'some...'
387 5 'more...'
388 6 'code...'
389 7 'to...'
390 8 'make...'
391 9 'a...'
392 10 'long...'
393 11 'listing...'
394 (Pdb) list
395 12 'useful...'
396 13 '...'
397 14 '...'
398 15 return foo
399 [EOF]
400 (Pdb) list 1,3
401 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000402 2 import test.test_pdb
403 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000404 (Pdb) list x
405 *** ...
406 (Pdb) next
407 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000408 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000409 (Pdb) next
410 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000411 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000412 (Pdb) step
413 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000414 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000415 -> def do_nothing():
416 (Pdb) longlist
417 ... -> def do_nothing():
418 ... pass
419 (Pdb) source do_something
420 ... def do_something():
421 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000422 (Pdb) source fooxxx
423 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000424 (Pdb) continue
425 """
426
427
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000428def test_post_mortem():
429 """Test post mortem traceback debugging.
430
431 >>> def test_function_2():
432 ... try:
433 ... 1/0
434 ... finally:
435 ... print('Exception!')
436
437 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700438 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000439 ... test_function_2()
440 ... print('Not reached.')
441
442 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
443 ... 'next', # step over exception-raising call
444 ... 'bt', # get a backtrace
445 ... 'list', # list code of test_function()
446 ... 'down', # step into test_function_2()
447 ... 'list', # list code of test_function_2()
448 ... 'continue',
449 ... ]):
450 ... try:
451 ... test_function()
452 ... except ZeroDivisionError:
453 ... print('Correctly reraised.')
454 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
455 -> test_function_2()
456 (Pdb) next
457 Exception!
458 ZeroDivisionError: division by zero
459 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
460 -> test_function_2()
461 (Pdb) bt
462 ...
463 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
464 -> test_function()
465 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
466 -> test_function_2()
467 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
468 -> 1/0
469 (Pdb) list
470 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700471 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000472 3 -> test_function_2()
473 4 print('Not reached.')
474 [EOF]
475 (Pdb) down
476 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
477 -> 1/0
478 (Pdb) list
479 1 def test_function_2():
480 2 try:
481 3 >> 1/0
482 4 finally:
483 5 -> print('Exception!')
484 [EOF]
485 (Pdb) continue
486 Correctly reraised.
487 """
488
489
Georg Brandl243ad662009-05-05 09:00:19 +0000490def test_pdb_skip_modules():
491 """This illustrates the simple case of module skipping.
492
493 >>> def skip_module():
494 ... import string
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700495 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000496 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000497
Georg Brandl9fa2e022009-09-16 16:40:45 +0000498 >>> with PdbTestInput([
499 ... 'step',
500 ... 'continue',
501 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000502 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000503 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
504 -> string.capwords('FOO')
505 (Pdb) step
506 --Return--
507 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
508 -> string.capwords('FOO')
509 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000510 """
Georg Brandl243ad662009-05-05 09:00:19 +0000511
512
513# Module for testing skipping of module that makes a callback
Brett Cannon9529fbf2013-06-15 17:11:25 -0400514mod = types.ModuleType('module_to_skip')
Georg Brandl243ad662009-05-05 09:00:19 +0000515exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
516
517
518def test_pdb_skip_modules_with_callback():
519 """This illustrates skipping of modules that call into other code.
520
521 >>> def skip_module():
522 ... def callback():
523 ... return None
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700524 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000525 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000526
Georg Brandl9fa2e022009-09-16 16:40:45 +0000527 >>> with PdbTestInput([
528 ... 'step',
529 ... 'step',
530 ... 'step',
531 ... 'step',
532 ... 'step',
533 ... 'continue',
534 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000535 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000536 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000537 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
538 -> mod.foo_pony(callback)
539 (Pdb) step
540 --Call--
541 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
542 -> def callback():
543 (Pdb) step
544 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
545 -> return None
546 (Pdb) step
547 --Return--
548 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
549 -> return None
550 (Pdb) step
551 --Return--
552 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
553 -> mod.foo_pony(callback)
554 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000555 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
556 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000557 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000558 """
Georg Brandl243ad662009-05-05 09:00:19 +0000559
560
Georg Brandl3f940892010-07-30 10:29:19 +0000561def test_pdb_continue_in_bottomframe():
562 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
563
564 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700565 ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
Georg Brandl3f940892010-07-30 10:29:19 +0000566 ... inst.set_trace()
567 ... inst.botframe = sys._getframe() # hackery to get the right botframe
568 ... print(1)
569 ... print(2)
570 ... print(3)
571 ... print(4)
572
Georg Brandl7410dd12010-07-30 12:01:20 +0000573 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000574 ... 'next',
575 ... 'break 7',
576 ... 'continue',
577 ... 'next',
578 ... 'continue',
579 ... 'continue',
580 ... ]):
581 ... test_function()
582 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
583 -> inst.botframe = sys._getframe() # hackery to get the right botframe
584 (Pdb) next
585 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
586 -> print(1)
587 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000588 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000589 (Pdb) continue
590 1
591 2
592 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
593 -> print(3)
594 (Pdb) next
595 3
596 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
597 -> print(4)
598 (Pdb) continue
599 4
600 """
601
602
Georg Brandl46b9afc2010-07-30 09:14:20 +0000603def pdb_invoke(method, arg):
604 """Run pdb.method(arg)."""
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700605 getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000606
607
608def test_pdb_run_with_incorrect_argument():
609 """Testing run and runeval with incorrect first argument.
610
611 >>> pti = PdbTestInput(['continue',])
612 >>> with pti:
613 ... pdb_invoke('run', lambda x: x)
614 Traceback (most recent call last):
615 TypeError: exec() arg 1 must be a string, bytes or code object
616
617 >>> with pti:
618 ... pdb_invoke('runeval', lambda x: x)
619 Traceback (most recent call last):
620 TypeError: eval() arg 1 must be a string, bytes or code object
621 """
622
623
624def test_pdb_run_with_code_object():
625 """Testing run and runeval with code object as a first argument.
626
Georg Brandle1e8df12010-07-31 08:14:16 +0000627 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000628 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000629 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000630 (Pdb) step
631 --Return--
632 > <string>(1)<module>()->None
633 (Pdb) x
634 1
635 (Pdb) continue
636
637 >>> with PdbTestInput(['x', 'continue']):
638 ... x=0
639 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
640 > <string>(1)<module>()->None
641 (Pdb) x
642 1
643 (Pdb) continue
644 """
645
Guido van Rossum8820c232013-11-21 11:30:06 -0800646def test_next_until_return_at_return_event():
647 """Test that pdb stops after a next/until/return issued at a return debug event.
648
649 >>> def test_function_2():
650 ... x = 1
651 ... x = 2
652
653 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700654 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800655 ... test_function_2()
656 ... test_function_2()
657 ... test_function_2()
658 ... end = 1
659
Antoine Pitrouc04d4682014-08-11 21:40:38 -0400660 >>> from bdb import Breakpoint
661 >>> Breakpoint.next = 1
Guido van Rossum8820c232013-11-21 11:30:06 -0800662 >>> with PdbTestInput(['break test_function_2',
663 ... 'continue',
664 ... 'return',
665 ... 'next',
666 ... 'continue',
667 ... 'return',
668 ... 'until',
669 ... 'continue',
670 ... 'return',
671 ... 'return',
672 ... 'continue']):
673 ... test_function()
674 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
675 -> test_function_2()
676 (Pdb) break test_function_2
677 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
678 (Pdb) continue
679 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
680 -> x = 1
681 (Pdb) return
682 --Return--
683 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
684 -> x = 2
685 (Pdb) next
686 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
687 -> test_function_2()
688 (Pdb) continue
689 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
690 -> x = 1
691 (Pdb) return
692 --Return--
693 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
694 -> x = 2
695 (Pdb) until
696 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
697 -> test_function_2()
698 (Pdb) continue
699 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
700 -> x = 1
701 (Pdb) return
702 --Return--
703 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
704 -> x = 2
705 (Pdb) return
706 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
707 -> end = 1
708 (Pdb) continue
709 """
710
711def test_pdb_next_command_for_generator():
712 """Testing skip unwindng stack on yield for generators for "next" command
713
714 >>> def test_gen():
715 ... yield 0
716 ... return 1
717 ... yield 2
718
719 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700720 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800721 ... it = test_gen()
722 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300723 ... if next(it) != 0:
724 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800725 ... next(it)
726 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300727 ... if ex.value != 1:
728 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800729 ... print("finished")
730
731 >>> with PdbTestInput(['step',
732 ... 'step',
733 ... 'step',
734 ... 'next',
735 ... 'next',
736 ... 'step',
737 ... 'step',
738 ... 'continue']):
739 ... test_function()
740 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
741 -> it = test_gen()
742 (Pdb) step
743 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
744 -> try:
745 (Pdb) step
746 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300747 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800748 (Pdb) step
749 --Call--
750 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
751 -> def test_gen():
752 (Pdb) next
753 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
754 -> yield 0
755 (Pdb) next
756 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
757 -> return 1
758 (Pdb) step
759 --Return--
760 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
761 -> return 1
762 (Pdb) step
763 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300764 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800765 -> next(it)
766 (Pdb) continue
767 finished
768 """
769
Pablo Galindo46877022018-01-29 00:25:05 +0000770def test_pdb_next_command_for_coroutine():
771 """Testing skip unwindng stack on yield for coroutines for "next" command
772
773 >>> import asyncio
774
775 >>> async def test_coro():
776 ... await asyncio.sleep(0)
777 ... await asyncio.sleep(0)
778 ... await asyncio.sleep(0)
779
780 >>> async def test_main():
781 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
782 ... await test_coro()
783
784 >>> def test_function():
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000785 ... loop = asyncio.new_event_loop()
Pablo Galindo46877022018-01-29 00:25:05 +0000786 ... loop.run_until_complete(test_main())
787 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700788 ... asyncio.set_event_loop_policy(None)
Pablo Galindo46877022018-01-29 00:25:05 +0000789 ... print("finished")
790
791 >>> with PdbTestInput(['step',
792 ... 'step',
793 ... 'next',
794 ... 'next',
795 ... 'next',
796 ... 'step',
797 ... 'continue']):
798 ... test_function()
799 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
800 -> await test_coro()
801 (Pdb) step
802 --Call--
803 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
804 -> async def test_coro():
805 (Pdb) step
806 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
807 -> await asyncio.sleep(0)
808 (Pdb) next
809 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
810 -> await asyncio.sleep(0)
811 (Pdb) next
812 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
813 -> await asyncio.sleep(0)
814 (Pdb) next
815 Internal StopIteration
816 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
817 -> await test_coro()
818 (Pdb) step
819 --Return--
820 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
821 -> await test_coro()
822 (Pdb) continue
823 finished
824 """
825
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500826def test_pdb_next_command_for_asyncgen():
827 """Testing skip unwindng stack on yield for coroutines for "next" command
828
829 >>> import asyncio
830
831 >>> async def agen():
832 ... yield 1
833 ... await asyncio.sleep(0)
834 ... yield 2
835
836 >>> async def test_coro():
837 ... async for x in agen():
838 ... print(x)
839
840 >>> async def test_main():
841 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
842 ... await test_coro()
843
844 >>> def test_function():
845 ... loop = asyncio.new_event_loop()
846 ... loop.run_until_complete(test_main())
847 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700848 ... asyncio.set_event_loop_policy(None)
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500849 ... print("finished")
850
851 >>> with PdbTestInput(['step',
852 ... 'step',
853 ... 'next',
854 ... 'next',
855 ... 'step',
856 ... 'next',
857 ... 'continue']):
858 ... test_function()
859 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main()
860 -> await test_coro()
861 (Pdb) step
862 --Call--
863 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro()
864 -> async def test_coro():
865 (Pdb) step
866 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
867 -> async for x in agen():
868 (Pdb) next
869 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro()
870 -> print(x)
871 (Pdb) next
872 1
873 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
874 -> async for x in agen():
875 (Pdb) step
876 --Call--
877 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen()
878 -> yield 1
879 (Pdb) next
880 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen()
881 -> await asyncio.sleep(0)
882 (Pdb) continue
883 2
884 finished
885 """
886
Guido van Rossum8820c232013-11-21 11:30:06 -0800887def test_pdb_return_command_for_generator():
888 """Testing no unwindng stack on yield for generators
889 for "return" command
890
891 >>> def test_gen():
892 ... yield 0
893 ... return 1
894 ... yield 2
895
896 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700897 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800898 ... it = test_gen()
899 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300900 ... if next(it) != 0:
901 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800902 ... next(it)
903 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300904 ... if ex.value != 1:
905 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800906 ... print("finished")
907
908 >>> with PdbTestInput(['step',
909 ... 'step',
910 ... 'step',
911 ... 'return',
912 ... 'step',
913 ... 'step',
914 ... 'continue']):
915 ... test_function()
916 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
917 -> it = test_gen()
918 (Pdb) step
919 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
920 -> try:
921 (Pdb) step
922 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300923 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800924 (Pdb) step
925 --Call--
926 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
927 -> def test_gen():
928 (Pdb) return
929 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300930 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800931 -> next(it)
932 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300933 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800934 -> except StopIteration as ex:
935 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300936 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
937 -> if ex.value != 1:
Guido van Rossum8820c232013-11-21 11:30:06 -0800938 (Pdb) continue
939 finished
940 """
941
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000942def test_pdb_return_command_for_coroutine():
943 """Testing no unwindng stack on yield for coroutines for "return" command
944
945 >>> import asyncio
946
947 >>> async def test_coro():
948 ... await asyncio.sleep(0)
949 ... await asyncio.sleep(0)
950 ... await asyncio.sleep(0)
951
952 >>> async def test_main():
953 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
954 ... await test_coro()
955
956 >>> def test_function():
957 ... loop = asyncio.new_event_loop()
958 ... loop.run_until_complete(test_main())
959 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700960 ... asyncio.set_event_loop_policy(None)
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000961 ... print("finished")
962
963 >>> with PdbTestInput(['step',
964 ... 'step',
965 ... 'next',
966 ... 'continue']):
967 ... test_function()
968 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main()
969 -> await test_coro()
970 (Pdb) step
971 --Call--
972 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro()
973 -> async def test_coro():
974 (Pdb) step
975 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro()
976 -> await asyncio.sleep(0)
977 (Pdb) next
978 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro()
979 -> await asyncio.sleep(0)
980 (Pdb) continue
981 finished
982 """
983
Guido van Rossum8820c232013-11-21 11:30:06 -0800984def test_pdb_until_command_for_generator():
985 """Testing no unwindng stack on yield for generators
Min ho Kimc4cacc82019-07-31 08:16:13 +1000986 for "until" command if target breakpoint is not reached
Guido van Rossum8820c232013-11-21 11:30:06 -0800987
988 >>> def test_gen():
989 ... yield 0
990 ... yield 1
991 ... yield 2
992
993 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700994 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800995 ... for i in test_gen():
996 ... print(i)
997 ... print("finished")
998
999 >>> with PdbTestInput(['step',
1000 ... 'until 4',
1001 ... 'step',
1002 ... 'step',
1003 ... 'continue']):
1004 ... test_function()
1005 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
1006 -> for i in test_gen():
1007 (Pdb) step
1008 --Call--
1009 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
1010 -> def test_gen():
1011 (Pdb) until 4
1012 0
1013 1
1014 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
1015 -> yield 2
1016 (Pdb) step
1017 --Return--
1018 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
1019 -> yield 2
1020 (Pdb) step
1021 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
1022 -> print(i)
1023 (Pdb) continue
1024 2
1025 finished
1026 """
1027
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001028def test_pdb_until_command_for_coroutine():
1029 """Testing no unwindng stack for coroutines
Min ho Kimc4cacc82019-07-31 08:16:13 +10001030 for "until" command if target breakpoint is not reached
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001031
1032 >>> import asyncio
1033
1034 >>> async def test_coro():
1035 ... print(0)
1036 ... await asyncio.sleep(0)
1037 ... print(1)
1038 ... await asyncio.sleep(0)
1039 ... print(2)
1040 ... await asyncio.sleep(0)
1041 ... print(3)
1042
1043 >>> async def test_main():
1044 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1045 ... await test_coro()
1046
1047 >>> def test_function():
1048 ... loop = asyncio.new_event_loop()
1049 ... loop.run_until_complete(test_main())
1050 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -07001051 ... asyncio.set_event_loop_policy(None)
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001052 ... print("finished")
1053
1054 >>> with PdbTestInput(['step',
1055 ... 'until 8',
1056 ... 'continue']):
1057 ... test_function()
1058 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main()
1059 -> await test_coro()
1060 (Pdb) step
1061 --Call--
1062 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro()
1063 -> async def test_coro():
1064 (Pdb) until 8
1065 0
1066 1
1067 2
1068 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro()
1069 -> print(3)
1070 (Pdb) continue
1071 3
1072 finished
1073 """
1074
Guido van Rossum8820c232013-11-21 11:30:06 -08001075def test_pdb_next_command_in_generator_for_loop():
Martin Panter46f50722016-05-26 05:35:26 +00001076 """The next command on returning from a generator controlled by a for loop.
Guido van Rossum8820c232013-11-21 11:30:06 -08001077
1078 >>> def test_gen():
1079 ... yield 0
1080 ... return 1
1081
1082 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001083 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001084 ... for i in test_gen():
1085 ... print('value', i)
1086 ... x = 123
1087
1088 >>> with PdbTestInput(['break test_gen',
1089 ... 'continue',
1090 ... 'next',
1091 ... 'next',
1092 ... 'next',
1093 ... 'continue']):
1094 ... test_function()
1095 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1096 -> for i in test_gen():
1097 (Pdb) break test_gen
1098 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
1099 (Pdb) continue
1100 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
1101 -> yield 0
1102 (Pdb) next
1103 value 0
1104 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
1105 -> return 1
1106 (Pdb) next
1107 Internal StopIteration: 1
1108 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1109 -> for i in test_gen():
1110 (Pdb) next
1111 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
1112 -> x = 123
1113 (Pdb) continue
1114 """
1115
1116def test_pdb_next_command_subiterator():
1117 """The next command in a generator with a subiterator.
1118
1119 >>> def test_subgenerator():
1120 ... yield 0
1121 ... return 1
1122
1123 >>> def test_gen():
1124 ... x = yield from test_subgenerator()
1125 ... return x
1126
1127 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001128 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001129 ... for i in test_gen():
1130 ... print('value', i)
1131 ... x = 123
1132
1133 >>> with PdbTestInput(['step',
1134 ... 'step',
1135 ... 'next',
1136 ... 'next',
1137 ... 'next',
1138 ... 'continue']):
1139 ... test_function()
1140 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1141 -> for i in test_gen():
1142 (Pdb) step
1143 --Call--
1144 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
1145 -> def test_gen():
1146 (Pdb) step
1147 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
1148 -> x = yield from test_subgenerator()
1149 (Pdb) next
1150 value 0
1151 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
1152 -> return x
1153 (Pdb) next
1154 Internal StopIteration: 1
1155 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1156 -> for i in test_gen():
1157 (Pdb) next
1158 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
1159 -> x = 123
1160 (Pdb) continue
1161 """
1162
Xavier de Gaye10e54ae2016-10-12 20:13:24 +02001163def test_pdb_issue_20766():
1164 """Test for reference leaks when the SIGINT handler is set.
1165
1166 >>> def test_function():
1167 ... i = 1
1168 ... while i <= 2:
1169 ... sess = pdb.Pdb()
1170 ... sess.set_trace(sys._getframe())
1171 ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1172 ... i += 1
1173
1174 >>> with PdbTestInput(['continue',
1175 ... 'continue']):
1176 ... test_function()
1177 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
1178 -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1179 (Pdb) continue
1180 pdb 1: <built-in function default_int_handler>
1181 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
1182 -> sess.set_trace(sys._getframe())
1183 (Pdb) continue
1184 pdb 2: <built-in function default_int_handler>
1185 """
Georg Brandl46b9afc2010-07-30 09:14:20 +00001186
Georg Brandl6cccb862010-07-30 14:16:43 +00001187
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001188class PdbTestCase(unittest.TestCase):
1189 def tearDown(self):
1190 support.unlink(support.TESTFN)
1191
1192 def _run_pdb(self, pdb_args, commands):
1193 self.addCleanup(support.rmtree, '__pycache__')
1194 cmd = [sys.executable, '-m', 'pdb'] + pdb_args
1195 with subprocess.Popen(
1196 cmd,
1197 stdout=subprocess.PIPE,
1198 stdin=subprocess.PIPE,
1199 stderr=subprocess.STDOUT,
1200 ) as proc:
1201 stdout, stderr = proc.communicate(str.encode(commands))
1202 stdout = stdout and bytes.decode(stdout)
1203 stderr = stderr and bytes.decode(stderr)
1204 return stdout, stderr
1205
1206 def run_pdb_script(self, script, commands):
Senthil Kumaran42d70812012-05-01 10:07:49 +08001207 """Run 'script' lines with pdb and the pdb 'commands'."""
1208 filename = 'main.py'
1209 with open(filename, 'w') as f:
1210 f.write(textwrap.dedent(script))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001211 self.addCleanup(support.unlink, filename)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001212 return self._run_pdb([filename], commands)
1213
1214 def run_pdb_module(self, script, commands):
1215 """Runs the script code as part of a module"""
1216 self.module_name = 't_main'
1217 support.rmtree(self.module_name)
1218 main_file = self.module_name + '/__main__.py'
1219 init_file = self.module_name + '/__init__.py'
1220 os.mkdir(self.module_name)
1221 with open(init_file, 'w') as f:
1222 pass
1223 with open(main_file, 'w') as f:
1224 f.write(textwrap.dedent(script))
1225 self.addCleanup(support.rmtree, self.module_name)
1226 return self._run_pdb(['-m', self.module_name], commands)
Senthil Kumaran42d70812012-05-01 10:07:49 +08001227
Georg Brandl6e220552013-10-13 20:51:47 +02001228 def _assert_find_function(self, file_content, func_name, expected):
1229 file_content = textwrap.dedent(file_content)
1230
1231 with open(support.TESTFN, 'w') as f:
1232 f.write(file_content)
1233
1234 expected = None if not expected else (
1235 expected[0], support.TESTFN, expected[1])
1236 self.assertEqual(
1237 expected, pdb.find_function(func_name, support.TESTFN))
1238
1239 def test_find_function_empty_file(self):
1240 self._assert_find_function('', 'foo', None)
1241
1242 def test_find_function_found(self):
1243 self._assert_find_function(
1244 """\
1245 def foo():
1246 pass
1247
1248 def bar():
1249 pass
1250
1251 def quux():
1252 pass
1253 """,
1254 'bar',
1255 ('bar', 4),
1256 )
1257
Georg Brandl6cccb862010-07-30 14:16:43 +00001258 def test_issue7964(self):
1259 # open the file as binary so we can force \r\n newline
1260 with open(support.TESTFN, 'wb') as f:
1261 f.write(b'print("testing my pdb")\r\n')
1262 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
1263 proc = subprocess.Popen(cmd,
1264 stdout=subprocess.PIPE,
1265 stdin=subprocess.PIPE,
1266 stderr=subprocess.STDOUT,
1267 )
Brian Curtin994ad6c2010-11-05 15:38:47 +00001268 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +00001269 stdout, stderr = proc.communicate(b'quit\n')
1270 self.assertNotIn(b'SyntaxError', stdout,
1271 "Got a syntax error running test script under PDB")
1272
Senthil Kumaran42d70812012-05-01 10:07:49 +08001273 def test_issue13183(self):
1274 script = """
1275 from bar import bar
1276
1277 def foo():
1278 bar()
1279
1280 def nope():
1281 pass
1282
1283 def foobar():
1284 foo()
1285 nope()
1286
1287 foobar()
1288 """
1289 commands = """
1290 from bar import bar
1291 break bar
1292 continue
1293 step
1294 step
1295 quit
1296 """
1297 bar = """
1298 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +08001299 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +08001300 """
1301 with open('bar.py', 'w') as f:
1302 f.write(textwrap.dedent(bar))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001303 self.addCleanup(support.unlink, 'bar.py')
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001304 stdout, stderr = self.run_pdb_script(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001305 self.assertTrue(
1306 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1307 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001308
Daniel Hahler9139f922019-04-01 23:59:50 +02001309 def test_issue13120(self):
1310 # Invoking "continue" on a non-main thread triggered an exception
1311 # inside signal.signal.
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001312
1313 with open(support.TESTFN, 'wb') as f:
1314 f.write(textwrap.dedent("""
1315 import threading
1316 import pdb
1317
1318 def start_pdb():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001319 pdb.Pdb(readrc=False).set_trace()
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001320 x = 1
1321 y = 1
1322
1323 t = threading.Thread(target=start_pdb)
1324 t.start()""").encode('ascii'))
1325 cmd = [sys.executable, '-u', support.TESTFN]
1326 proc = subprocess.Popen(cmd,
1327 stdout=subprocess.PIPE,
1328 stdin=subprocess.PIPE,
1329 stderr=subprocess.STDOUT,
1330 )
1331 self.addCleanup(proc.stdout.close)
1332 stdout, stderr = proc.communicate(b'cont\n')
1333 self.assertNotIn('Error', stdout.decode(),
1334 "Got an error running test script under PDB")
1335
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001336 def test_issue16180(self):
1337 # A syntax error in the debuggee.
1338 script = "def f: pass\n"
1339 commands = ''
1340 expected = "SyntaxError:"
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001341 stdout, stderr = self.run_pdb_script(script, commands)
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001342 self.assertIn(expected, stdout,
1343 '\n\nExpected:\n{}\nGot:\n{}\n'
1344 'Fail to handle a syntax error in the debuggee.'
1345 .format(expected, stdout))
1346
1347
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001348 def test_readrc_kwarg(self):
Victor Stinner11ea0442016-09-09 22:56:54 -07001349 script = textwrap.dedent("""
1350 import pdb; pdb.Pdb(readrc=False).set_trace()
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001351
Victor Stinner11ea0442016-09-09 22:56:54 -07001352 print('hello')
1353 """)
Victor Stinner11ea0442016-09-09 22:56:54 -07001354
Victor Stinnerbc626262016-09-09 23:22:09 -07001355 save_home = os.environ.pop('HOME', None)
1356 try:
1357 with support.temp_cwd():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001358 with open('.pdbrc', 'w') as f:
1359 f.write("invalid\n")
1360
1361 with open('main.py', 'w') as f:
1362 f.write(script)
1363
1364 cmd = [sys.executable, 'main.py']
1365 proc = subprocess.Popen(
1366 cmd,
1367 stdout=subprocess.PIPE,
1368 stdin=subprocess.PIPE,
1369 stderr=subprocess.PIPE,
1370 )
Victor Stinnerbc626262016-09-09 23:22:09 -07001371 with proc:
1372 stdout, stderr = proc.communicate(b'q\n')
1373 self.assertNotIn("NameError: name 'invalid' is not defined",
1374 stdout.decode())
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001375
1376 finally:
Victor Stinner11ea0442016-09-09 22:56:54 -07001377 if save_home is not None:
1378 os.environ['HOME'] = save_home
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001379
Barry Warsaw35425d62017-09-22 12:29:42 -04001380 def test_header(self):
1381 stdout = StringIO()
1382 header = 'Nobody expects... blah, blah, blah'
1383 with ExitStack() as resources:
1384 resources.enter_context(patch('sys.stdout', stdout))
1385 resources.enter_context(patch.object(pdb.Pdb, 'set_trace'))
1386 pdb.set_trace(header=header)
1387 self.assertEqual(stdout.getvalue(), header + '\n')
1388
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001389 def test_run_module(self):
1390 script = """print("SUCCESS")"""
1391 commands = """
1392 continue
1393 quit
1394 """
1395 stdout, stderr = self.run_pdb_module(script, commands)
1396 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1397
1398 def test_module_is_run_as_main(self):
1399 script = """
1400 if __name__ == '__main__':
1401 print("SUCCESS")
1402 """
1403 commands = """
1404 continue
1405 quit
1406 """
1407 stdout, stderr = self.run_pdb_module(script, commands)
1408 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1409
1410 def test_breakpoint(self):
1411 script = """
1412 if __name__ == '__main__':
1413 pass
1414 print("SUCCESS")
1415 pass
1416 """
1417 commands = """
1418 b 3
1419 quit
1420 """
1421 stdout, stderr = self.run_pdb_module(script, commands)
1422 self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout)
1423 self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout)
1424
1425 def test_run_pdb_with_pdb(self):
1426 commands = """
1427 c
1428 quit
1429 """
1430 stdout, stderr = self._run_pdb(["-m", "pdb"], commands)
Mario Corcherofcf8b4c2018-01-28 04:58:47 +00001431 self.assertIn(
1432 pdb._usage,
1433 stdout.replace('\r', '') # remove \r for windows
1434 )
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001435
1436 def test_module_without_a_main(self):
1437 module_name = 't_main'
1438 support.rmtree(module_name)
1439 init_file = module_name + '/__init__.py'
1440 os.mkdir(module_name)
1441 with open(init_file, 'w') as f:
1442 pass
1443 self.addCleanup(support.rmtree, module_name)
1444 stdout, stderr = self._run_pdb(['-m', module_name], "")
1445 self.assertIn("ImportError: No module named t_main.__main__",
1446 stdout.splitlines())
1447
1448 def test_blocks_at_first_code_line(self):
1449 script = """
1450 #This is a comment, on line 2
1451
1452 print("SUCCESS")
1453 """
1454 commands = """
1455 quit
1456 """
1457 stdout, stderr = self.run_pdb_module(script, commands)
1458 self.assertTrue(any("__main__.py(4)<module>()"
1459 in l for l in stdout.splitlines()), stdout)
1460
1461 def test_relative_imports(self):
1462 self.module_name = 't_main'
1463 support.rmtree(self.module_name)
1464 main_file = self.module_name + '/__main__.py'
1465 init_file = self.module_name + '/__init__.py'
1466 module_file = self.module_name + '/module.py'
1467 self.addCleanup(support.rmtree, self.module_name)
1468 os.mkdir(self.module_name)
1469 with open(init_file, 'w') as f:
1470 f.write(textwrap.dedent("""
1471 top_var = "VAR from top"
1472 """))
1473 with open(main_file, 'w') as f:
1474 f.write(textwrap.dedent("""
1475 from . import top_var
1476 from .module import var
1477 from . import module
1478 pass # We'll stop here and print the vars
1479 """))
1480 with open(module_file, 'w') as f:
1481 f.write(textwrap.dedent("""
1482 var = "VAR from module"
1483 var2 = "second var"
1484 """))
1485 commands = """
1486 b 5
1487 c
1488 p top_var
1489 p var
1490 p module.var2
1491 quit
1492 """
1493 stdout, _ = self._run_pdb(['-m', self.module_name], commands)
Mario Corchero38bfa842018-02-03 06:40:11 +00001494 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001495 self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
1496 self.assertTrue(any("second var" in l for l in stdout.splitlines()))
Georg Brandl6cccb862010-07-30 14:16:43 +00001497
Mario Corchero38bfa842018-02-03 06:40:11 +00001498 def test_relative_imports_on_plain_module(self):
1499 # Validates running a plain module. See bpo32691
1500 self.module_name = 't_main'
1501 support.rmtree(self.module_name)
1502 main_file = self.module_name + '/runme.py'
1503 init_file = self.module_name + '/__init__.py'
1504 module_file = self.module_name + '/module.py'
1505 self.addCleanup(support.rmtree, self.module_name)
1506 os.mkdir(self.module_name)
1507 with open(init_file, 'w') as f:
1508 f.write(textwrap.dedent("""
1509 top_var = "VAR from top"
1510 """))
1511 with open(main_file, 'w') as f:
1512 f.write(textwrap.dedent("""
1513 from . import module
1514 pass # We'll stop here and print the vars
1515 """))
1516 with open(module_file, 'w') as f:
1517 f.write(textwrap.dedent("""
1518 var = "VAR from module"
1519 """))
1520 commands = """
1521 b 3
1522 c
1523 p module.var
1524 quit
1525 """
1526 stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
1527 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
1528
Daniel Hahler3e936432019-03-12 04:29:04 +01001529 def test_errors_in_command(self):
1530 commands = "\n".join([
1531 'print(',
1532 'debug print(',
1533 'debug doesnotexist',
1534 'c',
1535 ])
1536 stdout, _ = self.run_pdb_script('', commands + '\n')
1537
Daniel Hahler43277052019-02-15 21:52:53 +01001538 self.assertEqual(stdout.splitlines()[1:], [
1539 '(Pdb) *** SyntaxError: unexpected EOF while parsing',
Daniel Hahler3e936432019-03-12 04:29:04 +01001540
1541 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1542 '*** SyntaxError: unexpected EOF while parsing',
1543 'LEAVING RECURSIVE DEBUGGER',
1544
1545 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1546 '> <string>(1)<module>()',
1547 "((Pdb)) *** NameError: name 'doesnotexist' is not defined",
1548 'LEAVING RECURSIVE DEBUGGER',
Daniel Hahler43277052019-02-15 21:52:53 +01001549 '(Pdb) ',
1550 ])
Georg Brandl6cccb862010-07-30 14:16:43 +00001551
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001552def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +00001553 from test import test_pdb
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001554 suites = [
1555 unittest.makeSuite(PdbTestCase),
1556 doctest.DocTestSuite(test_pdb)
1557 ]
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001558 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +00001559
1560
1561if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001562 unittest.main()