~ubuntu-branches/ubuntu/raring/ipython/raring

« back to all changes in this revision

Viewing changes to IPython/dtutils.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2011-11-22 23:40:57 UTC
  • mfrom: (6.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20111122234057-ta86ocdahnhwmnd8
Tags: 0.11-2
* upload to unstable
* add patch fix-version-checks-for-pyzmq-2.1.10.patch
* fix debianize-error-messages.patch to reraise unknown exceptions
* suggest python-zmq for ipython package
* use dh_sphinxdoc
  - bump sphinx dependency to >= 1.0.7+dfsg-1~, replace libjs-jquery
    dependency with ${sphinxdoc:Depends} and drop ipython-doc.links
* remove empty directory from ipython
* link duplicate images in ipython-doc
* remove obsolete Conflicts and Replaces

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Doctest-related utilities for IPython.
2
 
 
3
 
For most common uses, all you should need to run is::
4
 
 
5
 
  from IPython.dtutils import idoctest
6
 
 
7
 
See the idoctest docstring below for usage details.
8
 
"""
9
 
 
10
 
import doctest
11
 
import sys
12
 
 
13
 
import IPython.ipapi
14
 
ip = IPython.ipapi.get()
15
 
 
16
 
def rundoctest(text,ns=None,eraise=False):
17
 
    """Run a the input source as a doctest, in the caller's namespace.
18
 
 
19
 
    :Parameters:
20
 
      text : str
21
 
        Source to execute.
22
 
 
23
 
    :Keywords:
24
 
      ns : dict (None)
25
 
        Namespace where the code should be executed.  If not given, the
26
 
        caller's locals and globals are used.
27
 
      eraise : bool (False)
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).
32
 
    """
33
 
 
34
 
    name = 'interactive doctest'
35
 
    filename = '<IPython console>'
36
 
 
37
 
    if eraise:
38
 
        runner = doctest.DebugRunner()
39
 
    else:
40
 
        runner = doctest.DocTestRunner()
41
 
        
42
 
    parser = doctest.DocTestParser()
43
 
    if ns is None:
44
 
        f = sys._getframe(1)
45
 
        ns = f.f_globals.copy()
46
 
        ns.update(f.f_locals)
47
 
        
48
 
    test = parser.get_doctest(text,ns,name,filename,0)
49
 
    runner.run(test)
50
 
    runner.summarize(True)
51
 
 
52
 
       
53
 
def idoctest(ns=None,eraise=False):
54
 
    """Interactively prompt for input and run it as a doctest.
55
 
 
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.
58
 
 
59
 
    :Keywords:
60
 
      ns : dict (None)
61
 
        Namespace where the code should be executed.  If not given, the IPython
62
 
        interactive namespace is used.
63
 
      eraise : bool (False)
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).
68
 
      end_mark : str ('--')
69
 
        String to explicitly indicate the end of input.
70
 
 
71
 
    """
72
 
    
73
 
    inlines = []
74
 
    empty_lines = 0  # count consecutive empty lines
75
 
    run_test = True
76
 
 
77
 
    if ns is None:
78
 
        ns = ip.user_ns
79
 
 
80
 
    ip.IP.savehist()
81
 
    try:
82
 
        while True:
83
 
            line = raw_input()
84
 
            if not line or line.isspace():
85
 
                empty_lines += 1
86
 
            else:
87
 
                empty_lines = 0
88
 
 
89
 
            if empty_lines>=2:
90
 
                break
91
 
 
92
 
            inlines.append(line)
93
 
    except EOFError:
94
 
        pass
95
 
    except KeyboardInterrupt:
96
 
        print "KeyboardInterrupt - Discarding input."
97
 
        run_test = False
98
 
    
99
 
    ip.IP.reloadhist()
100
 
 
101
 
    if run_test:
102
 
        # Extra blank line at the end to ensure that the final docstring has a
103
 
        # closing newline
104
 
        inlines.append('')
105
 
        rundoctest('\n'.join(inlines),ns,eraise)
106
 
 
107
 
 
108
 
# For debugging of this module itself.
109
 
if __name__ == "__main__":
110
 
    t = """
111
 
    >>> for i in range(10):
112
 
    ...     print i,
113
 
    ...
114
 
    0 1 2 3 4 5 6 7 8 9
115
 
    """
116
 
 
117
 
    t2 = """
118
 
        A simple example::
119
 
 
120
 
          >>> for i in range(10):
121
 
          ...     print i,
122
 
          ...
123
 
          0 1 2 3 4 5 6 7 8 9
124
 
 
125
 
        Some more details::
126
 
 
127
 
          >>> print "hello"
128
 
          hello
129
 
    """
130
 
 
131
 
    t3 = """
132
 
        A failing example::
133
 
 
134
 
          >>> x=1
135
 
          >>> x+1
136
 
          3
137
 
    """