1212from test .test_doctest import _FakeInput
1313
1414
15+ class PdbTestInput (object ):
16+ """Context manager that makes testing Pdb in doctests easier."""
17+
18+ def __init__ (self , input ):
19+ self .input = input
20+
21+ def __enter__ (self ):
22+ self .real_stdin = sys .stdin
23+ sys .stdin = _FakeInput (self .input )
24+
25+ def __exit__ (self , * exc ):
26+ sys .stdin = self .real_stdin
27+
28+
29+ def test_pdb_displayhook ():
30+ """This tests the custom displayhook for pdb.
31+
32+ >>> def test_function(foo, bar):
33+ ... import pdb; pdb.Pdb().set_trace()
34+ ... pass
35+
36+ >>> with PdbTestInput([
37+ ... 'foo',
38+ ... 'bar',
39+ ... 'for i in range(5): print(i)',
40+ ... 'continue',
41+ ... ]):
42+ ... test_function(1, None)
43+ > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
44+ -> pass
45+ (Pdb) foo
46+ 1
47+ (Pdb) bar
48+ (Pdb) for i in range(5): print(i)
49+ 0
50+ 1
51+ 2
52+ 3
53+ 4
54+ (Pdb) continue
55+ """
56+
57+
1558def test_pdb_skip_modules ():
1659 """This illustrates the simple case of module skipping.
1760
1861 >>> def skip_module():
1962 ... import string
2063 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
2164 ... string.capwords('FOO')
22- >>> real_stdin = sys.stdin
23- >>> sys.stdin = _FakeInput([
24- ... 'step',
25- ... 'continue',
26- ... ])
2765
28- >>> try:
66+ >>> with PdbTestInput([
67+ ... 'step',
68+ ... 'continue',
69+ ... ]):
2970 ... skip_module()
30- ... finally:
31- ... sys.stdin = real_stdin
3271 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
3372 -> string.capwords('FOO')
3473 (Pdb) step
3574 --Return--
3675 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
3776 -> string.capwords('FOO')
3877 (Pdb) continue
39- """
78+ """
4079
4180
4281# Module for testing skipping of module that makes a callback
@@ -50,22 +89,19 @@ def test_pdb_skip_modules_with_callback():
5089 >>> def skip_module():
5190 ... def callback():
5291 ... return None
53- ... import pdb;pdb.Pdb(skip=['module_to_skip*']).set_trace()
92+ ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
5493 ... mod.foo_pony(callback)
55- >>> real_stdin = sys.stdin
56- >>> sys.stdin = _FakeInput([
57- ... 'step',
58- ... 'step',
59- ... 'step',
60- ... 'step',
61- ... 'step',
62- ... 'continue',
63- ... ])
64-
65- >>> try:
94+
95+ >>> with PdbTestInput([
96+ ... 'step',
97+ ... 'step',
98+ ... 'step',
99+ ... 'step',
100+ ... 'step',
101+ ... 'continue',
102+ ... ]):
66103 ... skip_module()
67- ... finally:
68- ... sys.stdin = real_stdin
104+ ... pass # provides something to "step" to
69105 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
70106 -> mod.foo_pony(callback)
71107 (Pdb) step
@@ -84,10 +120,10 @@ def test_pdb_skip_modules_with_callback():
84120 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
85121 -> mod.foo_pony(callback)
86122 (Pdb) step
87- > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[3 ]>(4 )<module>()
88- -> sys.stdin = real_stdin
123+ > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1 ]>(10 )<module>()
124+ -> pass # provides something to "step" to
89125 (Pdb) continue
90- """
126+ """
91127
92128
93129def test_main ():
0 commit comments