blob: 5c1a37c280a33f7f9cf4021ca58ef1b7f72050ba [file] [log] [blame]
Georg Brandl4d4313d2009-05-05 08:54:11 +00001# A test suite for pdb; at the moment, this only validates skipping of
2# specified test modules (RFE #5142).
3
4import imp
Georg Brandl4d4313d2009-05-05 08:54:11 +00005import sys
Jason R. Coombs5d032c02011-11-17 18:03:24 -05006import os
7import unittest
8import subprocess
Senthil Kumaran7f6d4e12012-05-01 10:36:28 +08009import textwrap
Georg Brandl4d4313d2009-05-05 08:54:11 +000010
11from test import test_support
12# This little helper class is essential for testing pdb under doctest.
13from test_doctest import _FakeInput
14
15
Senthil Kumaran7f6d4e12012-05-01 10:36:28 +080016class PdbTestCase(unittest.TestCase):
17
18 def run_pdb(self, script, commands):
19 """Run 'script' lines with pdb and the pdb 'commands'."""
20 filename = 'main.py'
21 with open(filename, 'w') as f:
22 f.write(textwrap.dedent(script))
23 cmd = [sys.executable, '-m', 'pdb', filename]
24 stdout = stderr = None
25 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
26 stdin=subprocess.PIPE,
27 stderr=subprocess.STDOUT,
28 )
29 stdout, stderr = proc.communicate(commands)
30 proc.stdout.close()
31 proc.stdin.close()
32 return stdout, stderr
33
34 def test_issue13183(self):
35 script = """
36 from bar import bar
37
38 def foo():
39 bar()
40
41 def nope():
42 pass
43
44 def foobar():
45 foo()
46 nope()
47
48 foobar()
49 """
50 commands = """
51 from bar import bar
52 break bar
53 continue
54 step
55 step
56 quit
57 """
58 bar = """
59 def bar():
Senthil Kumarana9e18cd2012-05-02 07:59:36 +080060 pass
Senthil Kumaran7f6d4e12012-05-01 10:36:28 +080061 """
62 with open('bar.py', 'w') as f:
63 f.write(textwrap.dedent(bar))
64 stdout, stderr = self.run_pdb(script, commands)
65 self.assertIn('main.py(5)foo()->None', stdout.split('\n')[-3],
66 'Fail to step into the caller after a return')
67
68
Georg Brandl6c39f062009-09-16 16:22:12 +000069class PdbTestInput(object):
70 """Context manager that makes testing Pdb in doctests easier."""
71
72 def __init__(self, input):
73 self.input = input
74
75 def __enter__(self):
76 self.real_stdin = sys.stdin
77 sys.stdin = _FakeInput(self.input)
78
79 def __exit__(self, *exc):
80 sys.stdin = self.real_stdin
81
82
Georg Brandl69dfe8d2009-09-16 16:36:39 +000083def write(x):
84 print x
85
86def test_pdb_displayhook():
87 """This tests the custom displayhook for pdb.
88
89 >>> def test_function(foo, bar):
90 ... import pdb; pdb.Pdb().set_trace()
91 ... pass
92
93 >>> with PdbTestInput([
94 ... 'foo',
95 ... 'bar',
96 ... 'for i in range(5): write(i)',
97 ... 'continue',
98 ... ]):
99 ... test_function(1, None)
100 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
101 -> pass
102 (Pdb) foo
103 1
104 (Pdb) bar
105 (Pdb) for i in range(5): write(i)
106 0
107 1
108 2
109 3
110 4
111 (Pdb) continue
112 """
113
Senthil Kumaran9a5897b2010-11-29 12:41:03 +0000114def test_pdb_breakpoint_commands():
115 """Test basic commands related to breakpoints.
116
117 >>> def test_function():
118 ... import pdb; pdb.Pdb().set_trace()
119 ... print(1)
120 ... print(2)
121 ... print(3)
122 ... print(4)
123
124 First, need to clear bdb state that might be left over from previous tests.
125 Otherwise, the new breakpoints might get assigned different numbers.
126
127 >>> from bdb import Breakpoint
128 >>> Breakpoint.next = 1
129 >>> Breakpoint.bplist = {}
130 >>> Breakpoint.bpbynumber = [None]
131
132 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
133 the breakpoint list outputs a tab for the "stop only" and "ignore next"
134 lines, which we don't want to put in here.
135
136 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
137 ... 'break 3',
138 ... 'disable 1',
139 ... 'ignore 1 10',
140 ... 'condition 1 1 < 2',
141 ... 'break 4',
142 ... 'break 4',
143 ... 'break',
144 ... 'clear 3',
145 ... 'break',
146 ... 'condition 1',
147 ... 'enable 1',
148 ... 'clear 1',
149 ... 'commands 2',
150 ... 'print 42',
151 ... 'end',
152 ... 'continue', # will stop at breakpoint 2 (line 4)
153 ... 'clear', # clear all!
154 ... 'y',
155 ... 'tbreak 5',
156 ... 'continue', # will stop at temporary breakpoint
157 ... 'break', # make sure breakpoint is gone
158 ... 'continue',
159 ... ]):
160 ... test_function()
161 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
162 -> print(1)
163 (Pdb) break 3
164 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
165 (Pdb) disable 1
166 (Pdb) ignore 1 10
167 Will ignore next 10 crossings of breakpoint 1.
168 (Pdb) condition 1 1 < 2
169 (Pdb) break 4
170 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
171 (Pdb) break 4
172 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
173 (Pdb) break
174 Num Type Disp Enb Where
175 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
176 stop only if 1 < 2
177 ignore next 10 hits
178 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
179 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
180 (Pdb) clear 3
181 Deleted breakpoint 3
182 (Pdb) break
183 Num Type Disp Enb Where
184 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
185 stop only if 1 < 2
186 ignore next 10 hits
187 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
188 (Pdb) condition 1
189 Breakpoint 1 is now unconditional.
190 (Pdb) enable 1
191 (Pdb) clear 1
192 Deleted breakpoint 1
193 (Pdb) commands 2
194 (com) print 42
195 (com) end
196 (Pdb) continue
197 1
198 42
199 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
200 -> print(2)
201 (Pdb) clear
202 Clear all breaks? y
203 (Pdb) tbreak 5
204 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
205 (Pdb) continue
206 2
207 Deleted breakpoint 4
208 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
209 -> print(3)
210 (Pdb) break
211 (Pdb) continue
212 3
213 4
214 """
215
Georg Brandl69dfe8d2009-09-16 16:36:39 +0000216
Georg Brandl4d4313d2009-05-05 08:54:11 +0000217def test_pdb_skip_modules():
218 """This illustrates the simple case of module skipping.
219
220 >>> def skip_module():
221 ... import string
Georg Brandl6c39f062009-09-16 16:22:12 +0000222 ... import pdb; pdb.Pdb(skip=['string*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +0000223 ... string.lower('FOO')
Georg Brandl4d4313d2009-05-05 08:54:11 +0000224
Georg Brandl6c39f062009-09-16 16:22:12 +0000225 >>> with PdbTestInput([
226 ... 'step',
Georg Brandl69dfe8d2009-09-16 16:36:39 +0000227 ... 'continue',
Georg Brandl6c39f062009-09-16 16:22:12 +0000228 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +0000229 ... skip_module()
Georg Brandl4d4313d2009-05-05 08:54:11 +0000230 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
231 -> string.lower('FOO')
232 (Pdb) step
233 --Return--
234 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
235 -> string.lower('FOO')
236 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +0000237 """
Georg Brandl4d4313d2009-05-05 08:54:11 +0000238
239
240# Module for testing skipping of module that makes a callback
241mod = imp.new_module('module_to_skip')
242exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__
243
244
245def test_pdb_skip_modules_with_callback():
246 """This illustrates skipping of modules that call into other code.
247
248 >>> def skip_module():
249 ... def callback():
250 ... return None
Georg Brandl6c39f062009-09-16 16:22:12 +0000251 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +0000252 ... mod.foo_pony(callback)
Georg Brandl4d4313d2009-05-05 08:54:11 +0000253
Georg Brandl6c39f062009-09-16 16:22:12 +0000254 >>> with PdbTestInput([
255 ... 'step',
256 ... 'step',
257 ... 'step',
258 ... 'step',
259 ... 'step',
260 ... 'continue',
261 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +0000262 ... skip_module()
Georg Brandl6c39f062009-09-16 16:22:12 +0000263 ... pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +0000264 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
265 -> mod.foo_pony(callback)
266 (Pdb) step
267 --Call--
268 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
269 -> def callback():
270 (Pdb) step
271 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
272 -> return None
273 (Pdb) step
274 --Return--
275 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
276 -> return None
277 (Pdb) step
278 --Return--
279 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
280 -> mod.foo_pony(callback)
281 (Pdb) step
Georg Brandl6c39f062009-09-16 16:22:12 +0000282 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
283 -> pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +0000284 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +0000285 """
Georg Brandl4d4313d2009-05-05 08:54:11 +0000286
287
Georg Brandl50775992010-08-01 19:33:15 +0000288def test_pdb_continue_in_bottomframe():
289 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
290
291 >>> def test_function():
292 ... import pdb, sys; inst = pdb.Pdb()
293 ... inst.set_trace()
294 ... inst.botframe = sys._getframe() # hackery to get the right botframe
295 ... print(1)
296 ... print(2)
297 ... print(3)
298 ... print(4)
299
Senthil Kumaran9a5897b2010-11-29 12:41:03 +0000300 First, need to clear bdb state that might be left over from previous tests.
301 Otherwise, the new breakpoints might get assigned different numbers.
302
303 >>> from bdb import Breakpoint
304 >>> Breakpoint.next = 1
305 >>> Breakpoint.bplist = {}
306 >>> Breakpoint.bpbynumber = [None]
307
Georg Brandl50775992010-08-01 19:33:15 +0000308 >>> with PdbTestInput([
309 ... 'next',
310 ... 'break 7',
311 ... 'continue',
312 ... 'next',
313 ... 'continue',
314 ... 'continue',
315 ... ]):
316 ... test_function()
317 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
318 -> inst.botframe = sys._getframe() # hackery to get the right botframe
319 (Pdb) next
320 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
321 -> print(1)
322 (Pdb) break 7
323 Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
324 (Pdb) continue
325 1
326 2
327 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
328 -> print(3)
329 (Pdb) next
330 3
331 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
332 -> print(4)
333 (Pdb) continue
334 4
335 """
336
Jason R. Coombs77cd2582011-12-08 22:14:56 -0500337class ModuleInitTester(unittest.TestCase):
Jason R. Coombs5d032c02011-11-17 18:03:24 -0500338
Jason R. Coombs77cd2582011-12-08 22:14:56 -0500339 def test_filename_correct(self):
340 """
341 In issue 7750, it was found that if the filename has a sequence that
342 resolves to an escape character in a Python string (such as \t), it
343 will be treated as the escaped character.
344 """
345 # the test_fn must contain something like \t
346 # on Windows, this will create 'test_mod.py' in the current directory.
347 # on Unix, this will create '.\test_mod.py' in the current directory.
348 test_fn = '.\\test_mod.py'
349 code = 'print("testing pdb")'
350 with open(test_fn, 'w') as f:
351 f.write(code)
352 self.addCleanup(os.remove, test_fn)
353 cmd = [sys.executable, '-m', 'pdb', test_fn,]
Jason R. Coombs5d032c02011-11-17 18:03:24 -0500354 proc = subprocess.Popen(cmd,
355 stdout=subprocess.PIPE,
356 stdin=subprocess.PIPE,
357 stderr=subprocess.STDOUT,
358 )
359 stdout, stderr = proc.communicate('quit\n')
Jason R. Coombs77cd2582011-12-08 22:14:56 -0500360 self.assertIn(code, stdout, "pdb munged the filename")
Jason R. Coombs5d032c02011-11-17 18:03:24 -0500361
Georg Brandl50775992010-08-01 19:33:15 +0000362
Georg Brandl4d4313d2009-05-05 08:54:11 +0000363def test_main():
364 from test import test_pdb
365 test_support.run_doctest(test_pdb, verbosity=True)
Senthil Kumaran7f6d4e12012-05-01 10:36:28 +0800366 test_support.run_unittest(
367 PdbTestCase,
368 ModuleInitTester)
Georg Brandl4d4313d2009-05-05 08:54:11 +0000369
370if __name__ == '__main__':
371 test_main()