Show More
@@ -1,567 +1,568 | |||||
1 | """Tests for debugging machinery. |
|
1 | """Tests for debugging machinery. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | import bdb |
|
7 | import bdb | |
8 | import builtins |
|
8 | import builtins | |
9 | import os |
|
9 | import os | |
10 | import sys |
|
10 | import sys | |
11 | import platform |
|
11 | import platform | |
12 |
|
12 | |||
13 | from tempfile import NamedTemporaryFile |
|
13 | from tempfile import NamedTemporaryFile | |
14 | from textwrap import dedent |
|
14 | from textwrap import dedent | |
15 | from unittest.mock import patch |
|
15 | from unittest.mock import patch | |
16 |
|
16 | |||
17 | from IPython.core import debugger |
|
17 | from IPython.core import debugger | |
18 | from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE |
|
18 | from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE | |
19 | from IPython.testing.decorators import skip_win32 |
|
19 | from IPython.testing.decorators import skip_win32 | |
20 | import pytest |
|
20 | import pytest | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Helper classes, from CPython's Pdb test suite |
|
23 | # Helper classes, from CPython's Pdb test suite | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | class _FakeInput(object): |
|
26 | class _FakeInput(object): | |
27 | """ |
|
27 | """ | |
28 | A fake input stream for pdb's interactive debugger. Whenever a |
|
28 | A fake input stream for pdb's interactive debugger. Whenever a | |
29 | line is read, print it (to simulate the user typing it), and then |
|
29 | line is read, print it (to simulate the user typing it), and then | |
30 | return it. The set of lines to return is specified in the |
|
30 | return it. The set of lines to return is specified in the | |
31 | constructor; they should not have trailing newlines. |
|
31 | constructor; they should not have trailing newlines. | |
32 | """ |
|
32 | """ | |
33 | def __init__(self, lines): |
|
33 | def __init__(self, lines): | |
34 | self.lines = iter(lines) |
|
34 | self.lines = iter(lines) | |
35 |
|
35 | |||
36 | def readline(self): |
|
36 | def readline(self): | |
37 | line = next(self.lines) |
|
37 | line = next(self.lines) | |
38 | print(line) |
|
38 | print(line) | |
39 | return line+'\n' |
|
39 | return line+'\n' | |
40 |
|
40 | |||
41 | class PdbTestInput(object): |
|
41 | class PdbTestInput(object): | |
42 | """Context manager that makes testing Pdb in doctests easier.""" |
|
42 | """Context manager that makes testing Pdb in doctests easier.""" | |
43 |
|
43 | |||
44 | def __init__(self, input): |
|
44 | def __init__(self, input): | |
45 | self.input = input |
|
45 | self.input = input | |
46 |
|
46 | |||
47 | def __enter__(self): |
|
47 | def __enter__(self): | |
48 | self.real_stdin = sys.stdin |
|
48 | self.real_stdin = sys.stdin | |
49 | sys.stdin = _FakeInput(self.input) |
|
49 | sys.stdin = _FakeInput(self.input) | |
50 |
|
50 | |||
51 | def __exit__(self, *exc): |
|
51 | def __exit__(self, *exc): | |
52 | sys.stdin = self.real_stdin |
|
52 | sys.stdin = self.real_stdin | |
53 |
|
53 | |||
54 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- | |
55 | # Tests |
|
55 | # Tests | |
56 | #----------------------------------------------------------------------------- |
|
56 | #----------------------------------------------------------------------------- | |
57 |
|
57 | |||
58 | def test_ipdb_magics(): |
|
58 | def test_ipdb_magics(): | |
59 | '''Test calling some IPython magics from ipdb. |
|
59 | '''Test calling some IPython magics from ipdb. | |
60 |
|
60 | |||
61 | First, set up some test functions and classes which we can inspect. |
|
61 | First, set up some test functions and classes which we can inspect. | |
62 |
|
62 | |||
63 | >>> class ExampleClass(object): |
|
63 | >>> class ExampleClass(object): | |
64 | ... """Docstring for ExampleClass.""" |
|
64 | ... """Docstring for ExampleClass.""" | |
65 | ... def __init__(self): |
|
65 | ... def __init__(self): | |
66 | ... """Docstring for ExampleClass.__init__""" |
|
66 | ... """Docstring for ExampleClass.__init__""" | |
67 | ... pass |
|
67 | ... pass | |
68 | ... def __str__(self): |
|
68 | ... def __str__(self): | |
69 | ... return "ExampleClass()" |
|
69 | ... return "ExampleClass()" | |
70 |
|
70 | |||
71 | >>> def example_function(x, y, z="hello"): |
|
71 | >>> def example_function(x, y, z="hello"): | |
72 | ... """Docstring for example_function.""" |
|
72 | ... """Docstring for example_function.""" | |
73 | ... pass |
|
73 | ... pass | |
74 |
|
74 | |||
75 | >>> old_trace = sys.gettrace() |
|
75 | >>> old_trace = sys.gettrace() | |
76 |
|
76 | |||
77 | Create a function which triggers ipdb. |
|
77 | Create a function which triggers ipdb. | |
78 |
|
78 | |||
79 | >>> def trigger_ipdb(): |
|
79 | >>> def trigger_ipdb(): | |
80 | ... a = ExampleClass() |
|
80 | ... a = ExampleClass() | |
81 | ... debugger.Pdb().set_trace() |
|
81 | ... debugger.Pdb().set_trace() | |
82 |
|
82 | |||
83 | >>> with PdbTestInput([ |
|
83 | >>> with PdbTestInput([ | |
84 | ... 'pdef example_function', |
|
84 | ... 'pdef example_function', | |
85 | ... 'pdoc ExampleClass', |
|
85 | ... 'pdoc ExampleClass', | |
86 | ... 'up', |
|
86 | ... 'up', | |
87 | ... 'down', |
|
87 | ... 'down', | |
88 | ... 'list', |
|
88 | ... 'list', | |
89 | ... 'pinfo a', |
|
89 | ... 'pinfo a', | |
90 | ... 'll', |
|
90 | ... 'll', | |
91 | ... 'continue', |
|
91 | ... 'continue', | |
92 | ... ]): |
|
92 | ... ]): | |
93 | ... trigger_ipdb() |
|
93 | ... trigger_ipdb() | |
94 | --Return-- |
|
94 | --Return-- | |
95 | None |
|
95 | None | |
96 | > <doctest ...>(3)trigger_ipdb() |
|
96 | > <doctest ...>(3)trigger_ipdb() | |
97 | 1 def trigger_ipdb(): |
|
97 | 1 def trigger_ipdb(): | |
98 | 2 a = ExampleClass() |
|
98 | 2 a = ExampleClass() | |
99 | ----> 3 debugger.Pdb().set_trace() |
|
99 | ----> 3 debugger.Pdb().set_trace() | |
100 | <BLANKLINE> |
|
100 | <BLANKLINE> | |
101 | ipdb> pdef example_function |
|
101 | ipdb> pdef example_function | |
102 | example_function(x, y, z='hello') |
|
102 | example_function(x, y, z='hello') | |
103 | ipdb> pdoc ExampleClass |
|
103 | ipdb> pdoc ExampleClass | |
104 | Class docstring: |
|
104 | Class docstring: | |
105 | Docstring for ExampleClass. |
|
105 | Docstring for ExampleClass. | |
106 | Init docstring: |
|
106 | Init docstring: | |
107 | Docstring for ExampleClass.__init__ |
|
107 | Docstring for ExampleClass.__init__ | |
108 | ipdb> up |
|
108 | ipdb> up | |
109 | > <doctest ...>(11)<module>() |
|
109 | > <doctest ...>(11)<module>() | |
110 | 7 'pinfo a', |
|
110 | 7 'pinfo a', | |
111 | 8 'll', |
|
111 | 8 'll', | |
112 | 9 'continue', |
|
112 | 9 'continue', | |
113 | 10 ]): |
|
113 | 10 ]): | |
114 | ---> 11 trigger_ipdb() |
|
114 | ---> 11 trigger_ipdb() | |
115 | <BLANKLINE> |
|
115 | <BLANKLINE> | |
116 | ipdb> down |
|
116 | ipdb> down | |
117 | None |
|
117 | None | |
118 | > <doctest ...>(3)trigger_ipdb() |
|
118 | > <doctest ...>(3)trigger_ipdb() | |
119 | 1 def trigger_ipdb(): |
|
119 | 1 def trigger_ipdb(): | |
120 | 2 a = ExampleClass() |
|
120 | 2 a = ExampleClass() | |
121 | ----> 3 debugger.Pdb().set_trace() |
|
121 | ----> 3 debugger.Pdb().set_trace() | |
122 | <BLANKLINE> |
|
122 | <BLANKLINE> | |
123 | ipdb> list |
|
123 | ipdb> list | |
124 | 1 def trigger_ipdb(): |
|
124 | 1 def trigger_ipdb(): | |
125 | 2 a = ExampleClass() |
|
125 | 2 a = ExampleClass() | |
126 | ----> 3 debugger.Pdb().set_trace() |
|
126 | ----> 3 debugger.Pdb().set_trace() | |
127 | <BLANKLINE> |
|
127 | <BLANKLINE> | |
128 | ipdb> pinfo a |
|
128 | ipdb> pinfo a | |
129 | Type: ExampleClass |
|
129 | Type: ExampleClass | |
130 | String form: ExampleClass() |
|
130 | String form: ExampleClass() | |
131 | Namespace: Local... |
|
131 | Namespace: Local... | |
132 | Docstring: Docstring for ExampleClass. |
|
132 | Docstring: Docstring for ExampleClass. | |
133 | Init docstring: Docstring for ExampleClass.__init__ |
|
133 | Init docstring: Docstring for ExampleClass.__init__ | |
134 | ipdb> ll |
|
134 | ipdb> ll | |
135 | 1 def trigger_ipdb(): |
|
135 | 1 def trigger_ipdb(): | |
136 | 2 a = ExampleClass() |
|
136 | 2 a = ExampleClass() | |
137 | ----> 3 debugger.Pdb().set_trace() |
|
137 | ----> 3 debugger.Pdb().set_trace() | |
138 | <BLANKLINE> |
|
138 | <BLANKLINE> | |
139 | ipdb> continue |
|
139 | ipdb> continue | |
140 |
|
140 | |||
141 | Restore previous trace function, e.g. for coverage.py |
|
141 | Restore previous trace function, e.g. for coverage.py | |
142 |
|
142 | |||
143 | >>> sys.settrace(old_trace) |
|
143 | >>> sys.settrace(old_trace) | |
144 | ''' |
|
144 | ''' | |
145 |
|
145 | |||
146 | def test_ipdb_magics2(): |
|
146 | def test_ipdb_magics2(): | |
147 | '''Test ipdb with a very short function. |
|
147 | '''Test ipdb with a very short function. | |
148 |
|
148 | |||
149 | >>> old_trace = sys.gettrace() |
|
149 | >>> old_trace = sys.gettrace() | |
150 |
|
150 | |||
151 | >>> def bar(): |
|
151 | >>> def bar(): | |
152 | ... pass |
|
152 | ... pass | |
153 |
|
153 | |||
154 | Run ipdb. |
|
154 | Run ipdb. | |
155 |
|
155 | |||
156 | >>> with PdbTestInput([ |
|
156 | >>> with PdbTestInput([ | |
157 | ... 'continue', |
|
157 | ... 'continue', | |
158 | ... ]): |
|
158 | ... ]): | |
159 | ... debugger.Pdb().runcall(bar) |
|
159 | ... debugger.Pdb().runcall(bar) | |
160 | > <doctest ...>(2)bar() |
|
160 | > <doctest ...>(2)bar() | |
161 | 1 def bar(): |
|
161 | 1 def bar(): | |
162 | ----> 2 pass |
|
162 | ----> 2 pass | |
163 | <BLANKLINE> |
|
163 | <BLANKLINE> | |
164 | ipdb> continue |
|
164 | ipdb> continue | |
165 |
|
165 | |||
166 | Restore previous trace function, e.g. for coverage.py |
|
166 | Restore previous trace function, e.g. for coverage.py | |
167 |
|
167 | |||
168 | >>> sys.settrace(old_trace) |
|
168 | >>> sys.settrace(old_trace) | |
169 | ''' |
|
169 | ''' | |
170 |
|
170 | |||
171 | def can_quit(): |
|
171 | def can_quit(): | |
172 | '''Test that quit work in ipydb |
|
172 | '''Test that quit work in ipydb | |
173 |
|
173 | |||
174 | >>> old_trace = sys.gettrace() |
|
174 | >>> old_trace = sys.gettrace() | |
175 |
|
175 | |||
176 | >>> def bar(): |
|
176 | >>> def bar(): | |
177 | ... pass |
|
177 | ... pass | |
178 |
|
178 | |||
179 | >>> with PdbTestInput([ |
|
179 | >>> with PdbTestInput([ | |
180 | ... 'quit', |
|
180 | ... 'quit', | |
181 | ... ]): |
|
181 | ... ]): | |
182 | ... debugger.Pdb().runcall(bar) |
|
182 | ... debugger.Pdb().runcall(bar) | |
183 | > <doctest ...>(2)bar() |
|
183 | > <doctest ...>(2)bar() | |
184 | 1 def bar(): |
|
184 | 1 def bar(): | |
185 | ----> 2 pass |
|
185 | ----> 2 pass | |
186 | <BLANKLINE> |
|
186 | <BLANKLINE> | |
187 | ipdb> quit |
|
187 | ipdb> quit | |
188 |
|
188 | |||
189 | Restore previous trace function, e.g. for coverage.py |
|
189 | Restore previous trace function, e.g. for coverage.py | |
190 |
|
190 | |||
191 | >>> sys.settrace(old_trace) |
|
191 | >>> sys.settrace(old_trace) | |
192 | ''' |
|
192 | ''' | |
193 |
|
193 | |||
194 |
|
194 | |||
195 | def can_exit(): |
|
195 | def can_exit(): | |
196 | '''Test that quit work in ipydb |
|
196 | '''Test that quit work in ipydb | |
197 |
|
197 | |||
198 | >>> old_trace = sys.gettrace() |
|
198 | >>> old_trace = sys.gettrace() | |
199 |
|
199 | |||
200 | >>> def bar(): |
|
200 | >>> def bar(): | |
201 | ... pass |
|
201 | ... pass | |
202 |
|
202 | |||
203 | >>> with PdbTestInput([ |
|
203 | >>> with PdbTestInput([ | |
204 | ... 'exit', |
|
204 | ... 'exit', | |
205 | ... ]): |
|
205 | ... ]): | |
206 | ... debugger.Pdb().runcall(bar) |
|
206 | ... debugger.Pdb().runcall(bar) | |
207 | > <doctest ...>(2)bar() |
|
207 | > <doctest ...>(2)bar() | |
208 | 1 def bar(): |
|
208 | 1 def bar(): | |
209 | ----> 2 pass |
|
209 | ----> 2 pass | |
210 | <BLANKLINE> |
|
210 | <BLANKLINE> | |
211 | ipdb> exit |
|
211 | ipdb> exit | |
212 |
|
212 | |||
213 | Restore previous trace function, e.g. for coverage.py |
|
213 | Restore previous trace function, e.g. for coverage.py | |
214 |
|
214 | |||
215 | >>> sys.settrace(old_trace) |
|
215 | >>> sys.settrace(old_trace) | |
216 | ''' |
|
216 | ''' | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | def test_interruptible_core_debugger(): |
|
219 | def test_interruptible_core_debugger(): | |
220 | """The debugger can be interrupted. |
|
220 | """The debugger can be interrupted. | |
221 |
|
221 | |||
222 | The presumption is there is some mechanism that causes a KeyboardInterrupt |
|
222 | The presumption is there is some mechanism that causes a KeyboardInterrupt | |
223 | (this is implemented in ipykernel). We want to ensure the |
|
223 | (this is implemented in ipykernel). We want to ensure the | |
224 | KeyboardInterrupt cause debugging to cease. |
|
224 | KeyboardInterrupt cause debugging to cease. | |
225 | """ |
|
225 | """ | |
226 | def raising_input(msg="", called=[0]): |
|
226 | def raising_input(msg="", called=[0]): | |
227 | called[0] += 1 |
|
227 | called[0] += 1 | |
228 | assert called[0] == 1, "input() should only be called once!" |
|
228 | assert called[0] == 1, "input() should only be called once!" | |
229 | raise KeyboardInterrupt() |
|
229 | raise KeyboardInterrupt() | |
230 |
|
230 | |||
231 | tracer_orig = sys.gettrace() |
|
231 | tracer_orig = sys.gettrace() | |
232 | try: |
|
232 | try: | |
233 | with patch.object(builtins, "input", raising_input): |
|
233 | with patch.object(builtins, "input", raising_input): | |
234 | debugger.InterruptiblePdb().set_trace() |
|
234 | debugger.InterruptiblePdb().set_trace() | |
235 | # The way this test will fail is by set_trace() never exiting, |
|
235 | # The way this test will fail is by set_trace() never exiting, | |
236 | # resulting in a timeout by the test runner. The alternative |
|
236 | # resulting in a timeout by the test runner. The alternative | |
237 | # implementation would involve a subprocess, but that adds issues |
|
237 | # implementation would involve a subprocess, but that adds issues | |
238 | # with interrupting subprocesses that are rather complex, so it's |
|
238 | # with interrupting subprocesses that are rather complex, so it's | |
239 | # simpler just to do it this way. |
|
239 | # simpler just to do it this way. | |
240 | finally: |
|
240 | finally: | |
241 | # restore the original trace function |
|
241 | # restore the original trace function | |
242 | sys.settrace(tracer_orig) |
|
242 | sys.settrace(tracer_orig) | |
243 |
|
243 | |||
244 |
|
244 | |||
245 | @skip_win32 |
|
245 | @skip_win32 | |
246 | def test_xmode_skip(): |
|
246 | def test_xmode_skip(): | |
247 | """that xmode skip frames |
|
247 | """that xmode skip frames | |
248 |
|
248 | |||
249 | Not as a doctest as pytest does not run doctests. |
|
249 | Not as a doctest as pytest does not run doctests. | |
250 | """ |
|
250 | """ | |
251 | import pexpect |
|
251 | import pexpect | |
252 | env = os.environ.copy() |
|
252 | env = os.environ.copy() | |
253 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
|
253 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" | |
254 |
|
254 | |||
255 | child = pexpect.spawn( |
|
255 | child = pexpect.spawn( | |
256 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
|
256 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env | |
257 | ) |
|
257 | ) | |
258 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
258 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE | |
259 |
|
259 | |||
260 | child.expect("IPython") |
|
260 | child.expect("IPython") | |
261 | child.expect("\n") |
|
261 | child.expect("\n") | |
262 | child.expect_exact("In [1]") |
|
262 | child.expect_exact("In [1]") | |
263 |
|
263 | |||
264 | block = dedent( |
|
264 | block = dedent( | |
265 | """ |
|
265 | """ | |
266 | def f(): |
|
266 | def f(): | |
267 | __tracebackhide__ = True |
|
267 | __tracebackhide__ = True | |
268 | g() |
|
268 | g() | |
269 |
|
269 | |||
270 | def g(): |
|
270 | def g(): | |
271 | raise ValueError |
|
271 | raise ValueError | |
272 |
|
272 | |||
273 | f() |
|
273 | f() | |
274 | """ |
|
274 | """ | |
275 | ) |
|
275 | ) | |
276 |
|
276 | |||
277 | for line in block.splitlines(): |
|
277 | for line in block.splitlines(): | |
278 | child.sendline(line) |
|
278 | child.sendline(line) | |
279 | child.expect_exact(line) |
|
279 | child.expect_exact(line) | |
280 | child.expect_exact("skipping") |
|
280 | child.expect_exact("skipping") | |
281 |
|
281 | |||
282 | block = dedent( |
|
282 | block = dedent( | |
283 | """ |
|
283 | """ | |
284 | def f(): |
|
284 | def f(): | |
285 | __tracebackhide__ = True |
|
285 | __tracebackhide__ = True | |
286 | g() |
|
286 | g() | |
287 |
|
287 | |||
288 | def g(): |
|
288 | def g(): | |
289 | from IPython.core.debugger import set_trace |
|
289 | from IPython.core.debugger import set_trace | |
290 | set_trace() |
|
290 | set_trace() | |
291 |
|
291 | |||
292 | f() |
|
292 | f() | |
293 | """ |
|
293 | """ | |
294 | ) |
|
294 | ) | |
295 |
|
295 | |||
296 | for line in block.splitlines(): |
|
296 | for line in block.splitlines(): | |
297 | child.sendline(line) |
|
297 | child.sendline(line) | |
298 | child.expect_exact(line) |
|
298 | child.expect_exact(line) | |
299 |
|
299 | |||
300 | child.expect("ipdb>") |
|
300 | child.expect("ipdb>") | |
301 | child.sendline("w") |
|
301 | child.sendline("w") | |
302 | child.expect("hidden") |
|
302 | child.expect("hidden") | |
303 | child.expect("ipdb>") |
|
303 | child.expect("ipdb>") | |
304 | child.sendline("skip_hidden false") |
|
304 | child.sendline("skip_hidden false") | |
305 | child.sendline("w") |
|
305 | child.sendline("w") | |
306 | child.expect("__traceba") |
|
306 | child.expect("__traceba") | |
307 | child.expect("ipdb>") |
|
307 | child.expect("ipdb>") | |
308 |
|
308 | |||
309 | child.close() |
|
309 | child.close() | |
310 |
|
310 | |||
311 |
|
311 | |||
312 | skip_decorators_blocks = ( |
|
312 | skip_decorators_blocks = ( | |
313 | """ |
|
313 | """ | |
314 | def helpers_helper(): |
|
314 | def helpers_helper(): | |
315 | pass # should not stop here except breakpoint |
|
315 | pass # should not stop here except breakpoint | |
316 | """, |
|
316 | """, | |
317 | """ |
|
317 | """ | |
318 | def helper_1(): |
|
318 | def helper_1(): | |
319 | helpers_helper() # should not stop here |
|
319 | helpers_helper() # should not stop here | |
320 | """, |
|
320 | """, | |
321 | """ |
|
321 | """ | |
322 | def helper_2(): |
|
322 | def helper_2(): | |
323 | pass # should not stop here |
|
323 | pass # should not stop here | |
324 | """, |
|
324 | """, | |
325 | """ |
|
325 | """ | |
326 | def pdb_skipped_decorator2(function): |
|
326 | def pdb_skipped_decorator2(function): | |
327 | def wrapped_fn(*args, **kwargs): |
|
327 | def wrapped_fn(*args, **kwargs): | |
328 | __debuggerskip__ = True |
|
328 | __debuggerskip__ = True | |
329 | helper_2() |
|
329 | helper_2() | |
330 | __debuggerskip__ = False |
|
330 | __debuggerskip__ = False | |
331 | result = function(*args, **kwargs) |
|
331 | result = function(*args, **kwargs) | |
332 | __debuggerskip__ = True |
|
332 | __debuggerskip__ = True | |
333 | helper_2() |
|
333 | helper_2() | |
334 | return result |
|
334 | return result | |
335 | return wrapped_fn |
|
335 | return wrapped_fn | |
336 | """, |
|
336 | """, | |
337 | """ |
|
337 | """ | |
338 | def pdb_skipped_decorator(function): |
|
338 | def pdb_skipped_decorator(function): | |
339 | def wrapped_fn(*args, **kwargs): |
|
339 | def wrapped_fn(*args, **kwargs): | |
340 | __debuggerskip__ = True |
|
340 | __debuggerskip__ = True | |
341 | helper_1() |
|
341 | helper_1() | |
342 | __debuggerskip__ = False |
|
342 | __debuggerskip__ = False | |
343 | result = function(*args, **kwargs) |
|
343 | result = function(*args, **kwargs) | |
344 | __debuggerskip__ = True |
|
344 | __debuggerskip__ = True | |
345 | helper_2() |
|
345 | helper_2() | |
346 | return result |
|
346 | return result | |
347 | return wrapped_fn |
|
347 | return wrapped_fn | |
348 | """, |
|
348 | """, | |
349 | """ |
|
349 | """ | |
350 | @pdb_skipped_decorator |
|
350 | @pdb_skipped_decorator | |
351 | @pdb_skipped_decorator2 |
|
351 | @pdb_skipped_decorator2 | |
352 | def bar(x, y): |
|
352 | def bar(x, y): | |
353 | return x * y |
|
353 | return x * y | |
354 | """, |
|
354 | """, | |
355 | """import IPython.terminal.debugger as ipdb""", |
|
355 | """import IPython.terminal.debugger as ipdb""", | |
356 | """ |
|
356 | """ | |
357 | def f(): |
|
357 | def f(): | |
358 | ipdb.set_trace() |
|
358 | ipdb.set_trace() | |
359 | bar(3, 4) |
|
359 | bar(3, 4) | |
360 | """, |
|
360 | """, | |
361 | """ |
|
361 | """ | |
362 | f() |
|
362 | f() | |
363 | """, |
|
363 | """, | |
364 | ) |
|
364 | ) | |
365 |
|
365 | |||
366 |
|
366 | |||
367 | def _decorator_skip_setup(): |
|
367 | def _decorator_skip_setup(): | |
368 | import pexpect |
|
368 | import pexpect | |
369 |
|
369 | |||
370 | env = os.environ.copy() |
|
370 | env = os.environ.copy() | |
371 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
|
371 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" | |
372 |
|
372 | |||
373 | child = pexpect.spawn( |
|
373 | child = pexpect.spawn( | |
374 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
|
374 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env | |
375 | ) |
|
375 | ) | |
376 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
376 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE | |
377 |
|
377 | |||
378 | child.expect("IPython") |
|
378 | child.expect("IPython") | |
379 | child.expect("\n") |
|
379 | child.expect("\n") | |
380 |
|
380 | |||
381 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
381 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE | |
|
382 | child.str_last_chars = 500 | |||
382 |
|
383 | |||
383 | dedented_blocks = [dedent(b).strip() for b in skip_decorators_blocks] |
|
384 | dedented_blocks = [dedent(b).strip() for b in skip_decorators_blocks] | |
384 | in_prompt_number = 1 |
|
385 | in_prompt_number = 1 | |
385 | for cblock in dedented_blocks: |
|
386 | for cblock in dedented_blocks: | |
386 | child.expect_exact(f"In [{in_prompt_number}]:") |
|
387 | child.expect_exact(f"In [{in_prompt_number}]:") | |
387 | in_prompt_number += 1 |
|
388 | in_prompt_number += 1 | |
388 | for line in cblock.splitlines(): |
|
389 | for line in cblock.splitlines(): | |
389 | child.sendline(line) |
|
390 | child.sendline(line) | |
390 | child.expect_exact(line) |
|
391 | child.expect_exact(line) | |
391 | child.sendline("") |
|
392 | child.sendline("") | |
392 | return child |
|
393 | return child | |
393 |
|
394 | |||
394 |
|
395 | |||
395 | @skip_win32 |
|
396 | @skip_win32 | |
396 | def test_decorator_skip(): |
|
397 | def test_decorator_skip(): | |
397 | """test that decorator frames can be skipped.""" |
|
398 | """test that decorator frames can be skipped.""" | |
398 |
|
399 | |||
399 | child = _decorator_skip_setup() |
|
400 | child = _decorator_skip_setup() | |
400 |
|
401 | |||
401 | child.expect_exact("3 bar(3, 4)") |
|
402 | child.expect_exact("3 bar(3, 4)") | |
402 | child.expect("ipdb>") |
|
403 | child.expect("ipdb>") | |
403 |
|
404 | |||
404 | child.expect("ipdb>") |
|
405 | child.expect("ipdb>") | |
405 | child.sendline("step") |
|
406 | child.sendline("step") | |
406 | child.expect_exact("step") |
|
407 | child.expect_exact("step") | |
407 |
|
408 | |||
408 | child.expect_exact("1 @pdb_skipped_decorator") |
|
409 | child.expect_exact("1 @pdb_skipped_decorator") | |
409 |
|
410 | |||
410 | child.sendline("s") |
|
411 | child.sendline("s") | |
411 | child.expect_exact("return x * y") |
|
412 | child.expect_exact("return x * y") | |
412 |
|
413 | |||
413 | child.close() |
|
414 | child.close() | |
414 |
|
415 | |||
415 |
|
416 | |||
416 | @pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") |
|
417 | @pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") | |
417 | @skip_win32 |
|
418 | @skip_win32 | |
418 | def test_decorator_skip_disabled(): |
|
419 | def test_decorator_skip_disabled(): | |
419 | """test that decorator frame skipping can be disabled""" |
|
420 | """test that decorator frame skipping can be disabled""" | |
420 |
|
421 | |||
421 | child = _decorator_skip_setup() |
|
422 | child = _decorator_skip_setup() | |
422 |
|
423 | |||
423 | child.expect_exact("3 bar(3, 4)") |
|
424 | child.expect_exact("3 bar(3, 4)") | |
424 |
|
425 | |||
425 | for input_, expected in [ |
|
426 | for input_, expected in [ | |
426 | ("skip_predicates debuggerskip False", ""), |
|
427 | ("skip_predicates debuggerskip False", ""), | |
427 | ("skip_predicates", "debuggerskip : False"), |
|
428 | ("skip_predicates", "debuggerskip : False"), | |
428 | ("step", "---> 2 def wrapped_fn"), |
|
429 | ("step", "---> 2 def wrapped_fn"), | |
429 | ("step", "----> 3 __debuggerskip__"), |
|
430 | ("step", "----> 3 __debuggerskip__"), | |
430 | ("step", "----> 4 helper_1()"), |
|
431 | ("step", "----> 4 helper_1()"), | |
431 | ("step", "---> 1 def helper_1():"), |
|
432 | ("step", "---> 1 def helper_1():"), | |
432 | ("next", "----> 2 helpers_helper()"), |
|
433 | ("next", "----> 2 helpers_helper()"), | |
433 | ("next", "--Return--"), |
|
434 | ("next", "--Return--"), | |
434 | ("next", "----> 5 __debuggerskip__ = False"), |
|
435 | ("next", "----> 5 __debuggerskip__ = False"), | |
435 | ]: |
|
436 | ]: | |
436 | child.expect("ipdb>") |
|
437 | child.expect("ipdb>") | |
437 | child.sendline(input_) |
|
438 | child.sendline(input_) | |
438 | child.expect_exact(input_) |
|
439 | child.expect_exact(input_) | |
439 | child.expect_exact(expected) |
|
440 | child.expect_exact(expected) | |
440 |
|
441 | |||
441 | child.close() |
|
442 | child.close() | |
442 |
|
443 | |||
443 |
|
444 | |||
444 | @pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") |
|
445 | @pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") | |
445 | @skip_win32 |
|
446 | @skip_win32 | |
446 | def test_decorator_skip_with_breakpoint(): |
|
447 | def test_decorator_skip_with_breakpoint(): | |
447 | """test that decorator frame skipping can be disabled""" |
|
448 | """test that decorator frame skipping can be disabled""" | |
448 |
|
449 | |||
449 | import pexpect |
|
450 | import pexpect | |
450 |
|
451 | |||
451 | env = os.environ.copy() |
|
452 | env = os.environ.copy() | |
452 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
|
453 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" | |
453 |
|
454 | |||
454 | child = pexpect.spawn( |
|
455 | child = pexpect.spawn( | |
455 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
|
456 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env | |
456 | ) |
|
457 | ) | |
457 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
458 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE | |
458 |
|
459 | |||
459 | child.expect("IPython") |
|
460 | child.expect("IPython") | |
460 | child.expect("\n") |
|
461 | child.expect("\n") | |
461 |
|
462 | |||
462 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
463 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE | |
463 |
|
464 | |||
464 | ### we need a filename, so we need to exec the full block with a filename |
|
465 | ### we need a filename, so we need to exec the full block with a filename | |
465 | with NamedTemporaryFile(suffix=".py", dir=".", delete=True) as tf: |
|
466 | with NamedTemporaryFile(suffix=".py", dir=".", delete=True) as tf: | |
466 |
|
467 | |||
467 | name = tf.name[:-3].split("/")[-1] |
|
468 | name = tf.name[:-3].split("/")[-1] | |
468 | tf.write("\n".join([dedent(x) for x in skip_decorators_blocks[:-1]]).encode()) |
|
469 | tf.write("\n".join([dedent(x) for x in skip_decorators_blocks[:-1]]).encode()) | |
469 | tf.flush() |
|
470 | tf.flush() | |
470 | codeblock = f"from {name} import f" |
|
471 | codeblock = f"from {name} import f" | |
471 |
|
472 | |||
472 | dedented_blocks = [ |
|
473 | dedented_blocks = [ | |
473 | codeblock, |
|
474 | codeblock, | |
474 | "f()", |
|
475 | "f()", | |
475 | ] |
|
476 | ] | |
476 |
|
477 | |||
477 | in_prompt_number = 1 |
|
478 | in_prompt_number = 1 | |
478 | for cblock in dedented_blocks: |
|
479 | for cblock in dedented_blocks: | |
479 | child.expect_exact(f"In [{in_prompt_number}]:") |
|
480 | child.expect_exact(f"In [{in_prompt_number}]:") | |
480 | in_prompt_number += 1 |
|
481 | in_prompt_number += 1 | |
481 | for line in cblock.splitlines(): |
|
482 | for line in cblock.splitlines(): | |
482 | child.sendline(line) |
|
483 | child.sendline(line) | |
483 | child.expect_exact(line) |
|
484 | child.expect_exact(line) | |
484 | child.sendline("") |
|
485 | child.sendline("") | |
485 |
|
486 | |||
486 | # as the filename does not exists, we'll rely on the filename prompt |
|
487 | # as the filename does not exists, we'll rely on the filename prompt | |
487 | child.expect_exact("47 bar(3, 4)") |
|
488 | child.expect_exact("47 bar(3, 4)") | |
488 |
|
489 | |||
489 | for input_, expected in [ |
|
490 | for input_, expected in [ | |
490 | (f"b {name}.py:3", ""), |
|
491 | (f"b {name}.py:3", ""), | |
491 | ("step", "1---> 3 pass # should not stop here except"), |
|
492 | ("step", "1---> 3 pass # should not stop here except"), | |
492 | ("step", "---> 38 @pdb_skipped_decorator"), |
|
493 | ("step", "---> 38 @pdb_skipped_decorator"), | |
493 | ("continue", ""), |
|
494 | ("continue", ""), | |
494 | ]: |
|
495 | ]: | |
495 | child.expect("ipdb>") |
|
496 | child.expect("ipdb>") | |
496 | child.sendline(input_) |
|
497 | child.sendline(input_) | |
497 | child.expect_exact(input_) |
|
498 | child.expect_exact(input_) | |
498 | child.expect_exact(expected) |
|
499 | child.expect_exact(expected) | |
499 |
|
500 | |||
500 | child.close() |
|
501 | child.close() | |
501 |
|
502 | |||
502 |
|
503 | |||
503 | @skip_win32 |
|
504 | @skip_win32 | |
504 | def test_where_erase_value(): |
|
505 | def test_where_erase_value(): | |
505 | """Test that `where` does not access f_locals and erase values.""" |
|
506 | """Test that `where` does not access f_locals and erase values.""" | |
506 | import pexpect |
|
507 | import pexpect | |
507 |
|
508 | |||
508 | env = os.environ.copy() |
|
509 | env = os.environ.copy() | |
509 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
|
510 | env["IPY_TEST_SIMPLE_PROMPT"] = "1" | |
510 |
|
511 | |||
511 | child = pexpect.spawn( |
|
512 | child = pexpect.spawn( | |
512 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
|
513 | sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env | |
513 | ) |
|
514 | ) | |
514 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
515 | child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE | |
515 |
|
516 | |||
516 | child.expect("IPython") |
|
517 | child.expect("IPython") | |
517 | child.expect("\n") |
|
518 | child.expect("\n") | |
518 | child.expect_exact("In [1]") |
|
519 | child.expect_exact("In [1]") | |
519 |
|
520 | |||
520 | block = dedent( |
|
521 | block = dedent( | |
521 | """ |
|
522 | """ | |
522 | def simple_f(): |
|
523 | def simple_f(): | |
523 | myvar = 1 |
|
524 | myvar = 1 | |
524 | print(myvar) |
|
525 | print(myvar) | |
525 | 1/0 |
|
526 | 1/0 | |
526 | print(myvar) |
|
527 | print(myvar) | |
527 | simple_f() """ |
|
528 | simple_f() """ | |
528 | ) |
|
529 | ) | |
529 |
|
530 | |||
530 | for line in block.splitlines(): |
|
531 | for line in block.splitlines(): | |
531 | child.sendline(line) |
|
532 | child.sendline(line) | |
532 | child.expect_exact(line) |
|
533 | child.expect_exact(line) | |
533 | child.expect_exact("ZeroDivisionError") |
|
534 | child.expect_exact("ZeroDivisionError") | |
534 | child.expect_exact("In [2]:") |
|
535 | child.expect_exact("In [2]:") | |
535 |
|
536 | |||
536 | child.sendline("%debug") |
|
537 | child.sendline("%debug") | |
537 |
|
538 | |||
538 | ## |
|
539 | ## | |
539 | child.expect("ipdb>") |
|
540 | child.expect("ipdb>") | |
540 |
|
541 | |||
541 | child.sendline("myvar") |
|
542 | child.sendline("myvar") | |
542 | child.expect("1") |
|
543 | child.expect("1") | |
543 |
|
544 | |||
544 | ## |
|
545 | ## | |
545 | child.expect("ipdb>") |
|
546 | child.expect("ipdb>") | |
546 |
|
547 | |||
547 | child.sendline("myvar = 2") |
|
548 | child.sendline("myvar = 2") | |
548 |
|
549 | |||
549 | ## |
|
550 | ## | |
550 | child.expect_exact("ipdb>") |
|
551 | child.expect_exact("ipdb>") | |
551 |
|
552 | |||
552 | child.sendline("myvar") |
|
553 | child.sendline("myvar") | |
553 |
|
554 | |||
554 | child.expect_exact("2") |
|
555 | child.expect_exact("2") | |
555 |
|
556 | |||
556 | ## |
|
557 | ## | |
557 | child.expect("ipdb>") |
|
558 | child.expect("ipdb>") | |
558 | child.sendline("where") |
|
559 | child.sendline("where") | |
559 |
|
560 | |||
560 | ## |
|
561 | ## | |
561 | child.expect("ipdb>") |
|
562 | child.expect("ipdb>") | |
562 | child.sendline("myvar") |
|
563 | child.sendline("myvar") | |
563 |
|
564 | |||
564 | child.expect_exact("2") |
|
565 | child.expect_exact("2") | |
565 | child.expect("ipdb>") |
|
566 | child.expect("ipdb>") | |
566 |
|
567 | |||
567 | child.close() |
|
568 | child.close() |
General Comments 0
You need to be logged in to leave comments.
Login now