~ellisonbg/ipython/trunk-dev

« back to all changes in this revision

Viewing changes to IPython/core/tests/test_iplib.py

  • Committer: Brian Granger
  • Date: 2010-01-31 23:57:46 UTC
  • mfrom: (1109.1.113 ipython)
  • Revision ID: ellisonbg@gmail.com-20100131235746-rj81qa8klooepq2m
Merging from upstream trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Tests for the key iplib module, where the main ipython class is defined.
2
2
"""
3
 
 
 
3
#-----------------------------------------------------------------------------
 
4
# Module imports
 
5
#-----------------------------------------------------------------------------
 
6
 
 
7
# stdlib
 
8
import os
 
9
import shutil
 
10
import tempfile
 
11
 
 
12
# third party
4
13
import nose.tools as nt
5
14
 
6
 
 
 
15
# our own packages
 
16
from IPython.testing import decorators as dec
 
17
from IPython.testing.globalipapp import get_ipython
 
18
 
 
19
#-----------------------------------------------------------------------------
 
20
# Globals
 
21
#-----------------------------------------------------------------------------
 
22
 
 
23
# Get the public instance of IPython
 
24
ip = get_ipython()
 
25
 
 
26
#-----------------------------------------------------------------------------
 
27
# Test functions
 
28
#-----------------------------------------------------------------------------
 
29
 
 
30
@dec.parametric
7
31
def test_reset():
8
32
    """reset must clear most namespaces."""
9
 
    ip = _ip.IP
10
 
    ip.reset()  # first, it should run without error
11
 
    # Then, check that most namespaces end up empty
 
33
    # The number of variables in the private user_ns_hidden is not zero, but it
 
34
    # should be constant regardless of what we do
 
35
    nvars_config_ns = len(ip.user_ns_hidden)
 
36
 
 
37
    # Check that reset runs without error
 
38
    ip.reset()
 
39
 
 
40
    # Once we've reset it (to clear of any junk that might have been there from
 
41
    # other tests, we can count how many variables are in the user's namespace
 
42
    nvars_user_ns = len(ip.user_ns)
 
43
 
 
44
    # Now add a few variables to user_ns, and check that reset clears them
 
45
    ip.user_ns['x'] = 1
 
46
    ip.user_ns['y'] = 1
 
47
    ip.reset()
 
48
    
 
49
    # Finally, check that all namespaces have only as many variables as we
 
50
    # expect to find in them:
12
51
    for ns in ip.ns_refs_table:
13
52
        if ns is ip.user_ns:
14
 
            # The user namespace is reset with some data, so we can't check for
15
 
            # it being empty
16
 
            continue
17
 
        nt.assert_equals(len(ns),0)
 
53
            nvars_expected = nvars_user_ns
 
54
        elif ns is ip.user_ns_hidden:
 
55
            nvars_expected = nvars_config_ns
 
56
        else:
 
57
            nvars_expected = 0
 
58
            
 
59
        yield nt.assert_equals(len(ns), nvars_expected)
 
60
 
 
61
 
 
62
# Tests for reporting of exceptions in various modes, handling of SystemExit,
 
63
# and %tb functionality.  This is really a mix of testing ultraTB and iplib.
 
64
 
 
65
def doctest_tb_plain():
 
66
    """
 
67
In [18]: xmode plain
 
68
Exception reporting mode: Plain
 
69
 
 
70
In [19]: run simpleerr.py
 
71
Traceback (most recent call last):
 
72
  ...line 32, in <module>
 
73
    bar(mode)
 
74
  ...line 16, in bar
 
75
    div0()
 
76
  ...line 8, in div0
 
77
    x/y
 
78
ZeroDivisionError: integer division or modulo by zero
 
79
    """
 
80
 
 
81
 
 
82
def doctest_tb_context():
 
83
    """
 
84
In [3]: xmode context
 
85
Exception reporting mode: Context
 
86
 
 
87
In [4]: run simpleerr.py
 
88
---------------------------------------------------------------------------
 
89
ZeroDivisionError                         Traceback (most recent call last)
 
90
<BLANKLINE>
 
91
... in <module>()
 
92
     30         mode = 'div'
 
93
     31 
 
94
---> 32     bar(mode)
 
95
     33 
 
96
     34 
 
97
<BLANKLINE>
 
98
... in bar(mode)
 
99
     14     "bar"
 
100
     15     if mode=='div':
 
101
---> 16         div0()
 
102
     17     elif mode=='exit':
 
103
     18         try:
 
104
<BLANKLINE>
 
105
... in div0()
 
106
      6     x = 1
 
107
      7     y = 0
 
108
----> 8     x/y
 
109
      9 
 
110
     10 def sysexit(stat, mode):
 
111
<BLANKLINE>
 
112
ZeroDivisionError: integer division or modulo by zero
 
