blob: 0ea2af5541750cffb379c1fdaff6ff6959b25297 [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
12from test import support
13# This little helper class is essential for testing pdb under doctest.
14from test.test_doctest import _FakeInput
15
16
Georg Brandl9fa2e022009-09-16 16:40:45 +000017class PdbTestInput(object):
18 """Context manager that makes testing Pdb in doctests easier."""
19
20 def __init__(self, input):
21 self.input = input
22
23 def __enter__(self):
24 self.real_stdin = sys.stdin
25 sys.stdin = _FakeInput(self.input)
Brett Cannon31f59292011-02-21 19:29:56 +000026 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
Georg Brandl9fa2e022009-09-16 16:40:45 +000027
28 def __exit__(self, *exc):
29 sys.stdin = self.real_stdin
Brett Cannon31f59292011-02-21 19:29:56 +000030 if self.orig_trace:
31 sys.settrace(self.orig_trace)
Georg Brandl9fa2e022009-09-16 16:40:45 +000032
33
34def test_pdb_displayhook():
35 """This tests the custom displayhook for pdb.
36
37 >>> def test_function(foo, bar):
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070038 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl9fa2e022009-09-16 16:40:45 +000039 ... pass
40
41 >>> with PdbTestInput([
42 ... 'foo',
43 ... 'bar',
44 ... 'for i in range(5): print(i)',
45 ... 'continue',
46 ... ]):
47 ... test_function(1, None)
48 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
49 -> pass
50 (Pdb) foo
51 1
52 (Pdb) bar
53 (Pdb) for i in range(5): print(i)
54 0
55 1
56 2
57 3
58 4
59 (Pdb) continue
60 """
61
62
Georg Brandl0d089622010-07-30 16:00:46 +000063def test_pdb_basic_commands():
64 """Test the basic commands of pdb.
65
66 >>> def test_function_2(foo, bar='default'):
67 ... print(foo)
68 ... for i in range(5):
69 ... print(i)
70 ... print(bar)
71 ... for i in range(10):
72 ... never_executed
73 ... print('after for')
74 ... print('...')
75 ... return foo.upper()
76
77 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -070078 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +000079 ... ret = test_function_2('baz')
80 ... print(ret)
81
82 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
83 ... 'step', # entering the function call
84 ... 'args', # display function args
85 ... 'list', # list function source
86 ... 'bt', # display backtrace
87 ... 'up', # step up to test_function()
88 ... 'down', # step down to test_function_2() again
89 ... 'next', # stepping to print(foo)
90 ... 'next', # stepping to the for loop
91 ... 'step', # stepping into the for loop
92 ... 'until', # continuing until out of the for loop
93 ... 'next', # executing the print(bar)
94 ... 'jump 8', # jump over second for loop
95 ... 'return', # return out of function
96 ... 'retval', # display return value
97 ... 'continue',
98 ... ]):
99 ... test_function()
100 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
101 -> ret = test_function_2('baz')
102 (Pdb) step
103 --Call--
104 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
105 -> def test_function_2(foo, bar='default'):
106 (Pdb) args
107 foo = 'baz'
108 bar = 'default'
109 (Pdb) list
110 1 -> def test_function_2(foo, bar='default'):
111 2 print(foo)
112 3 for i in range(5):
113 4 print(i)
114 5 print(bar)
115 6 for i in range(10):
116 7 never_executed
117 8 print('after for')
118 9 print('...')
119 10 return foo.upper()
120 [EOF]
121 (Pdb) bt
122 ...
123 <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
124 -> test_function()
125 <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
126 -> ret = test_function_2('baz')
127 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
128 -> def test_function_2(foo, bar='default'):
129 (Pdb) up
130 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
131 -> ret = test_function_2('baz')
132 (Pdb) down
133 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
134 -> def test_function_2(foo, bar='default'):
135 (Pdb) next
136 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
137 -> print(foo)
138 (Pdb) next
139 baz
140 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
141 -> for i in range(5):
142 (Pdb) step
143 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
144 -> print(i)
145 (Pdb) until
146 0
147 1
148 2
149 3
150 4
151 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
152 -> print(bar)
153 (Pdb) next
154 default
155 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
156 -> for i in range(10):
157 (Pdb) jump 8
158 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
159 -> print('after for')
160 (Pdb) return
161 after for
162 ...
163 --Return--
164 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
165 -> return foo.upper()
166 (Pdb) retval
167 'BAZ'
168 (Pdb) continue
169 BAZ
170 """
171
172
173def test_pdb_breakpoint_commands():
174 """Test basic commands related to breakpoints.
175
176 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700177 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0d089622010-07-30 16:00:46 +0000178 ... print(1)
179 ... print(2)
180 ... print(3)
181 ... print(4)
182
183 First, need to clear bdb state that might be left over from previous tests.
184 Otherwise, the new breakpoints might get assigned different numbers.
185
186 >>> from bdb import Breakpoint
187 >>> Breakpoint.next = 1
188 >>> Breakpoint.bplist = {}
189 >>> Breakpoint.bpbynumber = [None]
190
191 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
192 the breakpoint list outputs a tab for the "stop only" and "ignore next"
193 lines, which we don't want to put in here.
194
195 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
196 ... 'break 3',
197 ... 'disable 1',
198 ... 'ignore 1 10',
199 ... 'condition 1 1 < 2',
200 ... 'break 4',
Senthil Kumaran6f107042010-11-29 11:54:17 +0000201 ... 'break 4',
202 ... 'break',
203 ... 'clear 3',
Georg Brandl0d089622010-07-30 16:00:46 +0000204 ... 'break',
205 ... 'condition 1',
206 ... 'enable 1',
207 ... 'clear 1',
208 ... 'commands 2',
R David Murray78d692f2013-10-10 17:23:26 -0400209 ... 'p "42"',
210 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints)
Georg Brandl0d089622010-07-30 16:00:46 +0000211 ... 'end',
212 ... 'continue', # will stop at breakpoint 2 (line 4)
213 ... 'clear', # clear all!
214 ... 'y',
215 ... 'tbreak 5',
216 ... 'continue', # will stop at temporary breakpoint
217 ... 'break', # make sure breakpoint is gone
218 ... 'continue',
219 ... ]):
220 ... test_function()
221 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
222 -> print(1)
223 (Pdb) break 3
224 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
225 (Pdb) disable 1
226 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
227 (Pdb) ignore 1 10
228 Will ignore next 10 crossings of breakpoint 1.
229 (Pdb) condition 1 1 < 2
230 New condition set for breakpoint 1.
231 (Pdb) break 4
232 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Senthil Kumaran6f107042010-11-29 11:54:17 +0000233 (Pdb) break 4
234 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
235 (Pdb) break
236 Num Type Disp Enb Where
237 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
238 stop only if 1 < 2
239 ignore next 10 hits
240 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
241 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
242 (Pdb) clear 3
243 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
Georg Brandl0d089622010-07-30 16:00:46 +0000244 (Pdb) break
245 Num Type Disp Enb Where
246 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
247 stop only if 1 < 2
248 ignore next 10 hits
249 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
250 (Pdb) condition 1
251 Breakpoint 1 is now unconditional.
252 (Pdb) enable 1
253 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
254 (Pdb) clear 1
255 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
256 (Pdb) commands 2
R David Murray78d692f2013-10-10 17:23:26 -0400257 (com) p "42"
258 (com) print("42", 7*6)
Georg Brandl0d089622010-07-30 16:00:46 +0000259 (com) end
260 (Pdb) continue
261 1
R David Murray78d692f2013-10-10 17:23:26 -0400262 '42'
263 42 42
Georg Brandl0d089622010-07-30 16:00:46 +0000264 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
265 -> print(2)
266 (Pdb) clear
267 Clear all breaks? y
268 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
269 (Pdb) tbreak 5
Senthil Kumaran6f107042010-11-29 11:54:17 +0000270 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000271 (Pdb) continue
272 2
Senthil Kumaran6f107042010-11-29 11:54:17 +0000273 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
Georg Brandl0d089622010-07-30 16:00:46 +0000274 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
275 -> print(3)
276 (Pdb) break
277 (Pdb) continue
278 3
279 4
280 """
281
282
Georg Brandle59ca2a2010-07-30 17:04:28 +0000283def do_nothing():
284 pass
285
286def do_something():
287 print(42)
288
289def test_list_commands():
290 """Test the list and source commands of pdb.
291
292 >>> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000293 ... import test.test_pdb
294 ... test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000295 ... 'some...'
296 ... 'more...'
297 ... 'code...'
298 ... 'to...'
299 ... 'make...'
300 ... 'a...'
301 ... 'long...'
302 ... 'listing...'
303 ... 'useful...'
304 ... '...'
305 ... '...'
306 ... return foo
307
308 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700309 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000310 ... ret = test_function_2('baz')
311
312 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
313 ... 'list', # list first function
314 ... 'step', # step into second function
315 ... 'list', # list second function
316 ... 'list', # continue listing to EOF
317 ... 'list 1,3', # list specific lines
318 ... 'list x', # invalid argument
319 ... 'next', # step to import
320 ... 'next', # step over import
321 ... 'step', # step into do_nothing
322 ... 'longlist', # list all lines
323 ... 'source do_something', # list all lines of function
Georg Brandlcdf66a92010-07-30 18:15:16 +0000324 ... 'source fooxxx', # something that doesn't exit
Georg Brandle59ca2a2010-07-30 17:04:28 +0000325 ... 'continue',
326 ... ]):
327 ... test_function()
328 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
329 -> ret = test_function_2('baz')
330 (Pdb) list
331 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700332 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000333 3 -> ret = test_function_2('baz')
334 [EOF]
335 (Pdb) step
336 --Call--
337 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
338 -> def test_function_2(foo):
339 (Pdb) list
340 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000341 2 import test.test_pdb
342 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000343 4 'some...'
344 5 'more...'
345 6 'code...'
346 7 'to...'
347 8 'make...'
348 9 'a...'
349 10 'long...'
350 11 'listing...'
351 (Pdb) list
352 12 'useful...'
353 13 '...'
354 14 '...'
355 15 return foo
356 [EOF]
357 (Pdb) list 1,3
358 1 -> def test_function_2(foo):
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000359 2 import test.test_pdb
360 3 test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000361 (Pdb) list x
362 *** ...
363 (Pdb) next
364 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000365 -> import test.test_pdb
Georg Brandle59ca2a2010-07-30 17:04:28 +0000366 (Pdb) next
367 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
Georg Brandla8fbc6a2010-07-31 11:52:46 +0000368 -> test.test_pdb.do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000369 (Pdb) step
370 --Call--
Georg Brandle1e8df12010-07-31 08:14:16 +0000371 > ...test_pdb.py(...)do_nothing()
Georg Brandle59ca2a2010-07-30 17:04:28 +0000372 -> def do_nothing():
373 (Pdb) longlist
374 ... -> def do_nothing():
375 ... pass
376 (Pdb) source do_something
377 ... def do_something():
378 ... print(42)
Georg Brandlcdf66a92010-07-30 18:15:16 +0000379 (Pdb) source fooxxx
380 *** ...
Georg Brandle59ca2a2010-07-30 17:04:28 +0000381 (Pdb) continue
382 """
383
384
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000385def test_post_mortem():
386 """Test post mortem traceback debugging.
387
388 >>> def test_function_2():
389 ... try:
390 ... 1/0
391 ... finally:
392 ... print('Exception!')
393
394 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700395 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000396 ... test_function_2()
397 ... print('Not reached.')
398
399 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
400 ... 'next', # step over exception-raising call
401 ... 'bt', # get a backtrace
402 ... 'list', # list code of test_function()
403 ... 'down', # step into test_function_2()
404 ... 'list', # list code of test_function_2()
405 ... 'continue',
406 ... ]):
407 ... try:
408 ... test_function()
409 ... except ZeroDivisionError:
410 ... print('Correctly reraised.')
411 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
412 -> test_function_2()
413 (Pdb) next
414 Exception!
415 ZeroDivisionError: division by zero
416 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
417 -> test_function_2()
418 (Pdb) bt
419 ...
420 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
421 -> test_function()
422 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
423 -> test_function_2()
424 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
425 -> 1/0
426 (Pdb) list
427 1 def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700428 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000429 3 -> test_function_2()
430 4 print('Not reached.')
431 [EOF]
432 (Pdb) down
433 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
434 -> 1/0
435 (Pdb) list
436 1 def test_function_2():
437 2 try:
438 3 >> 1/0
439 4 finally:
440 5 -> print('Exception!')
441 [EOF]
442 (Pdb) continue
443 Correctly reraised.
444 """
445
446
Georg Brandl243ad662009-05-05 09:00:19 +0000447def test_pdb_skip_modules():
448 """This illustrates the simple case of module skipping.
449
450 >>> def skip_module():
451 ... import string
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700452 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000453 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +0000454
Georg Brandl9fa2e022009-09-16 16:40:45 +0000455 >>> with PdbTestInput([
456 ... 'step',
457 ... 'continue',
458 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000459 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +0000460 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
461 -> string.capwords('FOO')
462 (Pdb) step
463 --Return--
464 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
465 -> string.capwords('FOO')
466 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000467 """
Georg Brandl243ad662009-05-05 09:00:19 +0000468
469
470# Module for testing skipping of module that makes a callback
Brett Cannon9529fbf2013-06-15 17:11:25 -0400471mod = types.ModuleType('module_to_skip')
Georg Brandl243ad662009-05-05 09:00:19 +0000472exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
473
474
475def test_pdb_skip_modules_with_callback():
476 """This illustrates skipping of modules that call into other code.
477
478 >>> def skip_module():
479 ... def callback():
480 ... return None
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700481 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +0000482 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +0000483
Georg Brandl9fa2e022009-09-16 16:40:45 +0000484 >>> with PdbTestInput([
485 ... 'step',
486 ... 'step',
487 ... 'step',
488 ... 'step',
489 ... 'step',
490 ... 'continue',
491 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000492 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000493 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000494 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
495 -> mod.foo_pony(callback)
496 (Pdb) step
497 --Call--
498 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
499 -> def callback():
500 (Pdb) step
501 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
502 -> return None
503 (Pdb) step
504 --Return--
505 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
506 -> return None
507 (Pdb) step
508 --Return--
509 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
510 -> mod.foo_pony(callback)
511 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000512 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
513 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000514 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000515 """
Georg Brandl243ad662009-05-05 09:00:19 +0000516
517
Georg Brandl3f940892010-07-30 10:29:19 +0000518def test_pdb_continue_in_bottomframe():
519 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
520
521 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700522 ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
Georg Brandl3f940892010-07-30 10:29:19 +0000523 ... inst.set_trace()
524 ... inst.botframe = sys._getframe() # hackery to get the right botframe
525 ... print(1)
526 ... print(2)
527 ... print(3)
528 ... print(4)
529
Georg Brandl7410dd12010-07-30 12:01:20 +0000530 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000531 ... 'next',
532 ... 'break 7',
533 ... 'continue',
534 ... 'next',
535 ... 'continue',
536 ... 'continue',
537 ... ]):
538 ... test_function()
539 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
540 -> inst.botframe = sys._getframe() # hackery to get the right botframe
541 (Pdb) next
542 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
543 -> print(1)
544 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000545 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000546 (Pdb) continue
547 1
548 2
549 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
550 -> print(3)
551 (Pdb) next
552 3
553 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
554 -> print(4)
555 (Pdb) continue
556 4
557 """
558
559
Georg Brandl46b9afc2010-07-30 09:14:20 +0000560def pdb_invoke(method, arg):
561 """Run pdb.method(arg)."""
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700562 getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)
Georg Brandl46b9afc2010-07-30 09:14:20 +0000563
564
565def test_pdb_run_with_incorrect_argument():
566 """Testing run and runeval with incorrect first argument.
567
568 >>> pti = PdbTestInput(['continue',])
569 >>> with pti:
570 ... pdb_invoke('run', lambda x: x)
571 Traceback (most recent call last):
572 TypeError: exec() arg 1 must be a string, bytes or code object
573
574 >>> with pti:
575 ... pdb_invoke('runeval', lambda x: x)
576 Traceback (most recent call last):
577 TypeError: eval() arg 1 must be a string, bytes or code object
578 """
579
580
581def test_pdb_run_with_code_object():
582 """Testing run and runeval with code object as a first argument.
583
Georg Brandle1e8df12010-07-31 08:14:16 +0000584 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS
Georg Brandl46b9afc2010-07-30 09:14:20 +0000585 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
Georg Brandle1e8df12010-07-31 08:14:16 +0000586 > <string>(1)<module>()...
Georg Brandl46b9afc2010-07-30 09:14:20 +0000587 (Pdb) step
588 --Return--
589 > <string>(1)<module>()->None
590 (Pdb) x
591 1
592 (Pdb) continue
593
594 >>> with PdbTestInput(['x', 'continue']):
595 ... x=0
596 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
597 > <string>(1)<module>()->None
598 (Pdb) x
599 1
600 (Pdb) continue
601 """
602
Guido van Rossum8820c232013-11-21 11:30:06 -0800603def test_next_until_return_at_return_event():
604 """Test that pdb stops after a next/until/return issued at a return debug event.
605
606 >>> def test_function_2():
607 ... x = 1
608 ... x = 2
609
610 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700611 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800612 ... test_function_2()
613 ... test_function_2()
614 ... test_function_2()
615 ... end = 1
616
Antoine Pitrouc04d4682014-08-11 21:40:38 -0400617 >>> from bdb import Breakpoint
618 >>> Breakpoint.next = 1
Guido van Rossum8820c232013-11-21 11:30:06 -0800619 >>> with PdbTestInput(['break test_function_2',
620 ... 'continue',
621 ... 'return',
622 ... 'next',
623 ... 'continue',
624 ... 'return',
625 ... 'until',
626 ... 'continue',
627 ... 'return',
628 ... 'return',
629 ... 'continue']):
630 ... test_function()
631 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
632 -> test_function_2()
633 (Pdb) break test_function_2
634 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
635 (Pdb) continue
636 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
637 -> x = 1
638 (Pdb) return
639 --Return--
640 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
641 -> x = 2
642 (Pdb) next
643 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
644 -> test_function_2()
645 (Pdb) continue
646 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
647 -> x = 1
648 (Pdb) return
649 --Return--
650 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
651 -> x = 2
652 (Pdb) until
653 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
654 -> test_function_2()
655 (Pdb) continue
656 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
657 -> x = 1
658 (Pdb) return
659 --Return--
660 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
661 -> x = 2
662 (Pdb) return
663 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
664 -> end = 1
665 (Pdb) continue
666 """
667
668def test_pdb_next_command_for_generator():
669 """Testing skip unwindng stack on yield for generators for "next" command
670
671 >>> def test_gen():
672 ... yield 0
673 ... return 1
674 ... yield 2
675
676 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700677 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800678 ... it = test_gen()
679 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300680 ... if next(it) != 0:
681 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800682 ... next(it)
683 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300684 ... if ex.value != 1:
685 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800686 ... print("finished")
687
688 >>> with PdbTestInput(['step',
689 ... 'step',
690 ... 'step',
691 ... 'next',
692 ... 'next',
693 ... 'step',
694 ... 'step',
695 ... 'continue']):
696 ... test_function()
697 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
698 -> it = test_gen()
699 (Pdb) step
700 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
701 -> try:
702 (Pdb) step
703 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300704 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800705 (Pdb) step
706 --Call--
707 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
708 -> def test_gen():
709 (Pdb) next
710 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
711 -> yield 0
712 (Pdb) next
713 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
714 -> return 1
715 (Pdb) step
716 --Return--
717 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
718 -> return 1
719 (Pdb) step
720 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300721 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800722 -> next(it)
723 (Pdb) continue
724 finished
725 """
726
727def test_pdb_return_command_for_generator():
728 """Testing no unwindng stack on yield for generators
729 for "return" command
730
731 >>> def test_gen():
732 ... yield 0
733 ... return 1
734 ... yield 2
735
736 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700737 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800738 ... it = test_gen()
739 ... try:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300740 ... if next(it) != 0:
741 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800742 ... next(it)
743 ... except StopIteration as ex:
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300744 ... if ex.value != 1:
745 ... raise AssertionError
Guido van Rossum8820c232013-11-21 11:30:06 -0800746 ... print("finished")
747
748 >>> with PdbTestInput(['step',
749 ... 'step',
750 ... 'step',
751 ... 'return',
752 ... 'step',
753 ... 'step',
754 ... 'continue']):
755 ... test_function()
756 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
757 -> it = test_gen()
758 (Pdb) step
759 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
760 -> try:
761 (Pdb) step
762 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300763 -> if next(it) != 0:
Guido van Rossum8820c232013-11-21 11:30:06 -0800764 (Pdb) step
765 --Call--
766 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
767 -> def test_gen():
768 (Pdb) return
769 StopIteration: 1
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300770 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800771 -> next(it)
772 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300773 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
Guido van Rossum8820c232013-11-21 11:30:06 -0800774 -> except StopIteration as ex:
775 (Pdb) step
Serhiy Storchakaa16de5d2015-04-01 16:58:19 +0300776 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
777 -> if ex.value != 1:
Guido van Rossum8820c232013-11-21 11:30:06 -0800778 (Pdb) continue
779 finished
780 """
781
782def test_pdb_until_command_for_generator():
783 """Testing no unwindng stack on yield for generators
784 for "until" command if target breakpoing is not reached
785
786 >>> def test_gen():
787 ... yield 0
788 ... yield 1
789 ... yield 2
790
791 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700792 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800793 ... for i in test_gen():
794 ... print(i)
795 ... print("finished")
796
797 >>> with PdbTestInput(['step',
798 ... 'until 4',
799 ... 'step',
800 ... 'step',
801 ... 'continue']):
802 ... test_function()
803 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
804 -> for i in test_gen():
805 (Pdb) step
806 --Call--
807 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
808 -> def test_gen():
809 (Pdb) until 4
810 0
811 1
812 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
813 -> yield 2
814 (Pdb) step
815 --Return--
816 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
817 -> yield 2
818 (Pdb) step
819 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
820 -> print(i)
821 (Pdb) continue
822 2
823 finished
824 """
825
826def test_pdb_next_command_in_generator_for_loop():
Martin Panter46f50722016-05-26 05:35:26 +0000827 """The next command on returning from a generator controlled by a for loop.
Guido van Rossum8820c232013-11-21 11:30:06 -0800828
829 >>> def test_gen():
830 ... yield 0
831 ... return 1
832
833 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700834 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800835 ... for i in test_gen():
836 ... print('value', i)
837 ... x = 123
838
839 >>> with PdbTestInput(['break test_gen',
840 ... 'continue',
841 ... 'next',
842 ... 'next',
843 ... 'next',
844 ... 'continue']):
845 ... test_function()
846 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
847 -> for i in test_gen():
848 (Pdb) break test_gen
849 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
850 (Pdb) continue
851 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
852 -> yield 0
853 (Pdb) next
854 value 0
855 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
856 -> return 1
857 (Pdb) next
858 Internal StopIteration: 1
859 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
860 -> for i in test_gen():
861 (Pdb) next
862 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
863 -> x = 123
864 (Pdb) continue
865 """
866
867def test_pdb_next_command_subiterator():
868 """The next command in a generator with a subiterator.
869
870 >>> def test_subgenerator():
871 ... yield 0
872 ... return 1
873
874 >>> def test_gen():
875 ... x = yield from test_subgenerator()
876 ... return x
877
878 >>> def test_function():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700879 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
Guido van Rossum8820c232013-11-21 11:30:06 -0800880 ... for i in test_gen():
881 ... print('value', i)
882 ... x = 123
883
884 >>> with PdbTestInput(['step',
885 ... 'step',
886 ... 'next',
887 ... 'next',
888 ... 'next',
889 ... 'continue']):
890 ... test_function()
891 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
892 -> for i in test_gen():
893 (Pdb) step
894 --Call--
895 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
896 -> def test_gen():
897 (Pdb) step
898 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
899 -> x = yield from test_subgenerator()
900 (Pdb) next
901 value 0
902 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
903 -> return x
904 (Pdb) next
905 Internal StopIteration: 1
906 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
907 -> for i in test_gen():
908 (Pdb) next
909 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
910 -> x = 123
911 (Pdb) continue
912 """
913
Xavier de Gaye10e54ae2016-10-12 20:13:24 +0200914def test_pdb_issue_20766():
915 """Test for reference leaks when the SIGINT handler is set.
916
917 >>> def test_function():
918 ... i = 1
919 ... while i <= 2:
920 ... sess = pdb.Pdb()
921 ... sess.set_trace(sys._getframe())
922 ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
923 ... i += 1
924
925 >>> with PdbTestInput(['continue',
926 ... 'continue']):
927 ... test_function()
928 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
929 -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
930 (Pdb) continue
931 pdb 1: <built-in function default_int_handler>
932 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
933 -> sess.set_trace(sys._getframe())
934 (Pdb) continue
935 pdb 2: <built-in function default_int_handler>
936 """
Georg Brandl46b9afc2010-07-30 09:14:20 +0000937
Georg Brandl6cccb862010-07-30 14:16:43 +0000938class PdbTestCase(unittest.TestCase):
939
Senthil Kumaran42d70812012-05-01 10:07:49 +0800940 def run_pdb(self, script, commands):
941 """Run 'script' lines with pdb and the pdb 'commands'."""
942 filename = 'main.py'
943 with open(filename, 'w') as f:
944 f.write(textwrap.dedent(script))
Georg Brandl4bde9ca2012-05-01 09:21:16 +0200945 self.addCleanup(support.unlink, filename)
Victor Stinner047b7ae2014-10-05 17:37:41 +0200946 self.addCleanup(support.rmtree, '__pycache__')
Senthil Kumaran42d70812012-05-01 10:07:49 +0800947 cmd = [sys.executable, '-m', 'pdb', filename]
948 stdout = stderr = None
949 with subprocess.Popen(cmd, stdout=subprocess.PIPE,
950 stdin=subprocess.PIPE,
951 stderr=subprocess.STDOUT,
952 ) as proc:
953 stdout, stderr = proc.communicate(str.encode(commands))
954 stdout = stdout and bytes.decode(stdout)
955 stderr = stderr and bytes.decode(stderr)
956 return stdout, stderr
957
Georg Brandl6e220552013-10-13 20:51:47 +0200958 def _assert_find_function(self, file_content, func_name, expected):
959 file_content = textwrap.dedent(file_content)
960
961 with open(support.TESTFN, 'w') as f:
962 f.write(file_content)
963
964 expected = None if not expected else (
965 expected[0], support.TESTFN, expected[1])
966 self.assertEqual(
967 expected, pdb.find_function(func_name, support.TESTFN))
968
969 def test_find_function_empty_file(self):
970 self._assert_find_function('', 'foo', None)
971
972 def test_find_function_found(self):
973 self._assert_find_function(
974 """\
975 def foo():
976 pass
977
978 def bar():
979 pass
980
981 def quux():
982 pass
983 """,
984 'bar',
985 ('bar', 4),
986 )
987
Georg Brandl6cccb862010-07-30 14:16:43 +0000988 def test_issue7964(self):
989 # open the file as binary so we can force \r\n newline
990 with open(support.TESTFN, 'wb') as f:
991 f.write(b'print("testing my pdb")\r\n')
992 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
993 proc = subprocess.Popen(cmd,
994 stdout=subprocess.PIPE,
995 stdin=subprocess.PIPE,
996 stderr=subprocess.STDOUT,
997 )
Brian Curtin994ad6c2010-11-05 15:38:47 +0000998 self.addCleanup(proc.stdout.close)
Georg Brandl6cccb862010-07-30 14:16:43 +0000999 stdout, stderr = proc.communicate(b'quit\n')
1000 self.assertNotIn(b'SyntaxError', stdout,
1001 "Got a syntax error running test script under PDB")
1002
Senthil Kumaran42d70812012-05-01 10:07:49 +08001003 def test_issue13183(self):
1004 script = """
1005 from bar import bar
1006
1007 def foo():
1008 bar()
1009
1010 def nope():
1011 pass
1012
1013 def foobar():
1014 foo()
1015 nope()
1016
1017 foobar()
1018 """
1019 commands = """
1020 from bar import bar
1021 break bar
1022 continue
1023 step
1024 step
1025 quit
1026 """
1027 bar = """
1028 def bar():
Senthil Kumarancb172042012-05-02 08:00:22 +08001029 pass
Senthil Kumaran42d70812012-05-01 10:07:49 +08001030 """
1031 with open('bar.py', 'w') as f:
1032 f.write(textwrap.dedent(bar))
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001033 self.addCleanup(support.unlink, 'bar.py')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001034 stdout, stderr = self.run_pdb(script, commands)
Georg Brandl4bde9ca2012-05-01 09:21:16 +02001035 self.assertTrue(
1036 any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
1037 'Fail to step into the caller after a return')
Senthil Kumaran42d70812012-05-01 10:07:49 +08001038
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001039 def test_issue13210(self):
1040 # invoking "continue" on a non-main thread triggered an exception
1041 # inside signal.signal
1042
Andrew Svetlov96bc0432012-12-05 15:06:23 +02001043 # raises SkipTest if python was built without threads
1044 support.import_module('threading')
1045
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001046 with open(support.TESTFN, 'wb') as f:
1047 f.write(textwrap.dedent("""
1048 import threading
1049 import pdb
1050
1051 def start_pdb():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001052 pdb.Pdb(readrc=False).set_trace()
Andrew Svetlov539ee5d2012-12-04 21:08:28 +02001053 x = 1
1054 y = 1
1055
1056 t = threading.Thread(target=start_pdb)
1057 t.start()""").encode('ascii'))
1058 cmd = [sys.executable, '-u', support.TESTFN]
1059 proc = subprocess.Popen(cmd,
1060 stdout=subprocess.PIPE,
1061 stdin=subprocess.PIPE,
1062 stderr=subprocess.STDOUT,
1063 )
1064 self.addCleanup(proc.stdout.close)
1065 stdout, stderr = proc.communicate(b'cont\n')
1066 self.assertNotIn('Error', stdout.decode(),
1067 "Got an error running test script under PDB")
1068
Terry Jan Reedyca3f4352015-09-05 19:13:26 -04001069 def test_issue16180(self):
1070 # A syntax error in the debuggee.
1071 script = "def f: pass\n"
1072 commands = ''
1073 expected = "SyntaxError:"
1074 stdout, stderr = self.run_pdb(script, commands)
1075 self.assertIn(expected, stdout,
1076 '\n\nExpected:\n{}\nGot:\n{}\n'
1077 'Fail to handle a syntax error in the debuggee.'
1078 .format(expected, stdout))
1079
1080
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001081 def test_readrc_kwarg(self):
Victor Stinner11ea0442016-09-09 22:56:54 -07001082 script = textwrap.dedent("""
1083 import pdb; pdb.Pdb(readrc=False).set_trace()
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001084
Victor Stinner11ea0442016-09-09 22:56:54 -07001085 print('hello')
1086 """)
Victor Stinner11ea0442016-09-09 22:56:54 -07001087
Victor Stinnerbc626262016-09-09 23:22:09 -07001088 save_home = os.environ.pop('HOME', None)
1089 try:
1090 with support.temp_cwd():
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001091 with open('.pdbrc', 'w') as f:
1092 f.write("invalid\n")
1093
1094 with open('main.py', 'w') as f:
1095 f.write(script)
1096
1097 cmd = [sys.executable, 'main.py']
1098 proc = subprocess.Popen(
1099 cmd,
1100 stdout=subprocess.PIPE,
1101 stdin=subprocess.PIPE,
1102 stderr=subprocess.PIPE,
1103 )
Victor Stinnerbc626262016-09-09 23:22:09 -07001104 with proc:
1105 stdout, stderr = proc.communicate(b'q\n')
1106 self.assertNotIn("NameError: name 'invalid' is not defined",
1107 stdout.decode())
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001108
1109 finally:
Victor Stinner11ea0442016-09-09 22:56:54 -07001110 if save_home is not None:
1111 os.environ['HOME'] = save_home
Łukasz Langa2eb6eca2016-09-09 22:21:17 -07001112
Georg Brandl6cccb862010-07-30 14:16:43 +00001113 def tearDown(self):
1114 support.unlink(support.TESTFN)
1115
1116
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001117def load_tests(*args):
Georg Brandl243ad662009-05-05 09:00:19 +00001118 from test import test_pdb
Xavier de Gaye02e247f2016-10-02 11:42:22 +02001119 suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001120 return unittest.TestSuite(suites)
Georg Brandl243ad662009-05-05 09:00:19 +00001121
1122
1123if __name__ == '__main__':
Andrew Svetlovf0efea02013-03-18 10:09:50 -07001124 unittest.main()