blob: 1a2bbb382e864263e8541df884669ce756756d83 [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
Serhiy Storchaka19fcffa2020-06-21 11:07:50 +03008import codecs
Georg Brandl6cccb862010-07-30 14:16:43 +00009import unittest
10import subprocess
Senthil Kumaran42d70812012-05-01 10:07:49 +080011import textwrap
Georg Brandl243ad662009-05-05 09:00:19 +000012
Barry Warsaw35425d62017-09-22 12:29:42 -040013from contextlib import ExitStack
14from io import StringIO
Hai Shi604bba12020-08-04 23:51:43 +080015from test.support import os_helper
Georg Brandl243ad662009-05-05 09:00:19 +000016# This little helper class is essential for testing pdb under doctest.
17from test.test_doctest import _FakeInput
Barry Warsaw35425d62017-09-22 12:29:42 -040018from unittest.mock import patch
Georg Brandl243ad662009-05-05 09:00:19 +000019
20
Georg Brandl9fa2e022009-09-16 16:40:45 +000021class PdbTestInput(object):
22 """Context manager that makes testing Pdb in doctests easier."""
23
24 def __init__(self, input):
25 self.input = input
26
27 def __enter__(self):
28 self.real_stdin = sys.stdin
29 sys.stdin = _FakeInput(self.input)
Brett Cannon31f59292011-02-21 19:29:56 +000030 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000031
32 def __exit__(self, *exc):
33 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000034 if self.orig_trace:
35 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000036
37
38def test_pdb_displayhook():
39 """This tests the custom displayhook for pdb.
40
41 >>> def test_function(foo, bar):
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070042 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000043 ... pass
44
45 >>> with PdbTestInput([
46 ... 'foo',
47 ... 'bar',
48 ... 'for i in range(5): print(i)',
49 ... 'continue',
50 ... ]):
51 ... test_function(1, None)
52 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
53 -> pass
54 (Pdb) foo
55 1
56 (Pdb) bar
57 (Pdb) for i in range(5): print(i)
58 0
59 1
60 2
61 3
62 4
63 (Pdb) continue
64 """
65
66
Georg Brandl0d089622010-07-30 16:00:46 +000067def test_pdb_basic_commands():
68 """Test the basic commands of pdb.
69
70 >>> def test_function_2(foo, bar='default'):
71 ... print(foo)
72 ... for i in range(5):
73 ... print(i)
74 ... print(bar)
75 ... for i in range(10):
76 ... never_executed
77 ... print('after for')
78 ... print('...')
79 ... return foo.upper()
80
Rémi Lapeyrebf457c72019-05-21 00:17:30 +020081 >>> def test_function3(arg=None, *, kwonly=None):
82 ... pass
83
Rémi Lapeyre45856032019-05-24 22:44:31 +020084 >>> def test_function4(a, b, c, /):
85 ... pass
86
Georg Brandl0d089622010-07-30 16:00:46 +000087 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070088 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000089 ... ret = test_function_2('baz')
Rémi Lapeyrebf457c72019-05-21 00:17:30 +020090 ... test_function3(kwonly=True)
Rémi Lapeyre45856032019-05-24 22:44:31 +020091 ... test_function4(1, 2, 3)
Georg Brandl0d089622010-07-30 16:00:46 +000092 ... print(ret)
93
94 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
95 ... 'step', # entering the function call
96 ... 'args', # display function args
97 ... 'list', # list function source
98 ... 'bt', # display backtrace
99 ... 'up', # step up to test_function()
100 ... 'down', # step down to test_function_2() again
101 ... 'next', # stepping to print(foo)
102 ... 'next', # stepping to the for loop
103 ... 'step', # stepping into the for loop
104 ... 'until', # continuing until out of the for loop
105 ... 'next', # executing the print(bar)
106 ... 'jump 8', # jump over second for loop
107 ... 'return', # return out of function
108 ... 'retval', # display return value
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200109 ... 'next', # step to test_function3()
110 ... 'step', # stepping into test_function3()
111 ... 'args', # display function args
Rémi Lapeyre45856032019-05-24 22:44:31 +0200112 ... 'return', # return out of function
113 ... 'next', # step to test_function4()
114 ... 'step', # stepping to test_function4()
115 ... 'args', # display function args
Georg Brandl0d089622010-07-30 16:00:46 +0000116 ... 'continue',
117 ... ]):
118 ... test_function()
Rémi Lapeyre45856032019-05-24 22:44:31 +0200119 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000120 -> ret = test_function_2('baz')
121 (Pdb) step
122 --Call--
123 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
124 -> def test_function_2(foo, bar='default'):
125 (Pdb) args
126 foo = 'baz'
127 bar = 'default'
128 (Pdb) list
129 1 -> def test_function_2(foo, bar='default'):
130 2 print(foo)
131 3 for i in range(5):
132 4 print(i)
133 5 print(bar)
134 6 for i in range(10):
135 7 never_executed
136 8 print('after for')
137 9 print('...')
138 10 return foo.upper()
139 [EOF]
140 (Pdb) bt
141 ...
Rémi Lapeyre45856032019-05-24 22:44:31 +0200142 <doctest test.test_pdb.test_pdb_basic_commands[4]>(25)<module>()
Georg Brandl0d089622010-07-30 16:00:46 +0000143 -> test_function()
Rémi Lapeyre45856032019-05-24 22:44:31 +0200144 <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000145 -> ret = test_function_2('baz')
146 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
147 -> def test_function_2(foo, bar='default'):
148 (Pdb) up
Rémi Lapeyre45856032019-05-24 22:44:31 +0200149 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function()
Georg Brandl0d089622010-07-30 16:00:46 +0000150 -> ret = test_function_2('baz')
151 (Pdb) down
152 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
153 -> def test_function_2(foo, bar='default'):
154 (Pdb) next
155 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
156 -> print(foo)
157 (Pdb) next
158 baz
159 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
160 -> for i in range(5):
161 (Pdb) step
162 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
163 -> print(i)
164 (Pdb) until
165 0
166 1
167 2
168 3
169 4
170 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
171 -> print(bar)
172 (Pdb) next
173 default
174 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
175 -> for i in range(10):
176 (Pdb) jump 8
177 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
178 -> print('after for')
179 (Pdb) return
180 after for
181 ...
182 --Return--
183 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
184 -> return foo.upper()
185 (Pdb) retval
186 'BAZ'
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200187 (Pdb) next
Rémi Lapeyre45856032019-05-24 22:44:31 +0200188 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(4)test_function()
Rémi Lapeyrebf457c72019-05-21 00:17:30 +0200189 -> test_function3(kwonly=True)
190 (Pdb) step
191 --Call--
192 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(1)test_function3()
193 -> def test_function3(arg=None, *, kwonly=None):
194 (Pdb) args
195 arg = None
196 kwonly = True
Rémi Lapeyre45856032019-05-24 22:44:31 +0200197 (Pdb) return
198 --Return--
199 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(2)test_function3()->None
200 -> pass
201 (Pdb) next
202 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(5)test_function()
203 -> test_function4(1, 2, 3)
204 (Pdb) step
205 --Call--
206 > <doctest test.test_pdb.test_pdb_basic_commands[2]>(1)test_function4()
207 -> def test_function4(a, b, c, /):
208 (Pdb) args
209 a = 1
210 b = 2
211 c = 3
Georg Brandl0d089622010-07-30 16:00:46 +0000212 (Pdb) continue
213 BAZ
214 """
215
216
217def test_pdb_breakpoint_commands():
218 """Test basic commands related to breakpoints.
219
220 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700221 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000222 ... print(1)
223 ... print(2)
224 ... print(3)
225 ... print(4)
226
227 First, need to clear bdb state that might be left over from previous tests.
228 Otherwise, the new breakpoints might get assigned different numbers.
229
230 >>> from bdb import Breakpoint
231 >>> Breakpoint.next = 1
232 >>> Breakpoint.bplist = {}
233 >>> Breakpoint.bpbynumber = [None]
234
235 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
236 the breakpoint list outputs a tab for the "stop only" and "ignore next"
237 lines, which we don't want to put in here.
238
239 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
240 ... 'break 3',
241 ... 'disable 1',
242 ... 'ignore 1 10',
243 ... 'condition 1 1 < 2',
244 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000245 ... 'break 4',
246 ... 'break',
247 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000248 ... 'break',
249 ... 'condition 1',
250 ... 'enable 1',
251 ... 'clear 1',
252 ... 'commands 2',
R David Murray78d692f2013-10-10 17:23:26 -0400253 ... 'p "42"',
254 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
Georg Brandl0d089622010-07-30 16:00:46 +0000255 ... 'end',
256 ... 'continue', # will stop at breakpoint 2 (line 4)
257 ... 'clear', # clear all!
258 ... 'y',
259 ... 'tbreak 5',
260 ... 'continue', # will stop at temporary breakpoint
261 ... 'break', # make sure breakpoint is gone
262 ... 'continue',
263 ... ]):
264 ... test_function()
265 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
266 -> print(1)
267 (Pdb) break 3
268 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
269 (Pdb) disable 1
270 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
271 (Pdb) ignore 1 10
272 Will ignore next 10 crossings of breakpoint 1.
273 (Pdb) condition 1 1 < 2
274 New condition set for breakpoint 1.
275 (Pdb) break 4
276 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000277 (Pdb) break 4
278 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
279 (Pdb) break
280 Num Type Disp Enb Where
281 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
282 stop only if 1 < 2
283 ignore next 10 hits
284 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
285 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
286 (Pdb) clear 3
287 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000288 (Pdb) break
289 Num Type Disp Enb Where
290 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
291 stop only if 1 < 2
292 ignore next 10 hits
293 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
294 (Pdb) condition 1
295 Breakpoint 1 is now unconditional.
296 (Pdb) enable 1
297 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
298 (Pdb) clear 1
299 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
300 (Pdb) commands 2
R David Murray78d692f2013-10-10 17:23:26 -0400301 (com) p "42"
302 (com) print("42", 7*6)
Georg Brandl0d089622010-07-30 16:00:46 +0000303 (com) end
304 (Pdb) continue
305 1
R David Murray78d692f2013-10-10 17:23:26 -0400306 '42'
307 42 42
Georg Brandl0d089622010-07-30 16:00:46 +0000308 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
309 -> print(2)
310 (Pdb) clear
311 Clear all breaks? y
312 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
313 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000314 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000315 (Pdb) continue
316 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000317 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000318 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
319 -> print(3)
320 (Pdb) break
321 (Pdb) continue
322 3
323 4
324 """
325
326
Georg Brandle59ca2a2010-07-30 17:04:28 +0000327def do_nothing():
328 pass
329
330def do_something():
331 print(42)
332
333def test_list_commands():
334 """Test the list and source commands of pdb.
335
336 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000337 ... import test.test_pdb
338 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000339 ... 'some...'
340 ... 'more...'
341 ... 'code...'
342 ... 'to...'
343 ... 'make...'
344 ... 'a...'
345 ... 'long...'
346 ... 'listing...'
347 ... 'useful...'
348 ... '...'
349 ... '...'
350 ... return foo
351
352 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700353 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000354 ... ret = test_function_2('baz')
355
356 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
357 ... 'list', # list first function
358 ... 'step', # step into second function
359 ... 'list', # list second function
360 ... 'list', # continue listing to EOF
361 ... 'list 1,3', # list specific lines
362 ... 'list x', # invalid argument
363 ... 'next', # step to import
364 ... 'next', # step over import
365 ... 'step', # step into do_nothing
366 ... 'longlist', # list all lines
367 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000368 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000369 ... 'continue',
370 ... ]):
371 ... test_function()
372 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
373 -> ret = test_function_2('baz')
374 (Pdb) list
375 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700376 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000377 3 -> ret = test_function_2('baz')
378 [EOF]
379 (Pdb) step
380 --Call--
381 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
382 -> def test_function_2(foo):
383 (Pdb) list
384 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000385 2 import test.test_pdb
386 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000387 4 'some...'
388 5 'more...'
389 6 'code...'
390 7 'to...'
391 8 'make...'
392 9 'a...'
393 10 'long...'
394 11 'listing...'
395 (Pdb) list
396 12 'useful...'
397 13 '...'
398 14 '...'
399 15 return foo
400 [EOF]
401 (Pdb) list 1,3
402 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000403 2 import test.test_pdb
404 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000405 (Pdb) list x
406 *** ...
407 (Pdb) next
408 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000409 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000410 (Pdb) next
411 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000412 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000413 (Pdb) step
414 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000415 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000416 -> def do_nothing():
417 (Pdb) longlist
418 ... -> def do_nothing():
419 ... pass
420 (Pdb) source do_something
421 ... def do_something():
422 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000423 (Pdb) source fooxxx
424 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000425 (Pdb) continue
426 """
427
428
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000429def test_post_mortem():
430 """Test post mortem traceback debugging.
431
432 >>> def test_function_2():
433 ... try:
434 ... 1/0
435 ... finally:
436 ... print('Exception!')
437
438 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700439 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000440 ... test_function_2()
441 ... print('Not reached.')
442
443 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
444 ... 'next', # step over exception-raising call
445 ... 'bt', # get a backtrace
446 ... 'list', # list code of test_function()
447 ... 'down', # step into test_function_2()
448 ... 'list', # list code of test_function_2()
449 ... 'continue',
450 ... ]):
451 ... try:
452 ... test_function()
453 ... except ZeroDivisionError:
454 ... print('Correctly reraised.')
455 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
456 -> test_function_2()
457 (Pdb) next
458 Exception!
459 ZeroDivisionError: division by zero
460 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
461 -> test_function_2()
462 (Pdb) bt
463 ...
464 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
465 -> test_function()
466 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
467 -> test_function_2()
468 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
469 -> 1/0
470 (Pdb) list
471 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700472 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000473 3 -> test_function_2()
474 4 print('Not reached.')
475 [EOF]
476 (Pdb) down
477 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
478 -> 1/0
479 (Pdb) list
480 1 def test_function_2():
481 2 try:
482 3 >> 1/0
483 4 finally:
484 5 -> print('Exception!')
485 [EOF]
486 (Pdb) continue
487 Correctly reraised.
488 """
489
490
Georg Brandl243ad662009-05-05 09:00:19 +0000491def test_pdb_skip_modules():
492 """This illustrates the simple case of module skipping.
493
494 >>> def skip_module():
495 ... import string
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700496 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000497 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000498
Georg Brandl9fa2e022009-09-16 16:40:45 +0000499 >>> with PdbTestInput([
500 ... 'step',
501 ... 'continue',
502 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000503 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000504 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
505 -> string.capwords('FOO')
506 (Pdb) step
507 --Return--
508 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
509 -> string.capwords('FOO')
510 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000511 """
Georg Brandl243ad662009-05-05 09:00:19 +0000512
513
514# Module for testing skipping of module that makes a callback
Brett Cannon9529fbf2013-06-15 17:11:25 -0400515mod = types.ModuleType('module_to_skip')
Georg Brandl243ad662009-05-05 09:00:19 +0000516exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
517
518
519def test_pdb_skip_modules_with_callback():
520 """This illustrates skipping of modules that call into other code.
521
522 >>> def skip_module():
523 ... def callback():
524 ... return None
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700525 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000526 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000527
Georg Brandl9fa2e022009-09-16 16:40:45 +0000528 >>> with PdbTestInput([
529 ... 'step',
530 ... 'step',
531 ... 'step',
532 ... 'step',
533 ... 'step',
534 ... 'continue',
535 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000536 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000537 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000538 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
539 -> mod.foo_pony(callback)
540 (Pdb) step
541 --Call--
542 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
543 -> def callback():
544 (Pdb) step
545 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
546 -> return None
547 (Pdb) step
548 --Return--
549 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
550 -> return None
551 (Pdb) step
552 --Return--
553 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
554 -> mod.foo_pony(callback)
555 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000556 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
557 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000558 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000559 """
Georg Brandl243ad662009-05-05 09:00:19 +0000560
561
Georg Brandl3f940892010-07-30 10:29:19 +0000562def test_pdb_continue_in_bottomframe():
563 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
564
565 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700566 ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
Georg Brandl3f940892010-07-30 10:29:19 +0000567 ... inst.set_trace()
568 ... inst.botframe = sys._getframe() # hackery to get the right botframe
569 ... print(1)
570 ... print(2)
571 ... print(3)
572 ... print(4)
573
Georg Brandl7410dd12010-07-30 12:01:20 +0000574 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000575 ... 'next',
576 ... 'break 7',
577 ... 'continue',
578 ... 'next',
579 ... 'continue',
580 ... 'continue',
581 ... ]):
582 ... test_function()
583 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
584 -> inst.botframe = sys._getframe() # hackery to get the right botframe
585 (Pdb) next
586 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
587 -> print(1)
588 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000589 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000590 (Pdb) continue
591 1
592 2
593 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
594 -> print(3)
595 (Pdb) next
596 3
597 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
598 -> print(4)
599 (Pdb) continue
600 4
601 """
602
603
Georg Brandl46b9afc2010-07-30 09:14:20 +0000604def pdb_invoke(method, arg):
605 """Run pdb.method(arg)."""
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700606 getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000607
608
609def test_pdb_run_with_incorrect_argument():
610 """Testing run and runeval with incorrect first argument.
611
612 >>> pti = PdbTestInput(['continue',])
613 >>> with pti:
614 ... pdb_invoke('run', lambda x: x)
615 Traceback (most recent call last):
616 TypeError: exec() arg 1 must be a string, bytes or code object
617
618 >>> with pti:
619 ... pdb_invoke('runeval', lambda x: x)
620 Traceback (most recent call last):
621 TypeError: eval() arg 1 must be a string, bytes or code object
622 """
623
624
625def test_pdb_run_with_code_object():
626 """Testing run and runeval with code object as a first argument.
627
Georg Brandle1e8df12010-07-31 08:14:16 +0000628 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000629 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000630 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000631 (Pdb) step
632 --Return--
633 > <string>(1)<module>()->None
634 (Pdb) x
635 1
636 (Pdb) continue
637
638 >>> with PdbTestInput(['x', 'continue']):
639 ... x=0
640 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
641 > <string>(1)<module>()->None
642 (Pdb) x
643 1
644 (Pdb) continue
645 """
646
Guido van Rossum8820c232013-11-21 11:30:06 -0800647def test_next_until_return_at_return_event():
648 """Test that pdb stops after a next/until/return issued at a return debug event.
649
650 >>> def test_function_2():
651 ... x = 1
652 ... x = 2
653
654 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700655 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800656 ... test_function_2()
657 ... test_function_2()
658 ... test_function_2()
659 ... end = 1
660
Antoine Pitrouc04d4682014-08-11 21:40:38 -0400661 >>> from bdb import Breakpoint
662 >>> Breakpoint.next = 1
Guido van Rossum8820c232013-11-21 11:30:06 -0800663 >>> with PdbTestInput(['break test_function_2',
664 ... 'continue',
665 ... 'return',
666 ... 'next',
667 ... 'continue',
668 ... 'return',
669 ... 'until',
670 ... 'continue',
671 ... 'return',
672 ... 'return',
673 ... 'continue']):
674 ... test_function()
675 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
676 -> test_function_2()
677 (Pdb) break test_function_2
678 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
679 (Pdb) continue
680 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
681 -> x = 1
682 (Pdb) return
683 --Return--
684 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
685 -> x = 2
686 (Pdb) next
687 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
688 -> test_function_2()
689 (Pdb) continue
690 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
691 -> x = 1
692 (Pdb) return
693 --Return--
694 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
695 -> x = 2
696 (Pdb) until
697 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
698 -> test_function_2()
699 (Pdb) continue
700 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
701 -> x = 1
702 (Pdb) return
703 --Return--
704 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
705 -> x = 2
706 (Pdb) return
707 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
708 -> end = 1
709 (Pdb) continue
710 """
711
712def test_pdb_next_command_for_generator():
713 """Testing skip unwindng stack on yield for generators for "next" command
714
715 >>> def test_gen():
716 ... yield 0
717 ... return 1
718 ... yield 2
719
720 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700721 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800722 ... it = test_gen()
723 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300724 ... if next(it) != 0:
725 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800726 ... next(it)
727 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300728 ... if ex.value != 1:
729 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800730 ... print("finished")
731
732 >>> with PdbTestInput(['step',
733 ... 'step',
734 ... 'step',
735 ... 'next',
736 ... 'next',
737 ... 'step',
738 ... 'step',
739 ... 'continue']):
740 ... test_function()
741 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
742 -> it = test_gen()
743 (Pdb) step
744 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
745 -> try:
746 (Pdb) step
747 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300748 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800749 (Pdb) step
750 --Call--
751 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
752 -> def test_gen():
753 (Pdb) next
754 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
755 -> yield 0
756 (Pdb) next
757 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
758 -> return 1
759 (Pdb) step
760 --Return--
761 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
762 -> return 1
763 (Pdb) step
764 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300765 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800766 -> next(it)
767 (Pdb) continue
768 finished
769 """
770
Pablo Galindo46877022018-01-29 00:25:05 +0000771def test_pdb_next_command_for_coroutine():
772 """Testing skip unwindng stack on yield for coroutines for "next" command
773
774 >>> import asyncio
775
776 >>> async def test_coro():
777 ... await asyncio.sleep(0)
778 ... await asyncio.sleep(0)
779 ... await asyncio.sleep(0)
780
781 >>> async def test_main():
782 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
783 ... await test_coro()
784
785 >>> def test_function():
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000786 ... loop = asyncio.new_event_loop()
Pablo Galindo46877022018-01-29 00:25:05 +0000787 ... loop.run_until_complete(test_main())
788 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700789 ... asyncio.set_event_loop_policy(None)
Pablo Galindo46877022018-01-29 00:25:05 +0000790 ... print("finished")
791
792 >>> with PdbTestInput(['step',
793 ... 'step',
794 ... 'next',
795 ... 'next',
796 ... 'next',
797 ... 'step',
798 ... 'continue']):
799 ... test_function()
800 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
801 -> await test_coro()
802 (Pdb) step
803 --Call--
804 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
805 -> async def test_coro():
806 (Pdb) step
807 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
808 -> await asyncio.sleep(0)
809 (Pdb) next
810 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
811 -> await asyncio.sleep(0)
812 (Pdb) next
813 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
814 -> await asyncio.sleep(0)
815 (Pdb) next
816 Internal StopIteration
817 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
818 -> await test_coro()
819 (Pdb) step
820 --Return--
821 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
822 -> await test_coro()
823 (Pdb) continue
824 finished
825 """
826
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500827def test_pdb_next_command_for_asyncgen():
828 """Testing skip unwindng stack on yield for coroutines for "next" command
829
830 >>> import asyncio
831
832 >>> async def agen():
833 ... yield 1
834 ... await asyncio.sleep(0)
835 ... yield 2
836
837 >>> async def test_coro():
838 ... async for x in agen():
839 ... print(x)
840
841 >>> async def test_main():
842 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
843 ... await test_coro()
844
845 >>> def test_function():
846 ... loop = asyncio.new_event_loop()
847 ... loop.run_until_complete(test_main())
848 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700849 ... asyncio.set_event_loop_policy(None)
Yury Selivanov9ee1bf92018-01-28 22:43:46 -0500850 ... print("finished")
851
852 >>> with PdbTestInput(['step',
853 ... 'step',
854 ... 'next',
855 ... 'next',
856 ... 'step',
857 ... 'next',
858 ... 'continue']):
859 ... test_function()
860 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main()
861 -> await test_coro()
862 (Pdb) step
863 --Call--
864 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro()
865 -> async def test_coro():
866 (Pdb) step
867 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
868 -> async for x in agen():
869 (Pdb) next
870 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro()
871 -> print(x)
872 (Pdb) next
873 1
874 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
875 -> async for x in agen():
876 (Pdb) step
877 --Call--
878 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen()
879 -> yield 1
880 (Pdb) next
881 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen()
882 -> await asyncio.sleep(0)
883 (Pdb) continue
884 2
885 finished
886 """
887
Guido van Rossum8820c232013-11-21 11:30:06 -0800888def test_pdb_return_command_for_generator():
889 """Testing no unwindng stack on yield for generators
890 for "return" command
891
892 >>> def test_gen():
893 ... yield 0
894 ... return 1
895 ... yield 2
896
897 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700898 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800899 ... it = test_gen()
900 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300901 ... if next(it) != 0:
902 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800903 ... next(it)
904 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300905 ... if ex.value != 1:
906 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800907 ... print("finished")
908
909 >>> with PdbTestInput(['step',
910 ... 'step',
911 ... 'step',
912 ... 'return',
913 ... 'step',
914 ... 'step',
915 ... 'continue']):
916 ... test_function()
917 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
918 -> it = test_gen()
919 (Pdb) step
920 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
921 -> try:
922 (Pdb) step
923 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300924 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800925 (Pdb) step
926 --Call--
927 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
928 -> def test_gen():
929 (Pdb) return
930 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300931 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800932 -> next(it)
933 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300934 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800935 -> except StopIteration as ex:
936 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300937 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
938 -> if ex.value != 1:
Guido van Rossum8820c232013-11-21 11:30:06 -0800939 (Pdb) continue
940 finished
941 """
942
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000943def test_pdb_return_command_for_coroutine():
944 """Testing no unwindng stack on yield for coroutines for "return" command
945
946 >>> import asyncio
947
948 >>> async def test_coro():
949 ... await asyncio.sleep(0)
950 ... await asyncio.sleep(0)
951 ... await asyncio.sleep(0)
952
953 >>> async def test_main():
954 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
955 ... await test_coro()
956
957 >>> def test_function():
958 ... loop = asyncio.new_event_loop()
959 ... loop.run_until_complete(test_main())
960 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -0700961 ... asyncio.set_event_loop_policy(None)
Pablo Galindoc7ab5812018-01-29 01:31:00 +0000962 ... print("finished")
963
964 >>> with PdbTestInput(['step',
965 ... 'step',
966 ... 'next',
967 ... 'continue']):
968 ... test_function()
969 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main()
970 -> await test_coro()
971 (Pdb) step
972 --Call--
973 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro()
974 -> async def test_coro():
975 (Pdb) step
976 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro()
977 -> await asyncio.sleep(0)
978 (Pdb) next
979 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro()
980 -> await asyncio.sleep(0)
981 (Pdb) continue
982 finished
983 """
984
Guido van Rossum8820c232013-11-21 11:30:06 -0800985def test_pdb_until_command_for_generator():
986 """Testing no unwindng stack on yield for generators
Min ho Kimc4cacc82019-07-31 08:16:13 +1000987 for "until" command if target breakpoint is not reached
Guido van Rossum8820c232013-11-21 11:30:06 -0800988
989 >>> def test_gen():
990 ... yield 0
991 ... yield 1
992 ... yield 2
993
994 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700995 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800996 ... for i in test_gen():
997 ... print(i)
998 ... print("finished")
999
1000 >>> with PdbTestInput(['step',
1001 ... 'until 4',
1002 ... 'step',
1003 ... 'step',
1004 ... 'continue']):
1005 ... test_function()
1006 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
1007 -> for i in test_gen():
1008 (Pdb) step
1009 --Call--
1010 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
1011 -> def test_gen():
1012 (Pdb) until 4
1013 0
1014 1
1015 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
1016 -> yield 2
1017 (Pdb) step
1018 --Return--
1019 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
1020 -> yield 2
1021 (Pdb) step
1022 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
1023 -> print(i)
1024 (Pdb) continue
1025 2
1026 finished
1027 """
1028
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001029def test_pdb_until_command_for_coroutine():
1030 """Testing no unwindng stack for coroutines
Min ho Kimc4cacc82019-07-31 08:16:13 +10001031 for "until" command if target breakpoint is not reached
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001032
1033 >>> import asyncio
1034
1035 >>> async def test_coro():
1036 ... print(0)
1037 ... await asyncio.sleep(0)
1038 ... print(1)
1039 ... await asyncio.sleep(0)
1040 ... print(2)
1041 ... await asyncio.sleep(0)
1042 ... print(3)
1043
1044 >>> async def test_main():
1045 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1046 ... await test_coro()
1047
1048 >>> def test_function():
1049 ... loop = asyncio.new_event_loop()
1050 ... loop.run_until_complete(test_main())
1051 ... loop.close()
Brett Cannon8425de42018-06-01 20:34:09 -07001052 ... asyncio.set_event_loop_policy(None)
Andrew Svetlov4f4ef0a2018-01-29 16:17:45 +02001053 ... print("finished")
1054
1055 >>> with PdbTestInput(['step',
1056 ... 'until 8',
1057 ... 'continue']):
1058 ... test_function()
1059 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main()
1060 -> await test_coro()
1061 (Pdb) step
1062 --Call--
1063 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro()
1064 -> async def test_coro():
1065 (Pdb) until 8
1066 0
1067 1
1068 2
1069 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro()
1070 -> print(3)
1071 (Pdb) continue
1072 3
1073 finished
1074 """
1075
Guido van Rossum8820c232013-11-21 11:30:06 -08001076def test_pdb_next_command_in_generator_for_loop():
Martin Panter46f50722016-05-26 05:35:26 +00001077 """The next command on returning from a generator controlled by a for loop.
Guido van Rossum8820c232013-11-21 11:30:06 -08001078
1079 >>> def test_gen():
1080 ... yield 0
1081 ... return 1
1082
1083 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001084 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001085 ... for i in test_gen():
1086 ... print('value', i)
1087 ... x = 123
1088
1089 >>> with PdbTestInput(['break test_gen',
1090 ... 'continue',
1091 ... 'next',
1092 ... 'next',
1093 ... 'next',
1094 ... 'continue']):
1095 ... test_function()
1096 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1097 -> for i in test_gen():
1098 (Pdb) break test_gen
1099 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
1100 (Pdb) continue
1101 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
1102 -> yield 0
1103 (Pdb) next
1104 value 0
1105 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
1106 -> return 1
1107 (Pdb) next
1108 Internal StopIteration: 1
1109 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
1110 -> for i in test_gen():
1111 (Pdb) next
1112 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
1113 -> x = 123
1114 (Pdb) continue
1115 """
1116
1117def test_pdb_next_command_subiterator():
1118 """The next command in a generator with a subiterator.
1119
1120 >>> def test_subgenerator():
1121 ... yield 0
1122 ... return 1
1123
1124 >>> def test_gen():
1125 ... x = yield from test_subgenerator()
1126 ... return x
1127
1128 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001129 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -08001130 ... for i in test_gen():
1131 ... print('value', i)
1132 ... x = 123
1133
1134 >>> with PdbTestInput(['step',
1135 ... 'step',
1136 ... 'next',
1137 ... 'next',
1138 ... 'next',
1139 ... 'continue']):
1140 ... test_function()
1141 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1142 -> for i in test_gen():
1143 (Pdb) step
1144 --Call--
1145 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
1146 -> def test_gen():
1147 (Pdb) step
1148 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
1149 -> x = yield from test_subgenerator()
1150 (Pdb) next
1151 value 0
1152 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
1153 -> return x
1154 (Pdb) next
1155 Internal StopIteration: 1
1156 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
1157 -> for i in test_gen():
1158 (Pdb) next
1159 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
1160 -> x = 123
1161 (Pdb) continue
1162 """
1163
Xavier de Gaye10e54ae2016-10-12 20:13:24 +02001164def test_pdb_issue_20766():
1165 """Test for reference leaks when the SIGINT handler is set.
1166
1167 >>> def test_function():
1168 ... i = 1
1169 ... while i <= 2:
1170 ... sess = pdb.Pdb()
1171 ... sess.set_trace(sys._getframe())
1172 ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1173 ... i += 1
1174
1175 >>> with PdbTestInput(['continue',
1176 ... 'continue']):
1177 ... test_function()
1178 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
1179 -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
1180 (Pdb) continue
1181 pdb 1: <built-in function default_int_handler>
1182 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
1183 -> sess.set_trace(sys._getframe())
1184 (Pdb) continue
1185 pdb 2: <built-in function default_int_handler>
1186 """
Georg Brandl46b9afc2010-07-30 09:14:20 +00001187
Georg Brandl6cccb862010-07-30 14:16:43 +00001188
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001189class PdbTestCase(unittest.TestCase):
1190 def tearDown(self):
Hai Shi604bba12020-08-04 23:51:43 +08001191 os_helper.unlink(os_helper.TESTFN)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001192
1193 def _run_pdb(self, pdb_args, commands):
Hai Shi604bba12020-08-04 23:51:43 +08001194 self.addCleanup(os_helper.rmtree, '__pycache__')
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001195 cmd = [sys.executable, '-m', 'pdb'] + pdb_args
1196 with subprocess.Popen(
1197 cmd,
1198 stdout=subprocess.PIPE,
1199 stdin=subprocess.PIPE,
1200 stderr=subprocess.STDOUT,
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001201 env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'}
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001202 ) as proc:
1203 stdout, stderr = proc.communicate(str.encode(commands))
1204 stdout = stdout and bytes.decode(stdout)
1205 stderr = stderr and bytes.decode(stderr)
1206 return stdout, stderr
1207
1208 def run_pdb_script(self, script, commands):
Senthil Kumaran42d70812012-05-01 10:07:49 +08001209 """Run 'script' lines with pdb and the pdb 'commands'."""
1210 filename = 'main.py'
1211 with open(filename, 'w') as f:
1212 f.write(textwrap.dedent(script))
Hai Shi604bba12020-08-04 23:51:43 +08001213 self.addCleanup(os_helper.unlink, filename)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001214 return self._run_pdb([filename], commands)
1215
1216 def run_pdb_module(self, script, commands):
1217 """Runs the script code as part of a module"""
1218 self.module_name = 't_main'
Hai Shi604bba12020-08-04 23:51:43 +08001219 os_helper.rmtree(self.module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001220 main_file = self.module_name + '/__main__.py'
1221 init_file = self.module_name + '/__init__.py'
1222 os.mkdir(self.module_name)
1223 with open(init_file, 'w') as f:
1224 pass
1225 with open(main_file, 'w') as f:
1226 f.write(textwrap.dedent(script))
Hai Shi604bba12020-08-04 23:51:43 +08001227 self.addCleanup(os_helper.rmtree, self.module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001228 return self._run_pdb(['-m', self.module_name], commands)
Senthil Kumaran42d70812012-05-01 10:07:49 +08001229
Georg Brandl6e220552013-10-13 20:51:47 +02001230 def _assert_find_function(self, file_content, func_name, expected):
Hai Shi604bba12020-08-04 23:51:43 +08001231 with open(os_helper.TESTFN, 'wb') as f:
Georg Brandl6e220552013-10-13 20:51:47 +02001232 f.write(file_content)
1233
1234 expected = None if not expected else (
Hai Shi604bba12020-08-04 23:51:43 +08001235 expected[0], os_helper.TESTFN, expected[1])
Georg Brandl6e220552013-10-13 20:51:47 +02001236 self.assertEqual(
Hai Shi604bba12020-08-04 23:51:43 +08001237 expected, pdb.find_function(func_name, os_helper.TESTFN))
Georg Brandl6e220552013-10-13 20:51:47 +02001238
1239 def test_find_function_empty_file(self):
Serhiy Storchaka19fcffa2020-06-21 11:07:50 +03001240 self._assert_find_function(b'', 'foo', None)
Georg Brandl6e220552013-10-13 20:51:47 +02001241
1242 def test_find_function_found(self):
1243 self._assert_find_function(
1244 """\
Serhiy Storchaka19fcffa2020-06-21 11:07:50 +03001245def foo():
1246 pass
Georg Brandl6e220552013-10-13 20:51:47 +02001247
Serhiy Storchaka19fcffa2020-06-21 11:07:50 +03001248def bœr():
1249 pass
Georg Brandl6e220552013-10-13 20:51:47 +02001250
Serhiy Storchaka19fcffa2020-06-21 11:07:50 +03001251def quux():
1252 pass
1253""".encode(),
1254 'bœr',
1255 ('bœr', 4),
1256 )
1257
1258 def test_find_function_found_with_encoding_cookie(self):
1259 self._assert_find_function(
1260 """\
1261# coding: iso-8859-15
1262def foo():
1263 pass
1264
1265def bœr():
1266 pass
1267
1268def quux():
1269 pass
1270""".encode('iso-8859-15'),
1271 'bœr',
1272 ('bœr', 5),
1273 )
1274
1275 def test_find_function_found_with_bom(self):
1276 self._assert_find_function(
1277 codecs.BOM_UTF8 + """\
1278def bœr():
1279 pass
1280""".encode(),
1281 'bœr',
1282 ('bœr', 1),
Georg Brandl6e220552013-10-13 20:51:47 +02001283 )
1284
Georg Brandl6cccb862010-07-30 14:16:43 +00001285 def test_issue7964(self):
1286 # open the file as binary so we can force \r\n newline
Hai Shi604bba12020-08-04 23:51:43 +08001287 with open(os_helper.TESTFN, 'wb') as f:
Georg Brandl6cccb862010-07-30 14:16:43 +00001288 f.write(b'print("testing my pdb")\r\n')
Hai Shi604bba12020-08-04 23:51:43 +08001289 cmd = [sys.executable, '-m', 'pdb', os_helper.TESTFN]
Georg Brandl6cccb862010-07-30 14:16:43 +00001290 proc = subprocess.Popen(cmd,
1291 stdout=subprocess.PIPE,
1292 stdin=subprocess.PIPE,
1293 stderr=subprocess.STDOUT,
1294 )
Brian Curtin994ad6c2010-11-05 15:38:47 +00001295 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +00001296 stdout, stderr = proc.communicate(b'quit\n')
1297 self.assertNotIn(b'SyntaxError', stdout,
1298 "Got a syntax error running test script under PDB")
1299
Senthil Kumaran42d70812012-05-01 10:07:49 +08001300 def test_issue13183(self):
1301 script = """
1302 from bar import bar
1303
1304 def foo():
1305 bar()
1306
1307 def nope():
1308 pass
1309
1310 def foobar():
1311 foo()
1312 nope()
1313
1314 foobar()
1315 """
1316 commands = """
1317 from bar import bar
1318 break bar
1319 continue
1320 step
1321 step
1322 quit
1323 """
1324 bar = """
1325 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +08001326 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +08001327 """
1328 with open('bar.py', 'w') as f:
1329 f.write(textwrap.dedent(bar))
Hai Shi604bba12020-08-04 23:51:43 +08001330 self.addCleanup(os_helper.unlink, 'bar.py')
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001331 stdout, stderr = self.run_pdb_script(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001332 self.assertTrue(
1333 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1334 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001335
Daniel Hahler9139f922019-04-01 23:59:50 +02001336 def test_issue13120(self):
1337 # Invoking "continue" on a non-main thread triggered an exception
1338 # inside signal.signal.
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001339
Hai Shi604bba12020-08-04 23:51:43 +08001340 with open(os_helper.TESTFN, 'wb') as f:
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001341 f.write(textwrap.dedent("""
1342 import threading
1343 import pdb
1344
1345 def start_pdb():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001346 pdb.Pdb(readrc=False).set_trace()
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001347 x = 1
1348 y = 1
1349
1350 t = threading.Thread(target=start_pdb)
1351 t.start()""").encode('ascii'))
Hai Shi604bba12020-08-04 23:51:43 +08001352 cmd = [sys.executable, '-u', os_helper.TESTFN]
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001353 proc = subprocess.Popen(cmd,
1354 stdout=subprocess.PIPE,
1355 stdin=subprocess.PIPE,
1356 stderr=subprocess.STDOUT,
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001357 env={**os.environ, 'PYTHONIOENCODING': 'utf-8'}
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001358 )
1359 self.addCleanup(proc.stdout.close)
1360 stdout, stderr = proc.communicate(b'cont\n')
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001361 self.assertNotIn(b'Error', stdout,
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001362 "Got an error running test script under PDB")
1363
Daniel Hahler8d64bfa2019-09-09 12:45:58 +02001364 def test_issue36250(self):
1365
Hai Shi604bba12020-08-04 23:51:43 +08001366 with open(os_helper.TESTFN, 'wb') as f:
Daniel Hahler8d64bfa2019-09-09 12:45:58 +02001367 f.write(textwrap.dedent("""
1368 import threading
1369 import pdb
1370
1371 evt = threading.Event()
1372
1373 def start_pdb():
1374 evt.wait()
1375 pdb.Pdb(readrc=False).set_trace()
1376
1377 t = threading.Thread(target=start_pdb)
1378 t.start()
1379 pdb.Pdb(readrc=False).set_trace()
1380 evt.set()
1381 t.join()""").encode('ascii'))
Hai Shi604bba12020-08-04 23:51:43 +08001382 cmd = [sys.executable, '-u', os_helper.TESTFN]
Daniel Hahler8d64bfa2019-09-09 12:45:58 +02001383 proc = subprocess.Popen(cmd,
1384 stdout=subprocess.PIPE,
1385 stdin=subprocess.PIPE,
1386 stderr=subprocess.STDOUT,
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001387 env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'}
Daniel Hahler8d64bfa2019-09-09 12:45:58 +02001388 )
1389 self.addCleanup(proc.stdout.close)
1390 stdout, stderr = proc.communicate(b'cont\ncont\n')
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001391 self.assertNotIn(b'Error', stdout,
Daniel Hahler8d64bfa2019-09-09 12:45:58 +02001392 "Got an error running test script under PDB")
1393
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001394 def test_issue16180(self):
1395 # A syntax error in the debuggee.
1396 script = "def f: pass\n"
1397 commands = ''
1398 expected = "SyntaxError:"
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001399 stdout, stderr = self.run_pdb_script(script, commands)
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001400 self.assertIn(expected, stdout,
1401 '\n\nExpected:\n{}\nGot:\n{}\n'
1402 'Fail to handle a syntax error in the debuggee.'
1403 .format(expected, stdout))
1404
1405
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001406 def test_readrc_kwarg(self):
Victor Stinner11ea0442016-09-09 22:56:54 -07001407 script = textwrap.dedent("""
1408 import pdb; pdb.Pdb(readrc=False).set_trace()
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001409
Victor Stinner11ea0442016-09-09 22:56:54 -07001410 print('hello')
1411 """)
Victor Stinner11ea0442016-09-09 22:56:54 -07001412
Victor Stinnerbc626262016-09-09 23:22:09 -07001413 save_home = os.environ.pop('HOME', None)
1414 try:
Hai Shi604bba12020-08-04 23:51:43 +08001415 with os_helper.temp_cwd():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001416 with open('.pdbrc', 'w') as f:
1417 f.write("invalid\n")
1418
1419 with open('main.py', 'w') as f:
1420 f.write(script)
1421
1422 cmd = [sys.executable, 'main.py']
1423 proc = subprocess.Popen(
1424 cmd,
1425 stdout=subprocess.PIPE,
1426 stdin=subprocess.PIPE,
1427 stderr=subprocess.PIPE,
1428 )
Victor Stinnerbc626262016-09-09 23:22:09 -07001429 with proc:
1430 stdout, stderr = proc.communicate(b'q\n')
Serhiy Storchaka700cfa82020-06-25 17:56:31 +03001431 self.assertNotIn(b"NameError: name 'invalid' is not defined",
1432 stdout)
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001433
1434 finally:
Victor Stinner11ea0442016-09-09 22:56:54 -07001435 if save_home is not None:
1436 os.environ['HOME'] = save_home
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001437
Timothy Hopper7ea9a852019-08-02 18:20:14 -04001438 def test_readrc_homedir(self):
1439 save_home = os.environ.pop("HOME", None)
Hai Shi604bba12020-08-04 23:51:43 +08001440 with os_helper.temp_dir() as temp_dir, patch("os.path.expanduser"):
Timothy Hopper7ea9a852019-08-02 18:20:14 -04001441 rc_path = os.path.join(temp_dir, ".pdbrc")
1442 os.path.expanduser.return_value = rc_path
1443 try:
1444 with open(rc_path, "w") as f:
1445 f.write("invalid")
1446 self.assertEqual(pdb.Pdb().rcLines[0], "invalid")
1447 finally:
1448 if save_home is not None:
1449 os.environ["HOME"] = save_home
1450
Barry Warsaw35425d62017-09-22 12:29:42 -04001451 def test_header(self):
1452 stdout = StringIO()
1453 header = 'Nobody expects... blah, blah, blah'
1454 with ExitStack() as resources:
1455 resources.enter_context(patch('sys.stdout', stdout))
1456 resources.enter_context(patch.object(pdb.Pdb, 'set_trace'))
1457 pdb.set_trace(header=header)
1458 self.assertEqual(stdout.getvalue(), header + '\n')
1459
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001460 def test_run_module(self):
1461 script = """print("SUCCESS")"""
1462 commands = """
1463 continue
1464 quit
1465 """
1466 stdout, stderr = self.run_pdb_module(script, commands)
1467 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1468
1469 def test_module_is_run_as_main(self):
1470 script = """
1471 if __name__ == '__main__':
1472 print("SUCCESS")
1473 """
1474 commands = """
1475 continue
1476 quit
1477 """
1478 stdout, stderr = self.run_pdb_module(script, commands)
1479 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
1480
1481 def test_breakpoint(self):
1482 script = """
1483 if __name__ == '__main__':
1484 pass
1485 print("SUCCESS")
1486 pass
1487 """
1488 commands = """
1489 b 3
1490 quit
1491 """
1492 stdout, stderr = self.run_pdb_module(script, commands)
1493 self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout)
1494 self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout)
1495
1496 def test_run_pdb_with_pdb(self):
1497 commands = """
1498 c
1499 quit
1500 """
1501 stdout, stderr = self._run_pdb(["-m", "pdb"], commands)
Mario Corcherofcf8b4c2018-01-28 04:58:47 +00001502 self.assertIn(
1503 pdb._usage,
1504 stdout.replace('\r', '') # remove \r for windows
1505 )
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001506
1507 def test_module_without_a_main(self):
1508 module_name = 't_main'
Hai Shi604bba12020-08-04 23:51:43 +08001509 os_helper.rmtree(module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001510 init_file = module_name + '/__init__.py'
1511 os.mkdir(module_name)
1512 with open(init_file, 'w') as f:
1513 pass
Hai Shi604bba12020-08-04 23:51:43 +08001514 self.addCleanup(os_helper.rmtree, module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001515 stdout, stderr = self._run_pdb(['-m', module_name], "")
1516 self.assertIn("ImportError: No module named t_main.__main__",
1517 stdout.splitlines())
1518
1519 def test_blocks_at_first_code_line(self):
1520 script = """
1521 #This is a comment, on line 2
1522
1523 print("SUCCESS")
1524 """
1525 commands = """
1526 quit
1527 """
1528 stdout, stderr = self.run_pdb_module(script, commands)
1529 self.assertTrue(any("__main__.py(4)<module>()"
1530 in l for l in stdout.splitlines()), stdout)
1531
1532 def test_relative_imports(self):
1533 self.module_name = 't_main'
Hai Shi604bba12020-08-04 23:51:43 +08001534 os_helper.rmtree(self.module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001535 main_file = self.module_name + '/__main__.py'
1536 init_file = self.module_name + '/__init__.py'
1537 module_file = self.module_name + '/module.py'
Hai Shi604bba12020-08-04 23:51:43 +08001538 self.addCleanup(os_helper.rmtree, self.module_name)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001539 os.mkdir(self.module_name)
1540 with open(init_file, 'w') as f:
1541 f.write(textwrap.dedent("""
1542 top_var = "VAR from top"
1543 """))
1544 with open(main_file, 'w') as f:
1545 f.write(textwrap.dedent("""
1546 from . import top_var
1547 from .module import var
1548 from . import module
1549 pass # We'll stop here and print the vars
1550 """))
1551 with open(module_file, 'w') as f:
1552 f.write(textwrap.dedent("""
1553 var = "VAR from module"
1554 var2 = "second var"
1555 """))
1556 commands = """
1557 b 5
1558 c
1559 p top_var
1560 p var
1561 p module.var2
1562 quit
1563 """
1564 stdout, _ = self._run_pdb(['-m', self.module_name], commands)
Mario Corchero38bfa842018-02-03 06:40:11 +00001565 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001566 self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
1567 self.assertTrue(any("second var" in l for l in stdout.splitlines()))
Georg Brandl6cccb862010-07-30 14:16:43 +00001568
Mario Corchero38bfa842018-02-03 06:40:11 +00001569 def test_relative_imports_on_plain_module(self):
1570 # Validates running a plain module. See bpo32691
1571 self.module_name = 't_main'
Hai Shi604bba12020-08-04 23:51:43 +08001572 os_helper.rmtree(self.module_name)
Mario Corchero38bfa842018-02-03 06:40:11 +00001573 main_file = self.module_name + '/runme.py'
1574 init_file = self.module_name + '/__init__.py'
1575 module_file = self.module_name + '/module.py'
Hai Shi604bba12020-08-04 23:51:43 +08001576 self.addCleanup(os_helper.rmtree, self.module_name)
Mario Corchero38bfa842018-02-03 06:40:11 +00001577 os.mkdir(self.module_name)
1578 with open(init_file, 'w') as f:
1579 f.write(textwrap.dedent("""
1580 top_var = "VAR from top"
1581 """))
1582 with open(main_file, 'w') as f:
1583 f.write(textwrap.dedent("""
1584 from . import module
1585 pass # We'll stop here and print the vars
1586 """))
1587 with open(module_file, 'w') as f:
1588 f.write(textwrap.dedent("""
1589 var = "VAR from module"
1590 """))
1591 commands = """
1592 b 3
1593 c
1594 p module.var
1595 quit
1596 """
1597 stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
1598 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
1599
Daniel Hahler3e936432019-03-12 04:29:04 +01001600 def test_errors_in_command(self):
1601 commands = "\n".join([
1602 'print(',
1603 'debug print(',
1604 'debug doesnotexist',
1605 'c',
1606 ])
1607 stdout, _ = self.run_pdb_script('', commands + '\n')
1608
Daniel Hahler43277052019-02-15 21:52:53 +01001609 self.assertEqual(stdout.splitlines()[1:], [
1610 '(Pdb) *** SyntaxError: unexpected EOF while parsing',
Daniel Hahler3e936432019-03-12 04:29:04 +01001611
1612 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1613 '*** SyntaxError: unexpected EOF while parsing',
1614 'LEAVING RECURSIVE DEBUGGER',
1615
1616 '(Pdb) ENTERING RECURSIVE DEBUGGER',
1617 '> <string>(1)<module>()',
1618 "((Pdb)) *** NameError: name 'doesnotexist' is not defined",
1619 'LEAVING RECURSIVE DEBUGGER',
Daniel Hahler43277052019-02-15 21:52:53 +01001620 '(Pdb) ',
1621 ])
Georg Brandl6cccb862010-07-30 14:16:43 +00001622
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001623def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +00001624 from test import test_pdb
Mario Corchero9f1e5f12018-01-06 07:53:05 +00001625 suites = [
1626 unittest.makeSuite(PdbTestCase),
1627 doctest.DocTestSuite(test_pdb)
1628 ]
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001629 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +00001630
1631
1632if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001633 unittest.main()