test_debugger.py
226 lines
| 5.7 KiB
| text/x-python
|
PythonLexer
|
r7087 | """Tests for debugging machinery. | ||
""" | ||||
|
r13348 | from __future__ import print_function | ||
|
r7087 | |||
|
r22742 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
|
r7087 | |||
|
r7929 | import sys | ||
|
r22742 | import warnings | ||
|
r7929 | |||
|
r7087 | import nose.tools as nt | ||
from IPython.core import debugger | ||||
#----------------------------------------------------------------------------- | ||||
|
r7929 | # Helper classes, from CPython's Pdb test suite | ||
#----------------------------------------------------------------------------- | ||||
class _FakeInput(object): | ||||
""" | ||||
A fake input stream for pdb's interactive debugger. Whenever a | ||||
line is read, print it (to simulate the user typing it), and then | ||||
return it. The set of lines to return is specified in the | ||||
constructor; they should not have trailing newlines. | ||||
""" | ||||
def __init__(self, lines): | ||||
self.lines = iter(lines) | ||||
def readline(self): | ||||
line = next(self.lines) | ||||
|
r13348 | print(line) | ||
|
r7929 | return line+'\n' | ||
class PdbTestInput(object): | ||||
"""Context manager that makes testing Pdb in doctests easier.""" | ||||
def __init__(self, input): | ||||
self.input = input | ||||
def __enter__(self): | ||||
self.real_stdin = sys.stdin | ||||
sys.stdin = _FakeInput(self.input) | ||||
def __exit__(self, *exc): | ||||
sys.stdin = self.real_stdin | ||||
#----------------------------------------------------------------------------- | ||||
|
r7087 | # Tests | ||
#----------------------------------------------------------------------------- | ||||
def test_longer_repr(): | ||||
|
r13375 | try: | ||
from reprlib import repr as trepr # Py 3 | ||||
except ImportError: | ||||
from repr import repr as trepr # Py 2 | ||||
|
r7087 | |||
a = '1234567890'* 7 | ||||
ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'" | ||||
a_trunc = "'123456789012...8901234567890'" | ||||
|
r7875 | nt.assert_equal(trepr(a), a_trunc) | ||
|
r7087 | # The creation of our tracer modifies the repr module's repr function | ||
# in-place, since that global is used directly by the stdlib's pdb module. | ||||
|
r22742 | with warnings.catch_warnings(): | ||
warnings.simplefilter('ignore', DeprecationWarning) | ||||
debugger.Tracer() | ||||
|
r7875 | nt.assert_equal(trepr(a), ar) | ||
|
r7929 | |||
def test_ipdb_magics(): | ||||
'''Test calling some IPython magics from ipdb. | ||||
First, set up some test functions and classes which we can inspect. | ||||
>>> class ExampleClass(object): | ||||
... """Docstring for ExampleClass.""" | ||||
... def __init__(self): | ||||
... """Docstring for ExampleClass.__init__""" | ||||
... pass | ||||
... def __str__(self): | ||||
... return "ExampleClass()" | ||||
>>> def example_function(x, y, z="hello"): | ||||
... """Docstring for example_function.""" | ||||
... pass | ||||
|
r12286 | >>> old_trace = sys.gettrace() | ||
|
r7929 | Create a function which triggers ipdb. | ||
>>> def trigger_ipdb(): | ||||
... a = ExampleClass() | ||||
... debugger.Pdb().set_trace() | ||||
>>> with PdbTestInput([ | ||||
... 'pdef example_function', | ||||
... 'pdoc ExampleClass', | ||||
|
r21851 | ... 'up', | ||
... 'down', | ||||
... 'list', | ||||
|
r7929 | ... 'pinfo a', | ||
|
r21851 | ... 'll', | ||
|
r7929 | ... 'continue', | ||
... ]): | ||||
... trigger_ipdb() | ||||
--Return-- | ||||
None | ||||
> <doctest ...>(3)trigger_ipdb() | ||||
1 def trigger_ipdb(): | ||||
2 a = ExampleClass() | ||||
----> 3 debugger.Pdb().set_trace() | ||||
<BLANKLINE> | ||||
ipdb> pdef example_function | ||||
example_function(x, y, z='hello') | ||||
ipdb> pdoc ExampleClass | ||||
|
r15712 | Class docstring: | ||
|
r7929 | Docstring for ExampleClass. | ||
|
r15712 | Init docstring: | ||
|
r7929 | Docstring for ExampleClass.__init__ | ||
|
r21851 | ipdb> up | ||
> <doctest ...>(11)<module>() | ||||
|
r21945 | 7 'pinfo a', | ||
8 'll', | ||||
|
r21851 | 9 'continue', | ||
10 ]): | ||||
---> 11 trigger_ipdb() | ||||
<BLANKLINE> | ||||
ipdb> down | ||||
None | ||||
> <doctest ...>(3)trigger_ipdb() | ||||
1 def trigger_ipdb(): | ||||
2 a = ExampleClass() | ||||
----> 3 debugger.Pdb().set_trace() | ||||
<BLANKLINE> | ||||
ipdb> list | ||||
1 def trigger_ipdb(): | ||||
2 a = ExampleClass() | ||||
----> 3 debugger.Pdb().set_trace() | ||||
<BLANKLINE> | ||||
|
r7929 | ipdb> pinfo a | ||
|
r15711 | Type: ExampleClass | ||
|
r15712 | String form: ExampleClass() | ||
|
r15711 | Namespace: Local... | ||
Docstring: Docstring for ExampleClass. | ||||
|
r15712 | Init docstring: Docstring for ExampleClass.__init__ | ||
|
r21851 | ipdb> ll | ||
1 def trigger_ipdb(): | ||||
2 a = ExampleClass() | ||||
----> 3 debugger.Pdb().set_trace() | ||||
<BLANKLINE> | ||||
|
r7929 | ipdb> continue | ||
|
r12286 | |||
Restore previous trace function, e.g. for coverage.py | ||||
>>> sys.settrace(old_trace) | ||||
|
r7929 | ''' | ||
|
r8929 | |||
|
r11124 | def test_ipdb_magics2(): | ||
|
r8929 | '''Test ipdb with a very short function. | ||
|
r12286 | |||
>>> old_trace = sys.gettrace() | ||||
|
r8929 | |||
>>> def bar(): | ||||
... pass | ||||
Run ipdb. | ||||
>>> with PdbTestInput([ | ||||
... 'continue', | ||||
... ]): | ||||
... debugger.Pdb().runcall(bar) | ||||
> <doctest ...>(2)bar() | ||||
1 def bar(): | ||||
----> 2 pass | ||||
<BLANKLINE> | ||||
ipdb> continue | ||||
|
r12286 | |||
Restore previous trace function, e.g. for coverage.py | ||||
>>> sys.settrace(old_trace) | ||||
|
r8929 | ''' | ||
|
r22569 | |||
def can_quit(): | ||||
'''Test that quit work in ipydb | ||||
>>> old_trace = sys.gettrace() | ||||
>>> def bar(): | ||||
... pass | ||||
>>> with PdbTestInput([ | ||||
... 'quit', | ||||
... ]): | ||||
... debugger.Pdb().runcall(bar) | ||||
> <doctest ...>(2)bar() | ||||
1 def bar(): | ||||
----> 2 pass | ||||
<BLANKLINE> | ||||
ipdb> quit | ||||
Restore previous trace function, e.g. for coverage.py | ||||
>>> sys.settrace(old_trace) | ||||
''' | ||||
def can_exit(): | ||||
'''Test that quit work in ipydb | ||||
>>> old_trace = sys.gettrace() | ||||
>>> def bar(): | ||||
... pass | ||||
>>> with PdbTestInput([ | ||||
... 'exit', | ||||
... ]): | ||||
... debugger.Pdb().runcall(bar) | ||||
> <doctest ...>(2)bar() | ||||
1 def bar(): | ||||
----> 2 pass | ||||
<BLANKLINE> | ||||
ipdb> exit | ||||
Restore previous trace function, e.g. for coverage.py | ||||
>>> sys.settrace(old_trace) | ||||
''' | ||||