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