1
"""Doctest-related utilities for IPython.
3
For most common uses, all you should need to run is::
5
from IPython.dtutils import idoctest
7
See the idoctest docstring below for usage details.
14
ip = IPython.ipapi.get()
16
def rundoctest(text,ns=None,eraise=False):
17
"""Run a the input source as a doctest, in the caller's namespace.
25
Namespace where the code should be executed. If not given, the
26
caller's locals and globals are used.
28
If true, immediately raise any exceptions instead of reporting them at
29
the end. This allows you to then do interactive debugging via
30
IPython's facilities (use %debug after the fact, or with %pdb for
31
automatic activation).
34
name = 'interactive doctest'
35
filename = '<IPython console>'
38
runner = doctest.DebugRunner()
40
runner = doctest.DocTestRunner()
42
parser = doctest.DocTestParser()
45
ns = f.f_globals.copy()
48
test = parser.get_doctest(text,ns,name,filename,0)
50
runner.summarize(True)
53
def idoctest(ns=None,eraise=False):
54
"""Interactively prompt for input and run it as a doctest.
56
To finish entering input, enter two blank lines or Ctrl-D (EOF). If you
57
use Ctrl-C, the example is aborted and all input discarded.
61
Namespace where the code should be executed. If not given, the IPython
62
interactive namespace is used.
64
If true, immediately raise any exceptions instead of reporting them at
65
the end. This allows you to then do interactive debugging via
66
IPython's facilities (use %debug after the fact, or with %pdb for
67
automatic activation).
69
String to explicitly indicate the end of input.
74
empty_lines = 0 # count consecutive empty lines
84
if not line or line.isspace():
95
except KeyboardInterrupt:
96
print "KeyboardInterrupt - Discarding input."
102
# Extra blank line at the end to ensure that the final docstring has a
105
rundoctest('\n'.join(inlines),ns,eraise)
108
# For debugging of this module itself.
109
if __name__ == "__main__":
111
>>> for i in range(10):
120
>>> for i in range(10):