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