Show More
@@ -1,186 +1,233 b'' | |||
|
1 | 1 | """Tests for debugging machinery. |
|
2 | 2 | """ |
|
3 | 3 | from __future__ import print_function |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2012, The IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | import sys |
|
17 | 17 | |
|
18 | 18 | # third-party |
|
19 | 19 | import nose.tools as nt |
|
20 | 20 | |
|
21 | 21 | # Our own |
|
22 | 22 | from IPython.core import debugger |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Helper classes, from CPython's Pdb test suite |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | class _FakeInput(object): |
|
29 | 29 | """ |
|
30 | 30 | A fake input stream for pdb's interactive debugger. Whenever a |
|
31 | 31 | line is read, print it (to simulate the user typing it), and then |
|
32 | 32 | return it. The set of lines to return is specified in the |
|
33 | 33 | constructor; they should not have trailing newlines. |
|
34 | 34 | """ |
|
35 | 35 | def __init__(self, lines): |
|
36 | 36 | self.lines = iter(lines) |
|
37 | 37 | |
|
38 | 38 | def readline(self): |
|
39 | 39 | line = next(self.lines) |
|
40 | 40 | print(line) |
|
41 | 41 | return line+'\n' |
|
42 | 42 | |
|
43 | 43 | class PdbTestInput(object): |
|
44 | 44 | """Context manager that makes testing Pdb in doctests easier.""" |
|
45 | 45 | |
|
46 | 46 | def __init__(self, input): |
|
47 | 47 | self.input = input |
|
48 | 48 | |
|
49 | 49 | def __enter__(self): |
|
50 | 50 | self.real_stdin = sys.stdin |
|
51 | 51 | sys.stdin = _FakeInput(self.input) |
|
52 | 52 | |
|
53 | 53 | def __exit__(self, *exc): |
|
54 | 54 | sys.stdin = self.real_stdin |
|
55 | 55 | |
|
56 | 56 | #----------------------------------------------------------------------------- |
|
57 | 57 | # Tests |
|
58 | 58 | #----------------------------------------------------------------------------- |
|
59 | 59 | |
|
60 | 60 | def test_longer_repr(): |
|
61 | 61 | try: |
|
62 | 62 | from reprlib import repr as trepr # Py 3 |
|
63 | 63 | except ImportError: |
|
64 | 64 | from repr import repr as trepr # Py 2 |
|
65 | 65 | |
|
66 | 66 | a = '1234567890'* 7 |
|
67 | 67 | ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'" |
|
68 | 68 | a_trunc = "'123456789012...8901234567890'" |
|
69 | 69 | nt.assert_equal(trepr(a), a_trunc) |
|
70 | 70 | # The creation of our tracer modifies the repr module's repr function |
|
71 | 71 | # in-place, since that global is used directly by the stdlib's pdb module. |
|
72 |
|
|
|
72 | debugger.Tracer() | |
|
73 | 73 | nt.assert_equal(trepr(a), ar) |
|
74 | 74 | |
|
75 | 75 | def test_ipdb_magics(): |
|
76 | 76 | '''Test calling some IPython magics from ipdb. |
|
77 | 77 | |
|
78 | 78 | First, set up some test functions and classes which we can inspect. |
|
79 | 79 | |
|
80 | 80 | >>> class ExampleClass(object): |
|
81 | 81 | ... """Docstring for ExampleClass.""" |
|
82 | 82 | ... def __init__(self): |
|
83 | 83 | ... """Docstring for ExampleClass.__init__""" |
|
84 | 84 | ... pass |
|
85 | 85 | ... def __str__(self): |
|
86 | 86 | ... return "ExampleClass()" |
|
87 | 87 | |
|
88 | 88 | >>> def example_function(x, y, z="hello"): |
|
89 | 89 | ... """Docstring for example_function.""" |
|
90 | 90 | ... pass |
|
91 | 91 | |
|
92 | 92 | >>> old_trace = sys.gettrace() |
|
93 | 93 | |
|
94 | 94 | Create a function which triggers ipdb. |
|
95 | 95 | |
|
96 | 96 | >>> def trigger_ipdb(): |
|
97 | 97 | ... a = ExampleClass() |
|
98 | 98 | ... debugger.Pdb().set_trace() |
|
99 | 99 | |
|
100 | 100 | >>> with PdbTestInput([ |
|
101 | 101 | ... 'pdef example_function', |
|
102 | 102 | ... 'pdoc ExampleClass', |
|
103 | 103 | ... 'up', |
|
104 | 104 | ... 'down', |
|
105 | 105 | ... 'list', |
|
106 | 106 | ... 'pinfo a', |
|
107 | 107 | ... 'll', |
|
108 | 108 | ... 'continue', |
|
109 | 109 | ... ]): |
|
110 | 110 | ... trigger_ipdb() |
|
111 | 111 | --Return-- |
|
112 | 112 | None |
|
113 | 113 | > <doctest ...>(3)trigger_ipdb() |
|
114 | 114 | 1 def trigger_ipdb(): |
|
115 | 115 | 2 a = ExampleClass() |
|
116 | 116 | ----> 3 debugger.Pdb().set_trace() |
|
117 | 117 | <BLANKLINE> |
|
118 | 118 | ipdb> pdef example_function |
|
119 | 119 | example_function(x, y, z='hello') |
|
120 | 120 | ipdb> pdoc ExampleClass |
|
121 | 121 | Class docstring: |
|
122 | 122 | Docstring for ExampleClass. |
|
123 | 123 | Init docstring: |
|
124 | 124 | Docstring for ExampleClass.__init__ |
|
125 | 125 | ipdb> up |
|
126 | 126 | > <doctest ...>(11)<module>() |
|
127 | 127 | 7 'pinfo a', |
|
128 | 128 | 8 'll', |
|
129 | 129 | 9 'continue', |
|
130 | 130 | 10 ]): |
|
131 | 131 | ---> 11 trigger_ipdb() |
|
132 | 132 | <BLANKLINE> |
|
133 | 133 | ipdb> down |
|
134 | 134 | None |
|
135 | 135 | > <doctest ...>(3)trigger_ipdb() |
|
136 | 136 | 1 def trigger_ipdb(): |
|
137 | 137 | 2 a = ExampleClass() |
|
138 | 138 | ----> 3 debugger.Pdb().set_trace() |
|
139 | 139 | <BLANKLINE> |
|
140 | 140 | ipdb> list |
|
141 | 141 | 1 def trigger_ipdb(): |
|
142 | 142 | 2 a = ExampleClass() |
|
143 | 143 | ----> 3 debugger.Pdb().set_trace() |
|
144 | 144 | <BLANKLINE> |
|
145 | 145 | ipdb> pinfo a |
|
146 | 146 | Type: ExampleClass |
|
147 | 147 | String form: ExampleClass() |
|
148 | 148 | Namespace: Local... |
|
149 | 149 | Docstring: Docstring for ExampleClass. |
|
150 | 150 | Init docstring: Docstring for ExampleClass.__init__ |
|
151 | 151 | ipdb> ll |
|
152 | 152 | 1 def trigger_ipdb(): |
|
153 | 153 | 2 a = ExampleClass() |
|
154 | 154 | ----> 3 debugger.Pdb().set_trace() |
|
155 | 155 | <BLANKLINE> |
|
156 | 156 | ipdb> continue |
|
157 | 157 | |
|
158 | 158 | Restore previous trace function, e.g. for coverage.py |
|
159 | 159 | |
|
160 | 160 | >>> sys.settrace(old_trace) |
|
161 | 161 | ''' |
|
162 | 162 | |
|
163 | 163 | def test_ipdb_magics2(): |
|
164 | 164 | '''Test ipdb with a very short function. |
|
165 | 165 | |
|
166 | 166 | >>> old_trace = sys.gettrace() |
|
167 | 167 | |
|
168 | 168 | >>> def bar(): |
|
169 | 169 | ... pass |
|
170 | 170 | |
|
171 | 171 | Run ipdb. |
|
172 | 172 | |
|
173 | 173 | >>> with PdbTestInput([ |
|
174 | 174 | ... 'continue', |
|
175 | 175 | ... ]): |
|
176 | 176 | ... debugger.Pdb().runcall(bar) |
|
177 | 177 | > <doctest ...>(2)bar() |
|
178 | 178 | 1 def bar(): |
|
179 | 179 | ----> 2 pass |
|
180 | 180 | <BLANKLINE> |
|
181 | 181 | ipdb> continue |
|
182 | 182 | |
|
183 | 183 | Restore previous trace function, e.g. for coverage.py |
|
184 | 184 | |
|
185 | 185 | >>> sys.settrace(old_trace) |
|
186 | 186 | ''' |
|
187 | ||
|
188 | def can_quit(): | |
|
189 | '''Test that quit work in ipydb | |
|
190 | ||
|
191 | >>> old_trace = sys.gettrace() | |
|
192 | ||
|
193 | >>> def bar(): | |
|
194 | ... pass | |
|
195 | ||
|
196 | >>> with PdbTestInput([ | |
|
197 | ... 'quit', | |
|
198 | ... ]): | |
|
199 | ... debugger.Pdb().runcall(bar) | |
|
200 | > <doctest ...>(2)bar() | |
|
201 | 1 def bar(): | |
|
202 | ----> 2 pass | |
|
203 | <BLANKLINE> | |
|
204 | ipdb> quit | |
|
205 | ||
|
206 | Restore previous trace function, e.g. for coverage.py | |
|
207 | ||
|
208 | >>> sys.settrace(old_trace) | |
|
209 | ''' | |
|
210 | ||
|
211 | ||
|
212 | def can_exit(): | |
|
213 | '''Test that quit work in ipydb | |
|
214 | ||
|
215 | >>> old_trace = sys.gettrace() | |
|
216 | ||
|
217 | >>> def bar(): | |
|
218 | ... pass | |
|
219 | ||
|
220 | >>> with PdbTestInput([ | |
|
221 | ... 'exit', | |
|
222 | ... ]): | |
|
223 | ... debugger.Pdb().runcall(bar) | |
|
224 | > <doctest ...>(2)bar() | |
|
225 | 1 def bar(): | |
|
226 | ----> 2 pass | |
|
227 | <BLANKLINE> | |
|
228 | ipdb> exit | |
|
229 | ||
|
230 | Restore previous trace function, e.g. for coverage.py | |
|
231 | ||
|
232 | >>> sys.settrace(old_trace) | |
|
233 | ''' |
General Comments 0
You need to be logged in to leave comments.
Login now