##// END OF EJS Templates
exclude 3.8...
Matthias Bussonnier -
Show More
@@ -1,289 +1,291 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 import stack_data
4 import stack_data
5 import sys
5
6
6 SV_VERSION = tuple([int(x) for x in stack_data.__version__.split(".")[0:2]])
7 SV_VERSION = tuple([int(x) for x in stack_data.__version__.split(".")[0:2]])
7
8
8
9
9 def test_reset():
10 def test_reset():
10 """reset must clear most namespaces."""
11 """reset must clear most namespaces."""
11
12
12 # Check that reset runs without error
13 # Check that reset runs without error
13 ip.reset()
14 ip.reset()
14
15
15 # Once we've reset it (to clear of any junk that might have been there from
16 # Once we've reset it (to clear of any junk that might have been there from
16 # other tests, we can count how many variables are in the user's namespace
17 # other tests, we can count how many variables are in the user's namespace
17 nvars_user_ns = len(ip.user_ns)
18 nvars_user_ns = len(ip.user_ns)
18 nvars_hidden = len(ip.user_ns_hidden)
19 nvars_hidden = len(ip.user_ns_hidden)
19
20
20 # Now add a few variables to user_ns, and check that reset clears them
21 # Now add a few variables to user_ns, and check that reset clears them
21 ip.user_ns['x'] = 1
22 ip.user_ns['x'] = 1
22 ip.user_ns['y'] = 1
23 ip.user_ns['y'] = 1
23 ip.reset()
24 ip.reset()
24
25
25 # Finally, check that all namespaces have only as many variables as we
26 # Finally, check that all namespaces have only as many variables as we
26 # expect to find in them:
27 # expect to find in them:
27 assert len(ip.user_ns) == nvars_user_ns
28 assert len(ip.user_ns) == nvars_user_ns
28 assert len(ip.user_ns_hidden) == nvars_hidden
29 assert len(ip.user_ns_hidden) == nvars_hidden
29
30
30
31
31 # Tests for reporting of exceptions in various modes, handling of SystemExit,
32 # Tests for reporting of exceptions in various modes, handling of SystemExit,
32 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
33 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
33
34
34 def doctest_tb_plain():
35 def doctest_tb_plain():
35 """
36 """
36 In [18]: xmode plain
37 In [18]: xmode plain
37 Exception reporting mode: Plain
38 Exception reporting mode: Plain
38
39
39 In [19]: run simpleerr.py
40 In [19]: run simpleerr.py
40 Traceback (most recent call last):
41 Traceback (most recent call last):
41 File ...:...
42 File ...:...
42 bar(mode)
43 bar(mode)
43 File ...:... in bar
44 File ...:... in bar
44 div0()
45 div0()
45 File ...:... in div0
46 File ...:... in div0
46 x/y
47 x/y
47 ZeroDivisionError: ...
48 ZeroDivisionError: ...
48 """
49 """
49
50
50
51
51 def doctest_tb_context():
52 def doctest_tb_context():
52 """
53 """
53 In [3]: xmode context
54 In [3]: xmode context
54 Exception reporting mode: Context
55 Exception reporting mode: Context
55
56
56 In [4]: run simpleerr.py
57 In [4]: run simpleerr.py
57 ---------------------------------------------------------------------------
58 ---------------------------------------------------------------------------
58 ZeroDivisionError Traceback (most recent call last)
59 ZeroDivisionError Traceback (most recent call last)
59 <BLANKLINE>
60 <BLANKLINE>
60 ...
61 ...
61 30 except IndexError:
62 30 except IndexError:
62 31 mode = 'div'
63 31 mode = 'div'
63 ---> 33 bar(mode)
64 ---> 33 bar(mode)
64 <BLANKLINE>
65 <BLANKLINE>
65 ... in bar(mode)
66 ... in bar(mode)
66 15 "bar"
67 15 "bar"
67 16 if mode=='div':
68 16 if mode=='div':
68 ---> 17 div0()
69 ---> 17 div0()
69 18 elif mode=='exit':
70 18 elif mode=='exit':
70 19 try:
71 19 try:
71 <BLANKLINE>
72 <BLANKLINE>
72 ... in div0()
73 ... in div0()
73 6 x = 1
74 6 x = 1
74 7 y = 0
75 7 y = 0
75 ----> 8 x/y
76 ----> 8 x/y
76 <BLANKLINE>
77 <BLANKLINE>
77 ZeroDivisionError: ..."""
78 ZeroDivisionError: ..."""
78
79
79
80
80 def doctest_tb_verbose():
81 def doctest_tb_verbose():
81 """
82 """
82 In [5]: xmode verbose
83 In [5]: xmode verbose
83 Exception reporting mode: Verbose
84 Exception reporting mode: Verbose
84
85
85 In [6]: run simpleerr.py
86 In [6]: run simpleerr.py
86 ---------------------------------------------------------------------------
87 ---------------------------------------------------------------------------
87 ZeroDivisionError Traceback (most recent call last)
88 ZeroDivisionError Traceback (most recent call last)
88 <BLANKLINE>
89 <BLANKLINE>
89 ...
90 ...
90 30 except IndexError:
91 30 except IndexError:
91 31 mode = 'div'
92 31 mode = 'div'
92 ---> 33 bar(mode)
93 ---> 33 bar(mode)
93 mode = 'div'
94 mode = 'div'
94 <BLANKLINE>
95 <BLANKLINE>
95 ... in bar(mode='div')
96 ... in bar(mode='div')
96 15 "bar"
97 15 "bar"
97 16 if mode=='div':
98 16 if mode=='div':
98 ---> 17 div0()
99 ---> 17 div0()
99 18 elif mode=='exit':
100 18 elif mode=='exit':
100 19 try:
101 19 try:
101 <BLANKLINE>
102 <BLANKLINE>
102 ... in div0()
103 ... in div0()
103 6 x = 1
104 6 x = 1
104 7 y = 0
105 7 y = 0
105 ----> 8 x/y
106 ----> 8 x/y
106 x = 1
107 x = 1
107 y = 0
108 y = 0
108 <BLANKLINE>
109 <BLANKLINE>
109 ZeroDivisionError: ...
110 ZeroDivisionError: ...
110 """
111 """
111
112
112
113
113 def doctest_tb_sysexit():
114 def doctest_tb_sysexit():
114 """
115 """
115 In [17]: %xmode plain
116 In [17]: %xmode plain
116 Exception reporting mode: Plain
117 Exception reporting mode: Plain
117
118
118 In [18]: %run simpleerr.py exit
119 In [18]: %run simpleerr.py exit
119 An exception has occurred, use %tb to see the full traceback.
120 An exception has occurred, use %tb to see the full traceback.
120 SystemExit: (1, 'Mode = exit')
121 SystemExit: (1, 'Mode = exit')
121
122
122 In [19]: %run simpleerr.py exit 2
123 In [19]: %run simpleerr.py exit 2
123 An exception has occurred, use %tb to see the full traceback.
124 An exception has occurred, use %tb to see the full traceback.
124 SystemExit: (2, 'Mode = exit')
125 SystemExit: (2, 'Mode = exit')
125
126
126 In [20]: %tb
127 In [20]: %tb
127 Traceback (most recent call last):
128 Traceback (most recent call last):
128 File ...:... in execfile
129 File ...:... in execfile
129 exec(compiler(f.read(), fname, "exec"), glob, loc)
130 exec(compiler(f.read(), fname, "exec"), glob, loc)
130 File ...:...
131 File ...:...
131 bar(mode)
132 bar(mode)
132 File ...:... in bar
133 File ...:... in bar
133 sysexit(stat, mode)
134 sysexit(stat, mode)
134 File ...:... in sysexit
135 File ...:... in sysexit
135 raise SystemExit(stat, f"Mode = {mode}")
136 raise SystemExit(stat, f"Mode = {mode}")
136 SystemExit: (2, 'Mode = exit')
137 SystemExit: (2, 'Mode = exit')
137
138
138 In [21]: %xmode context
139 In [21]: %xmode context
139 Exception reporting mode: Context
140 Exception reporting mode: Context
140
141
141 In [22]: %tb
142 In [22]: %tb
142 ---------------------------------------------------------------------------
143 ---------------------------------------------------------------------------
143 SystemExit Traceback (most recent call last)
144 SystemExit Traceback (most recent call last)
144 File ..., in execfile(fname, glob, loc, compiler)
145 File ..., in execfile(fname, glob, loc, compiler)
145 ... with open(fname, "rb") as f:
146 ... with open(fname, "rb") as f:
146 ... compiler = compiler or compile
147 ... compiler = compiler or compile
147 ---> ... exec(compiler(f.read(), fname, "exec"), glob, loc)
148 ---> ... exec(compiler(f.read(), fname, "exec"), glob, loc)
148 ...
149 ...
149 30 except IndexError:
150 30 except IndexError:
150 31 mode = 'div'
151 31 mode = 'div'
151 ---> 33 bar(mode)
152 ---> 33 bar(mode)
152 <BLANKLINE>
153 <BLANKLINE>
153 ...bar(mode)
154 ...bar(mode)
154 21 except:
155 21 except:
155 22 stat = 1
156 22 stat = 1
156 ---> 23 sysexit(stat, mode)
157 ---> 23 sysexit(stat, mode)
157 24 else:
158 24 else:
158 25 raise ValueError('Unknown mode')
159 25 raise ValueError('Unknown mode')
159 <BLANKLINE>
160 <BLANKLINE>
160 ...sysexit(stat, mode)
161 ...sysexit(stat, mode)
161 10 def sysexit(stat, mode):
162 10 def sysexit(stat, mode):
162 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
163 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
163 <BLANKLINE>
164 <BLANKLINE>
164 SystemExit: (2, 'Mode = exit')
165 SystemExit: (2, 'Mode = exit')
165 """
166 """
166
167
167
168
168 if SV_VERSION < (0, 6):
169 if sys.version_info >= (3, 9):
169
170 if SV_VERSION < (0, 6):
170 def doctest_tb_sysexit_verbose_stack_data_05():
171
171 """
172 def doctest_tb_sysexit_verbose_stack_data_05():
172 In [18]: %run simpleerr.py exit
173 """
173 An exception has occurred, use %tb to see the full traceback.
174 In [18]: %run simpleerr.py exit
174 SystemExit: (1, 'Mode = exit')
175 An exception has occurred, use %tb to see the full traceback.
175
176 SystemExit: (1, 'Mode = exit')
176 In [19]: %run simpleerr.py exit 2
177
177 An exception has occurred, use %tb to see the full traceback.
178 In [19]: %run simpleerr.py exit 2
178 SystemExit: (2, 'Mode = exit')
179 An exception has occurred, use %tb to see the full traceback.
179
180 SystemExit: (2, 'Mode = exit')
180 In [23]: %xmode verbose
181
181 Exception reporting mode: Verbose
182 In [23]: %xmode verbose
182
183 Exception reporting mode: Verbose
183 In [24]: %tb
184
184 ---------------------------------------------------------------------------
185 In [24]: %tb
185 SystemExit Traceback (most recent call last)
186 ---------------------------------------------------------------------------
186 <BLANKLINE>
187 SystemExit Traceback (most recent call last)
187 ...
188 <BLANKLINE>
188 30 except IndexError:
189 ...
189 31 mode = 'div'
190 30 except IndexError:
190 ---> 33 bar(mode)
191 31 mode = 'div'
191 mode = 'exit'
192 ---> 33 bar(mode)
192 <BLANKLINE>
193 mode = 'exit'
193 ... in bar(mode='exit')
194 <BLANKLINE>
194 ... except:
195 ... in bar(mode='exit')
195 ... stat = 1
196 ... except:
196 ---> ... sysexit(stat, mode)
197 ... stat = 1
197 mode = 'exit'
198 ---> ... sysexit(stat, mode)
198 stat = 2
199 mode = 'exit'
199 ... else:
200 stat = 2
200 ... raise ValueError('Unknown mode')
201 ... else:
201 <BLANKLINE>
202 ... raise ValueError('Unknown mode')
202 ... in sysexit(stat=2, mode='exit')
203 <BLANKLINE>
203 10 def sysexit(stat, mode):
204 ... in sysexit(stat=2, mode='exit')
204 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
205 10 def sysexit(stat, mode):
205 stat = 2
206 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
206 <BLANKLINE>
207 stat = 2
207 SystemExit: (2, 'Mode = exit')
208 <BLANKLINE>
208 """
209 SystemExit: (2, 'Mode = exit')
209
210 """
210 else:
211
211 # currently the only difference is
212 else:
212 # + mode = 'exit'
213 # currently the only difference is
213
214 # + mode = 'exit'
214 def doctest_tb_sysexit_verbose_stack_data_06():
215
215 """
216 def doctest_tb_sysexit_verbose_stack_data_06():
216 In [18]: %run simpleerr.py exit
217 """
217 An exception has occurred, use %tb to see the full traceback.
218 In [18]: %run simpleerr.py exit
218 SystemExit: (1, 'Mode = exit')
219 An exception has occurred, use %tb to see the full traceback.
219
220 SystemExit: (1, 'Mode = exit')
220 In [19]: %run simpleerr.py exit 2
221
221 An exception has occurred, use %tb to see the full traceback.
222 In [19]: %run simpleerr.py exit 2
222 SystemExit: (2, 'Mode = exit')
223 An exception has occurred, use %tb to see the full traceback.
223
224 SystemExit: (2, 'Mode = exit')
224 In [23]: %xmode verbose
225
225 Exception reporting mode: Verbose
226 In [23]: %xmode verbose
226
227 Exception reporting mode: Verbose
227 In [24]: %tb
228
228 ---------------------------------------------------------------------------
229 In [24]: %tb
229 SystemExit Traceback (most recent call last)
230 ---------------------------------------------------------------------------
230 <BLANKLINE>
231 SystemExit Traceback (most recent call last)
231 ...
232 <BLANKLINE>
232 30 except IndexError:
233 ...
233 31 mode = 'div'
234 30 except IndexError:
234 ---> 33 bar(mode)
235 31 mode = 'div'
235 mode = 'exit'
236 ---> 33 bar(mode)
236 <BLANKLINE>
237 mode = 'exit'
237 ... in bar(mode='exit')
238 <BLANKLINE>
238 ... except:
239 ... in bar(mode='exit')
239 ... stat = 1
240 ... except:
240 ---> ... sysexit(stat, mode)
241 ... stat = 1
241 mode = 'exit'
242 ---> ... sysexit(stat, mode)
242 stat = 2
243 mode = 'exit'
243 ... else:
244 stat = 2
244 ... raise ValueError('Unknown mode')
245 ... else:
245 <BLANKLINE>
246 ... raise ValueError('Unknown mode')
246 ... in sysexit(stat=2, mode='exit')
247 <BLANKLINE>
247 10 def sysexit(stat, mode):
248 ... in sysexit(stat=2, mode='exit')
248 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
249 10 def sysexit(stat, mode):
249 stat = 2
250 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
250 mode = 'exit'
251 stat = 2
251 <BLANKLINE>
252 mode = 'exit'
252 SystemExit: (2, 'Mode = exit')
253 <BLANKLINE>
253 """
254 SystemExit: (2, 'Mode = exit')
255 """
254
256
255 def test_run_cell():
257 def test_run_cell():
256 import textwrap
258 import textwrap
257
259
258 ip.run_cell("a = 10\na+=1")
260 ip.run_cell("a = 10\na+=1")
259 ip.run_cell("assert a == 11\nassert 1")
261 ip.run_cell("assert a == 11\nassert 1")
260
262
261 assert ip.user_ns["a"] == 11
263 assert ip.user_ns["a"] == 11
262 complex = textwrap.dedent(
264 complex = textwrap.dedent(
263 """
265 """
264 if 1:
266 if 1:
265 print "hello"
267 print "hello"
266 if 1:
268 if 1:
267 print "world"
269 print "world"
268
270
269 if 2:
271 if 2:
270 print "foo"
272 print "foo"
271
273
272 if 3:
274 if 3:
273 print "bar"
275 print "bar"
274
276
275 if 4:
277 if 4:
276 print "bar"
278 print "bar"
277
279
278 """
280 """
279 )
281 )
280 # Simply verifies that this kind of input is run
282 # Simply verifies that this kind of input is run
281 ip.run_cell(complex)
283 ip.run_cell(complex)
282
284
283
285
284 def test_db():
286 def test_db():
285 """Test the internal database used for variable persistence."""
287 """Test the internal database used for variable persistence."""
286 ip.db["__unittest_"] = 12
288 ip.db["__unittest_"] = 12
287 assert ip.db["__unittest_"] == 12
289 assert ip.db["__unittest_"] == 12
288 del ip.db["__unittest_"]
290 del ip.db["__unittest_"]
289 assert "__unittest_" not in ip.db
291 assert "__unittest_" not in ip.db
General Comments 0
You need to be logged in to leave comments. Login now