1
1
"""Tests for the key iplib module, where the main ipython class is defined.
3
#-----------------------------------------------------------------------------
5
#-----------------------------------------------------------------------------
4
13
import nose.tools as nt
16
from IPython.testing import decorators as dec
17
from IPython.testing.globalipapp import get_ipython
19
#-----------------------------------------------------------------------------
21
#-----------------------------------------------------------------------------
23
# Get the public instance of IPython
26
#-----------------------------------------------------------------------------
28
#-----------------------------------------------------------------------------
8
32
"""reset must clear most namespaces."""
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)
37
# Check that reset runs without error
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)
44
# Now add a few variables to user_ns, and check that reset clears them
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
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
59
yield nt.assert_equals(len(ns), nvars_expected)
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.
65
def doctest_tb_plain():
68
Exception reporting mode: Plain
70
In [19]: run simpleerr.py
71
Traceback (most recent call last):
72
...line 32, in <module>
78
ZeroDivisionError: integer division or modulo by zero
82
def doctest_tb_context():
85
Exception reporting mode: Context
87
In [4]: run simpleerr.py
88
---------------------------------------------------------------------------
89
ZeroDivisionError Traceback (most recent call last)
102
17 elif mode=='exit':
110
10 def sysexit(stat, mode):
112
ZeroDivisionError: integer division or modulo by zero
116
def doctest_tb_verbose():
118
In [5]: xmode verbose
119
Exception reporting mode: Verbose
121
In [6]: run simpleerr.py
122
---------------------------------------------------------------------------
123
ZeroDivisionError Traceback (most recent call last)
129
global bar = <function bar at ...>
134
... in bar(mode='div')
138
global div0 = <function div0 at ...>
139
17 elif mode=='exit':
149
10 def sysexit(stat, mode):
151
ZeroDivisionError: integer division or modulo by zero
155
def doctest_tb_sysexit():
157
In [17]: %xmode plain
158
Exception reporting mode: Plain
160
In [18]: %run simpleerr.py exit
161
An exception has occurred, use %tb to see the full traceback.
162
SystemExit: (1, 'Mode = exit')
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')
169
Traceback (most recent call last):
172
File ... line 22, in bar
174
File ... line 11, in sysexit
175
raise SystemExit(stat, 'Mode = %s' % mode)
176
SystemExit: (2, 'Mode = exit')
178
In [21]: %xmode context
179
Exception reporting mode: Context
182
---------------------------------------------------------------------------
183
SystemExit Traceback (most recent call last)
195
---> 22 sysexit(stat, mode)
197
24 raise ValueError('Unknown mode')
199
...sysexit(stat, mode)
201
10 def sysexit(stat, mode):
202
---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
206
SystemExit: (2, 'Mode = exit')
208
In [23]: %xmode verbose
209
Exception reporting mode: Verbose
212
---------------------------------------------------------------------------
213
SystemExit Traceback (most recent call last)
219
global bar = <function bar at ...>
224
... in bar(mode='exit')
227
---> 22 sysexit(stat, mode)
228
global sysexit = <function sysexit at ...>
232
24 raise ValueError('Unknown mode')
234
... in sysexit(stat=2, mode='exit')
236
10 def sysexit(stat, mode):
237
---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
238
global SystemExit = undefined
244
SystemExit: (2, 'Mode = exit')