113
"""
 
114
 
 
115
 
 
116
def doctest_tb_verbose():
 
117
    """
 
118
In [5]: xmode verbose
 
119
Exception reporting mode: Verbose
 
120
 
 
121
In [6]: run simpleerr.py
 
122
---------------------------------------------------------------------------
 
123
ZeroDivisionError                         Traceback (most recent call last)
 
124
<BLANKLINE>
 
125
... in <module>()
 
126
     30         mode = 'div'
 
127
     31 
 
128
---> 32     bar(mode)
 
129
        global bar = <function bar at ...>
 
130
        global mode = 'div'
 
131
     33 
 
132
     34 
 
133
<BLANKLINE>
 
134
... in bar(mode='div')
 
135
     14     "bar"
 
136
     15     if mode=='div':
 
137
---> 16         div0()
 
138
        global div0 = <function div0 at ...>
 
139
     17     elif mode=='exit':
 
140
     18         try:
 
141
<BLANKLINE>
 
142
... in div0()
 
143
      6     x = 1
 
144
      7     y = 0
 
145
----> 8     x/y
 
146
        x = 1
 
147
        y = 0
 
148
      9 
 
149
     10 def sysexit(stat, mode):
 
150
<BLANKLINE>
 
151
ZeroDivisionError: integer division or modulo by zero
 
152
      """
 
153
 
 
154
 
 
155
def doctest_tb_sysexit():
 
156
    """
 
157
In [17]: %xmode plain
 
158
Exception reporting mode: Plain
 
159
 
 
160
In [18]: %run simpleerr.py exit
 
161
An exception has occurred, use %tb to see the full traceback.
 
162
SystemExit: (1, 'Mode = exit')
 
163
 
 
164
In [19]: %run simpleerr.py exit 2
 
165
An exception has occurred, use %tb to see the full traceback.
 
166
SystemExit: (2, 'Mode = exit')
 
167
 
 
168
In [20]: %tb
 
169
Traceback (most recent call last):
 
170
  File ... in <module>
 
171
    bar(mode)
 
172
  File ... line 22, in bar
 
173
    sysexit(stat, mode)
 
174
  File ... line 11, in sysexit
 
175
    raise SystemExit(stat, 'Mode = %s' % mode)
 
176
SystemExit: (2, 'Mode = exit')
 
177
 
 
178
In [21]: %xmode context
 
179
Exception reporting mode: Context
 
180
 
 
181
In [22]: %tb
 
182
---------------------------------------------------------------------------
 
183
SystemExit                                Traceback (most recent call last)
 
184
<BLANKLINE>
 
185
...<module>()
 
186
     30         mode = 'div'
 
187
     31 
 
188
---> 32     bar(mode)
 
189
     33 
 
190
     34 
 
191
<BLANKLINE>
 
192
...bar(mode)
 
193
     20         except:
 
194
     21             stat = 1
 
195
---> 22         sysexit(stat, mode)
 
196
     23     else:
 
197
     24         raise ValueError('Unknown mode')
 
198
<BLANKLINE>
 
199
...sysexit(stat, mode)
 
200
      9 
 
201
     10 def sysexit(stat, mode):
 
202
---> 11     raise SystemExit(stat, 'Mode = %s' % mode)
 
203
     12 
 
204
     13 def bar(mode):
 
205
<BLANKLINE>
 
206
SystemExit: (2, 'Mode = exit')
 
207
 
 
208
In [23]: %xmode verbose
 
209
Exception reporting mode: Verbose
 
210
 
 
211
In [24]: %tb
 
212
---------------------------------------------------------------------------
 
213
SystemExit                                Traceback (most recent call last)
 
214
<BLANKLINE>
 
215
... in <module>()
 
216
     30         mode = 'div'
 
217
     31 
 
218
---> 32     bar(mode)
 
219
        global bar = <function bar at ...>
 
220
        global mode = 'exit'
 
221
     33 
 
222
     34 
 
223
<BLANKLINE>
 
224
... in bar(mode='exit')
 
225
     20         except:
 
226
     21             stat = 1
 
227
---> 22         sysexit(stat, mode)
 
228
        global sysexit = <function sysexit at ...>
 
229
        stat = 2
 
230
        mode = 'exit'
 
231
     23     else:
 
232
     24         raise ValueError('Unknown mode')
 
233
<BLANKLINE>
 
234
... in sysexit(stat=2, mode='exit')
 
235
      9 
 
236
     10 def sysexit(stat, mode):
 
237
---> 11     raise SystemExit(stat, 'Mode = %s' % mode)
 
238
        global SystemExit = undefined
 
239
        stat = 2
 
240
        mode = 'exit'
 
241
     12 
 
242
     13 def bar(mode):
 
243
<BLANKLINE>
 
244
SystemExit: (2, 'Mode = exit')
 
245
    """