blob: b248c214d0c1416e7b9d023d4d5338ebf8e0ec46 [file] [log] [blame]
Georg Brandl46b9afc2010-07-30 09:14:20 +00001# A test suite for pdb; not very comprehensive at the moment.
Georg Brandl243ad662009-05-05 09:00:19 +00002
3import imp
Georg Brandl6cccb862010-07-30 14:16:43 +00004import pdb
Georg Brandl243ad662009-05-05 09:00:19 +00005import sys
Georg Brandl6cccb862010-07-30 14:16:43 +00006import unittest
7import subprocess
Georg Brandl243ad662009-05-05 09:00:19 +00008
9from test import support
10# This little helper class is essential for testing pdb under doctest.
11from test.test_doctest import _FakeInput
12
13
Georg Brandl9fa2e022009-09-16 16:40:45 +000014class PdbTestInput(object):
15 """Context manager that makes testing Pdb in doctests easier."""
16
17 def __init__(self, input):
18 self.input = input
19
20 def __enter__(self):
21 self.real_stdin = sys.stdin
22 sys.stdin = _FakeInput(self.input)
23
24 def __exit__(self, *exc):
25 sys.stdin = self.real_stdin
26
27
28def test_pdb_displayhook():
29 """This tests the custom displayhook for pdb.
30
31 >>> def test_function(foo, bar):
32 ... import pdb; pdb.Pdb().set_trace()
33 ... pass
34
35 >>> with PdbTestInput([
36 ... 'foo',
37 ... 'bar',
38 ... 'for i in range(5): print(i)',
39 ... 'continue',
40 ... ]):
41 ... test_function(1, None)
42 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
43 -> pass
44 (Pdb) foo
45 1
46 (Pdb) bar
47 (Pdb) for i in range(5): print(i)
48 0
49 1
50 2
51 3
52 4
53 (Pdb) continue
54 """
55
56
Georg Brandl243ad662009-05-05 09:00:19 +000057def test_pdb_skip_modules():
58 """This illustrates the simple case of module skipping.
59
60 >>> def skip_module():
61 ... import string
62 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
63 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +000064
Georg Brandl9fa2e022009-09-16 16:40:45 +000065 >>> with PdbTestInput([
66 ... 'step',
67 ... 'continue',
68 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000069 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +000070 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
71 -> string.capwords('FOO')
72 (Pdb) step
73 --Return--
74 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
75 -> string.capwords('FOO')
76 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +000077 """
Georg Brandl243ad662009-05-05 09:00:19 +000078
79
80# Module for testing skipping of module that makes a callback
81mod = imp.new_module('module_to_skip')
82exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
83
84
85def test_pdb_skip_modules_with_callback():
86 """This illustrates skipping of modules that call into other code.
87
88 >>> def skip_module():
89 ... def callback():
90 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +000091 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +000092 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +000093
Georg Brandl9fa2e022009-09-16 16:40:45 +000094 >>> with PdbTestInput([
95 ... 'step',
96 ... 'step',
97 ... 'step',
98 ... 'step',
99 ... 'step',
100 ... 'continue',
101 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000102 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000103 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000104 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
105 -> mod.foo_pony(callback)
106 (Pdb) step
107 --Call--
108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
109 -> def callback():
110 (Pdb) step
111 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
112 -> return None
113 (Pdb) step
114 --Return--
115 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
116 -> return None
117 (Pdb) step
118 --Return--
119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
120 -> mod.foo_pony(callback)
121 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000122 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
123 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000124 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000125 """
Georg Brandl243ad662009-05-05 09:00:19 +0000126
127
Georg Brandl3f940892010-07-30 10:29:19 +0000128def test_pdb_continue_in_bottomframe():
129 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
130
131 >>> def test_function():
132 ... import pdb, sys; inst = pdb.Pdb()
133 ... inst.set_trace()
134 ... inst.botframe = sys._getframe() # hackery to get the right botframe
135 ... print(1)
136 ... print(2)
137 ... print(3)
138 ... print(4)
139
Georg Brandl7410dd12010-07-30 12:01:20 +0000140 >>> with PdbTestInput([ # doctest: +ELLIPSIS
Georg Brandl3f940892010-07-30 10:29:19 +0000141 ... 'next',
142 ... 'break 7',
143 ... 'continue',
144 ... 'next',
145 ... 'continue',
146 ... 'continue',
147 ... ]):
148 ... test_function()
149 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
150 -> inst.botframe = sys._getframe() # hackery to get the right botframe
151 (Pdb) next
152 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
153 -> print(1)
154 (Pdb) break 7
Georg Brandl7410dd12010-07-30 12:01:20 +0000155 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
Georg Brandl3f940892010-07-30 10:29:19 +0000156 (Pdb) continue
157 1
158 2
159 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
160 -> print(3)
161 (Pdb) next
162 3
163 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
164 -> print(4)
165 (Pdb) continue
166 4
167 """
168
169
Georg Brandl7410dd12010-07-30 12:01:20 +0000170def test_pdb_breakpoints():
171 """Test handling of breakpoints.
172
173 >>> def test_function():
174 ... import pdb; pdb.Pdb().set_trace()
175 ... print(1)
176 ... print(2)
177 ... print(3)
178 ... print(4)
179
180 First, need to clear bdb state that might be left over from previous tests.
181 Otherwise, the new breakpoints might get assigned different numbers.
182
183 >>> from bdb import Breakpoint
184 >>> Breakpoint.next = 1
185 >>> Breakpoint.bplist = {}
186 >>> Breakpoint.bpbynumber = [None]
187
188 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
189 the breakpoint list outputs a tab for the "stop only" and "ignore next"
190 lines, which we don't want to put in here.
191
192 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
193 ... 'break 3',
194 ... 'disable 1',
195 ... 'ignore 1 10',
196 ... 'condition 1 1 < 2',
197 ... 'break 4',
198 ... 'break',
199 ... 'condition 1',
200 ... 'enable 1',
201 ... 'clear 1',
202 ... 'commands 2',
203 ... 'print 42',
204 ... 'end',
205 ... 'continue', # will stop at breakpoint 2
206 ... 'continue',
207 ... ]):
208 ... test_function()
209 > <doctest test.test_pdb.test_pdb_breakpoints[0]>(3)test_function()
210 -> print(1)
211 (Pdb) break 3
212 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
213 (Pdb) disable 1
214 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
215 (Pdb) ignore 1 10
216 Will ignore next 10 crossings of breakpoint 1.
217 (Pdb) condition 1 1 < 2
218 New condition set for breakpoint 1.
219 (Pdb) break 4
220 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4
221 (Pdb) break
222 Num Type Disp Enb Where
223 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
224 stop only if 1 < 2
225 ignore next 10 hits
226 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4
227 (Pdb) condition 1
228 Breakpoint 1 is now unconditional.
229 (Pdb) enable 1
230 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
231 (Pdb) clear 1
232 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3
233 (Pdb) commands 2
234 (com) print 42
235 (com) end
236 (Pdb) continue
237 1
238 42
239 > <doctest test.test_pdb.test_pdb_breakpoints[0]>(4)test_function()
240 -> print(2)
241 (Pdb) continue
242 2
243 3
244 4
245 """
246
247
Georg Brandl46b9afc2010-07-30 09:14:20 +0000248def pdb_invoke(method, arg):
249 """Run pdb.method(arg)."""
250 import pdb; getattr(pdb, method)(arg)
251
252
253def test_pdb_run_with_incorrect_argument():
254 """Testing run and runeval with incorrect first argument.
255
256 >>> pti = PdbTestInput(['continue',])
257 >>> with pti:
258 ... pdb_invoke('run', lambda x: x)
259 Traceback (most recent call last):
260 TypeError: exec() arg 1 must be a string, bytes or code object
261
262 >>> with pti:
263 ... pdb_invoke('runeval', lambda x: x)
264 Traceback (most recent call last):
265 TypeError: eval() arg 1 must be a string, bytes or code object
266 """
267
268
269def test_pdb_run_with_code_object():
270 """Testing run and runeval with code object as a first argument.
271
272 >>> with PdbTestInput(['step','x', 'continue']):
273 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
274 > <string>(1)<module>()
275 (Pdb) step
276 --Return--
277 > <string>(1)<module>()->None
278 (Pdb) x
279 1
280 (Pdb) continue
281
282 >>> with PdbTestInput(['x', 'continue']):
283 ... x=0
284 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
285 > <string>(1)<module>()->None
286 (Pdb) x
287 1
288 (Pdb) continue
289 """
290
291
Georg Brandl6cccb862010-07-30 14:16:43 +0000292class PdbTestCase(unittest.TestCase):
293
294 def test_issue7964(self):
295 # open the file as binary so we can force \r\n newline
296 with open(support.TESTFN, 'wb') as f:
297 f.write(b'print("testing my pdb")\r\n')
298 cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
299 proc = subprocess.Popen(cmd,
300 stdout=subprocess.PIPE,
301 stdin=subprocess.PIPE,
302 stderr=subprocess.STDOUT,
303 )
304 stdout, stderr = proc.communicate(b'quit\n')
305 self.assertNotIn(b'SyntaxError', stdout,
306 "Got a syntax error running test script under PDB")
307
308 def tearDown(self):
309 support.unlink(support.TESTFN)
310
311
Georg Brandl243ad662009-05-05 09:00:19 +0000312def test_main():
313 from test import test_pdb
314 support.run_doctest(test_pdb, verbosity=True)
Georg Brandl6cccb862010-07-30 14:16:43 +0000315 support.run_unittest(PdbTestCase)
Georg Brandl243ad662009-05-05 09:00:19 +0000316
317
318if __name__ == '__main__':
319 test_main()