##// END OF EJS Templates
Fix doctest_tb_sysexit
Alex Hall -
Show More
@@ -1,237 +1,231 b''
1 """Tests for the key interactiveshell module, where the main ipython class is defined.
1 """Tests for the key interactiveshell module, where the main ipython class is defined.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # third party
7 # third party
8 import nose.tools as nt
8 import nose.tools as nt
9
9
10 # our own packages
10 # our own packages
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Test functions
13 # Test functions
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 def test_reset():
16 def test_reset():
17 """reset must clear most namespaces."""
17 """reset must clear most namespaces."""
18
18
19 # Check that reset runs without error
19 # Check that reset runs without error
20 ip.reset()
20 ip.reset()
21
21
22 # Once we've reset it (to clear of any junk that might have been there from
22 # Once we've reset it (to clear of any junk that might have been there from
23 # other tests, we can count how many variables are in the user's namespace
23 # other tests, we can count how many variables are in the user's namespace
24 nvars_user_ns = len(ip.user_ns)
24 nvars_user_ns = len(ip.user_ns)
25 nvars_hidden = len(ip.user_ns_hidden)
25 nvars_hidden = len(ip.user_ns_hidden)
26
26
27 # Now add a few variables to user_ns, and check that reset clears them
27 # Now add a few variables to user_ns, and check that reset clears them
28 ip.user_ns['x'] = 1
28 ip.user_ns['x'] = 1
29 ip.user_ns['y'] = 1
29 ip.user_ns['y'] = 1
30 ip.reset()
30 ip.reset()
31
31
32 # Finally, check that all namespaces have only as many variables as we
32 # Finally, check that all namespaces have only as many variables as we
33 # expect to find in them:
33 # expect to find in them:
34 nt.assert_equal(len(ip.user_ns), nvars_user_ns)
34 nt.assert_equal(len(ip.user_ns), nvars_user_ns)
35 nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden)
35 nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden)
36
36
37
37
38 # Tests for reporting of exceptions in various modes, handling of SystemExit,
38 # Tests for reporting of exceptions in various modes, handling of SystemExit,
39 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
39 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
40
40
41 def doctest_tb_plain():
41 def doctest_tb_plain():
42 """
42 """
43 In [18]: xmode plain
43 In [18]: xmode plain
44 Exception reporting mode: Plain
44 Exception reporting mode: Plain
45
45
46 In [19]: run simpleerr.py
46 In [19]: run simpleerr.py
47 Traceback (most recent call last):
47 Traceback (most recent call last):
48 ...line 32, in <module>
48 ...line 32, in <module>
49 bar(mode)
49 bar(mode)
50 ...line 16, in bar
50 ...line 16, in bar
51 div0()
51 div0()
52 ...line 8, in div0
52 ...line 8, in div0
53 x/y
53 x/y
54 ZeroDivisionError: ...
54 ZeroDivisionError: ...
55 """
55 """
56
56
57
57
58 def doctest_tb_context():
58 def doctest_tb_context():
59 """
59 """
60 In [3]: xmode context
60 In [3]: xmode context
61 Exception reporting mode: Context
61 Exception reporting mode: Context
62
62
63 In [4]: run simpleerr.py
63 In [4]: run simpleerr.py
64 ---------------------------------------------------------------------------
64 ---------------------------------------------------------------------------
65 ZeroDivisionError Traceback (most recent call last)
65 ZeroDivisionError Traceback (most recent call last)
66 <BLANKLINE>
66 <BLANKLINE>
67 ... in <module>
67 ... in <module>
68 29 except IndexError:
68 29 except IndexError:
69 30 mode = 'div'
69 30 mode = 'div'
70 ---> 32 bar(mode)
70 ---> 32 bar(mode)
71 <BLANKLINE>
71 <BLANKLINE>
72 ... in bar(mode)
72 ... in bar(mode)
73 14 "bar"
73 14 "bar"
74 15 if mode=='div':
74 15 if mode=='div':
75 ---> 16 div0()
75 ---> 16 div0()
76 17 elif mode=='exit':
76 17 elif mode=='exit':
77 18 try:
77 18 try:
78 <BLANKLINE>
78 <BLANKLINE>
79 ... in div0()
79 ... in div0()
80 6 x = 1
80 6 x = 1
81 7 y = 0
81 7 y = 0
82 ----> 8 x/y
82 ----> 8 x/y
83 <BLANKLINE>
83 <BLANKLINE>
84 ZeroDivisionError: ...
84 ZeroDivisionError: ...
85 """
85 """
86
86
87
87
88 def doctest_tb_verbose():
88 def doctest_tb_verbose():
89 """
89 """
90 In [5]: xmode verbose
90 In [5]: xmode verbose
91 Exception reporting mode: Verbose
91 Exception reporting mode: Verbose
92
92
93 In [6]: run simpleerr.py
93 In [6]: run simpleerr.py
94 ---------------------------------------------------------------------------
94 ---------------------------------------------------------------------------
95 ZeroDivisionError Traceback (most recent call last)
95 ZeroDivisionError Traceback (most recent call last)
96 <BLANKLINE>
96 <BLANKLINE>
97 ... in <module>
97 ... in <module>
98 29 except IndexError:
98 29 except IndexError:
99 30 mode = 'div'
99 30 mode = 'div'
100 ---> 32 bar(mode)
100 ---> 32 bar(mode)
101 mode = 'div'
101 mode = 'div'
102 <BLANKLINE>
102 <BLANKLINE>
103 ... in bar(mode='div')
103 ... in bar(mode='div')
104 14 "bar"
104 14 "bar"
105 15 if mode=='div':
105 15 if mode=='div':
106 ---> 16 div0()
106 ---> 16 div0()
107 17 elif mode=='exit':
107 17 elif mode=='exit':
108 18 try:
108 18 try:
109 <BLANKLINE>
109 <BLANKLINE>
110 ... in div0()
110 ... in div0()
111 6 x = 1
111 6 x = 1
112 7 y = 0
112 7 y = 0
113 ----> 8 x/y
113 ----> 8 x/y
114 x = 1
114 x = 1
115 y = 0
115 y = 0
116 <BLANKLINE>
116 <BLANKLINE>
117 ZeroDivisionError: ...
117 ZeroDivisionError: ...
118 """
118 """
119
119
120 def doctest_tb_sysexit():
120 def doctest_tb_sysexit():
121 """
121 """
122 In [17]: %xmode plain
122 In [17]: %xmode plain
123 Exception reporting mode: Plain
123 Exception reporting mode: Plain
124
124
125 In [18]: %run simpleerr.py exit
125 In [18]: %run simpleerr.py exit
126 An exception has occurred, use %tb to see the full traceback.
126 An exception has occurred, use %tb to see the full traceback.
127 SystemExit: (1, 'Mode = exit')
127 SystemExit: (1, 'Mode = exit')
128
128
129 In [19]: %run simpleerr.py exit 2
129 In [19]: %run simpleerr.py exit 2
130 An exception has occurred, use %tb to see the full traceback.
130 An exception has occurred, use %tb to see the full traceback.
131 SystemExit: (2, 'Mode = exit')
131 SystemExit: (2, 'Mode = exit')
132
132
133 In [20]: %tb
133 In [20]: %tb
134 Traceback (most recent call last):
134 Traceback (most recent call last):
135 File ... in <module>
135 File ... in <module>
136 bar(mode)
136 bar(mode)
137 File ... line 22, in bar
137 File ... line 22, in bar
138 sysexit(stat, mode)
138 sysexit(stat, mode)
139 File ... line 11, in sysexit
139 File ... line 11, in sysexit
140 raise SystemExit(stat, 'Mode = %s' % mode)
140 raise SystemExit(stat, 'Mode = %s' % mode)
141 SystemExit: (2, 'Mode = exit')
141 SystemExit: (2, 'Mode = exit')
142
142
143 In [21]: %xmode context
143 In [21]: %xmode context
144 Exception reporting mode: Context
144 Exception reporting mode: Context
145
145
146 In [22]: %tb
146 In [22]: %tb
147 ---------------------------------------------------------------------------
147 ---------------------------------------------------------------------------
148 SystemExit Traceback (most recent call last)
148 SystemExit Traceback (most recent call last)
149 <BLANKLINE>
149 <BLANKLINE>
150 ...<module>
150 ...<module>
151 29 except IndexError:
151 29 except IndexError:
152 30 mode = 'div'
152 30 mode = 'div'
153 ---> 32 bar(mode)
153 ---> 32 bar(mode)
154 <BLANKLINE>
154 <BLANKLINE>
155 ...bar(mode)
155 ...bar(mode)
156 20 except:
156 20 except:
157 21 stat = 1
157 21 stat = 1
158 ---> 22 sysexit(stat, mode)
158 ---> 22 sysexit(stat, mode)
159 23 else:
159 23 else:
160 24 raise ValueError('Unknown mode')
160 24 raise ValueError('Unknown mode')
161 <BLANKLINE>
161 <BLANKLINE>
162 ...sysexit(stat, mode)
162 ...sysexit(stat, mode)
163 10 def sysexit(stat, mode):
163 10 def sysexit(stat, mode):
164 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
164 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
165 <BLANKLINE>
165 <BLANKLINE>
166 SystemExit: (2, 'Mode = exit')
166 SystemExit: (2, 'Mode = exit')
167
167
168 In [23]: %xmode verbose
168 In [23]: %xmode verbose
169 Exception reporting mode: Verbose
169 Exception reporting mode: Verbose
170
170
171 In [24]: %tb
171 In [24]: %tb
172 ---------------------------------------------------------------------------
172 ---------------------------------------------------------------------------
173 SystemExit Traceback (most recent call last)
173 SystemExit Traceback (most recent call last)
174 <BLANKLINE>
174 <BLANKLINE>
175 ... in <module>
175 ... in <module>
176 29 except IndexError:
176 30 mode = 'div'
177 30 mode = 'div'
177 31
178 ---> 32 bar(mode)
178 ---> 32 bar(mode)
179 global bar = <function bar at ...>
179 mode = 'exit'
180 global mode = 'exit'
181 <BLANKLINE>
180 <BLANKLINE>
182 ... in bar(mode='exit')
181 ... in bar(mode='exit')
183 20 except:
182 20 except:
184 21 stat = 1
183 21 stat = 1
185 ---> 22 sysexit(stat, mode)
184 ---> 22 sysexit(stat, mode)
186 global sysexit = <function sysexit at ...>
187 stat = 2
188 mode = 'exit'
185 mode = 'exit'
186 stat = 2
189 23 else:
187 23 else:
190 24 raise ValueError('Unknown mode')
188 24 raise ValueError('Unknown mode')
191 <BLANKLINE>
189 <BLANKLINE>
192 ... in sysexit(stat=2, mode='exit')
190 ... in sysexit(stat=2, mode='exit')
193 9
194 10 def sysexit(stat, mode):
191 10 def sysexit(stat, mode):
195 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
192 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
196 global SystemExit = undefined
197 stat = 2
193 stat = 2
198 mode = 'exit'
194 mode = 'exit'
199 12
200 13 def bar(mode):
201 <BLANKLINE>
195 <BLANKLINE>
202 SystemExit: (2, 'Mode = exit')
196 SystemExit: (2, 'Mode = exit')
203 """
197 """
204
198
205
199
206 def test_run_cell():
200 def test_run_cell():
207 import textwrap
201 import textwrap
208 ip.run_cell('a = 10\na+=1')
202 ip.run_cell('a = 10\na+=1')
209 ip.run_cell('assert a == 11\nassert 1')
203 ip.run_cell('assert a == 11\nassert 1')
210
204
211 nt.assert_equal(ip.user_ns['a'], 11)
205 nt.assert_equal(ip.user_ns['a'], 11)
212 complex = textwrap.dedent("""
206 complex = textwrap.dedent("""
213 if 1:
207 if 1:
214 print "hello"
208 print "hello"
215 if 1:
209 if 1:
216 print "world"
210 print "world"
217
211
218 if 2:
212 if 2:
219 print "foo"
213 print "foo"
220
214
221 if 3:
215 if 3:
222 print "bar"
216 print "bar"
223
217
224 if 4:
218 if 4:
225 print "bar"
219 print "bar"
226
220
227 """)
221 """)
228 # Simply verifies that this kind of input is run
222 # Simply verifies that this kind of input is run
229 ip.run_cell(complex)
223 ip.run_cell(complex)
230
224
231
225
232 def test_db():
226 def test_db():
233 """Test the internal database used for variable persistence."""
227 """Test the internal database used for variable persistence."""
234 ip.db['__unittest_'] = 12
228 ip.db['__unittest_'] = 12
235 nt.assert_equal(ip.db['__unittest_'], 12)
229 nt.assert_equal(ip.db['__unittest_'], 12)
236 del ip.db['__unittest_']
230 del ip.db['__unittest_']
237 assert '__unittest_' not in ip.db
231 assert '__unittest_' not in ip.db
General Comments 0
You need to be logged in to leave comments. Login now