The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_pdb.py
blob6017edc5e2e9192ac1b371f91d2fcbada0467976
1 # A test suite for pdb; at the moment, this only validates skipping of
2 # specified test modules (RFE #5142).
4 import imp
5 import os
6 import sys
7 import doctest
8 import tempfile
10 from test import test_support
11 # This little helper class is essential for testing pdb under doctest.
12 from test_doctest import _FakeInput
15 class PdbTestInput(object):
16 """Context manager that makes testing Pdb in doctests easier."""
18 def __init__(self, input):
19 self.input = input
21 def __enter__(self):
22 self.real_stdin = sys.stdin
23 sys.stdin = _FakeInput(self.input)
25 def __exit__(self, *exc):
26 sys.stdin = self.real_stdin
29 def write(x):
30 print x
32 def test_pdb_displayhook():
33 """This tests the custom displayhook for pdb.
35 >>> def test_function(foo, bar):
36 ... import pdb; pdb.Pdb().set_trace()
37 ... pass
39 >>> with PdbTestInput([
40 ... 'foo',
41 ... 'bar',
42 ... 'for i in range(5): write(i)',
43 ... 'continue',
44 ... ]):
45 ... test_function(1, None)
46 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
47 -> pass
48 (Pdb) foo
50 (Pdb) bar
51 (Pdb) for i in range(5): write(i)
57 (Pdb) continue
58 """
61 def test_pdb_skip_modules():
62 """This illustrates the simple case of module skipping.
64 >>> def skip_module():
65 ... import string
66 ... import pdb; pdb.Pdb(skip=['string*']).set_trace()
67 ... string.lower('FOO')
69 >>> with PdbTestInput([
70 ... 'step',
71 ... 'continue',
72 ... ]):
73 ... skip_module()
74 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
75 -> string.lower('FOO')
76 (Pdb) step
77 --Return--
78 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
79 -> string.lower('FOO')
80 (Pdb) continue
81 """
84 # Module for testing skipping of module that makes a callback
85 mod = imp.new_module('module_to_skip')
86 exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__
89 def test_pdb_skip_modules_with_callback():
90 """This illustrates skipping of modules that call into other code.
92 >>> def skip_module():
93 ... def callback():
94 ... return None
95 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
96 ... mod.foo_pony(callback)
98 >>> with PdbTestInput([
99 ... 'step',
100 ... 'step',
101 ... 'step',
102 ... 'step',
103 ... 'step',
104 ... 'continue',
105 ... ]):
106 ... skip_module()
107 ... pass # provides something to "step" to
108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
109 -> mod.foo_pony(callback)
110 (Pdb) step
111 --Call--
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
113 -> def callback():
114 (Pdb) step
115 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
116 -> return None
117 (Pdb) step
118 --Return--
119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
120 -> return None
121 (Pdb) step
122 --Return--
123 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
124 -> mod.foo_pony(callback)
125 (Pdb) step
126 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
127 -> pass # provides something to "step" to
128 (Pdb) continue
132 def test_main():
133 from test import test_pdb
134 test_support.run_doctest(test_pdb, verbosity=True)
137 if __name__ == '__main__':
138 test_main()