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 | |
|
168 | if SV_VERSION < (0, 6): | |
|
169 | ||
|
170 | def doctest_tb_sysexit_verbose_stack_data_05(): | |
|
171 | """ | |
|
172 | In [18]: %run simpleerr.py exit | |
|
173 | An exception has occurred, use %tb to see the full traceback. | |
|
174 | SystemExit: (1, 'Mode = exit') | |
|
175 | ||
|
176 | In [19]: %run simpleerr.py exit 2 | |
|
177 | An exception has occurred, use %tb to see the full traceback. | |
|
178 | SystemExit: (2, 'Mode = exit') | |
|
179 | ||
|
180 | In [23]: %xmode verbose | |
|
181 | Exception reporting mode: Verbose | |
|
182 | ||
|
183 | In [24]: %tb | |
|
184 | --------------------------------------------------------------------------- | |
|
185 | SystemExit Traceback (most recent call last) | |
|
186 | <BLANKLINE> | |
|
187 | ... | |
|
188 | 30 except IndexError: | |
|
189 | 31 mode = 'div' | |
|
190 | ---> 33 bar(mode) | |
|
191 |
|
|
|
192 | <BLANKLINE> | |
|
193 | ... in bar(mode='exit') | |
|
194 |
... |
|
|
195 |
... |
|
|
196 | ---> ... sysexit(stat, mode) | |
|
197 | mode = 'exit' | |
|
198 |
|
|
|
199 |
|
|
|
200 | ... raise ValueError('Unknown mode') | |
|
201 | <BLANKLINE> | |
|
202 | ... in sysexit(stat=2, mode='exit') | |
|
203 |
|
|
|
204 | ---> 11 raise SystemExit(stat, f"Mode = {mode}") | |
|
205 | stat = 2 | |
|
206 | <BLANKLINE> | |
|
207 | SystemExit: (2, 'Mode = exit') | |
|
208 | """ | |
|
209 | ||
|
210 | else: | |
|
211 | # currently the only difference is | |
|
212 | # + mode = 'exit' | |
|
213 | ||
|
214 | def doctest_tb_sysexit_verbose_stack_data_06(): | |
|
215 | """ | |
|
216 | In [18]: %run simpleerr.py exit | |
|
217 | An exception has occurred, use %tb to see the full traceback. | |
|
218 | SystemExit: (1, 'Mode = exit') | |
|
219 | ||
|
220 | In [19]: %run simpleerr.py exit 2 | |
|
221 | An exception has occurred, use %tb to see the full traceback. | |
|
222 | SystemExit: (2, 'Mode = exit') | |
|
223 | ||
|
224 | In [23]: %xmode verbose | |
|
225 | Exception reporting mode: Verbose | |
|
226 | ||
|
227 | In [24]: %tb | |
|
228 | --------------------------------------------------------------------------- | |
|
229 | SystemExit Traceback (most recent call last) | |
|
230 | <BLANKLINE> | |
|
231 | ... | |
|
232 | 30 except IndexError: | |
|
233 | 31 mode = 'div' | |
|
234 | ---> 33 bar(mode) | |
|
235 |
|
|
|
236 | <BLANKLINE> | |
|
237 | ... in bar(mode='exit') | |
|
238 |
|
|
|
239 |
... |
|
|
240 | ---> ... sysexit(stat, mode) | |
|
241 | mode = 'exit' | |
|
242 |
|
|
|
243 |
|
|
|
244 | ... raise ValueError('Unknown mode') | |
|
245 | <BLANKLINE> | |
|
246 | ... in sysexit(stat=2, mode='exit') | |
|
247 |
|
|
|
248 | ---> 11 raise SystemExit(stat, f"Mode = {mode}") | |
|
249 | stat = 2 | |
|
250 |
|
|
|
251 | <BLANKLINE> | |
|
252 | SystemExit: (2, 'Mode = exit') | |
|
253 | """ | |
|
169 | if sys.version_info >= (3, 9): | |
|
170 | if SV_VERSION < (0, 6): | |
|
171 | ||
|
172 | def doctest_tb_sysexit_verbose_stack_data_05(): | |
|
173 | """ | |
|
174 | In [18]: %run simpleerr.py exit | |
|
175 | An exception has occurred, use %tb to see the full traceback. | |
|
176 | SystemExit: (1, 'Mode = exit') | |
|
177 | ||
|
178 | In [19]: %run simpleerr.py exit 2 | |
|
179 | An exception has occurred, use %tb to see the full traceback. | |
|
180 | SystemExit: (2, 'Mode = exit') | |
|
181 | ||
|
182 | In [23]: %xmode verbose | |
|
183 | Exception reporting mode: Verbose | |
|
184 | ||
|
185 | In [24]: %tb | |
|
186 | --------------------------------------------------------------------------- | |
|
187 | SystemExit Traceback (most recent call last) | |
|
188 | <BLANKLINE> | |
|
189 | ... | |
|
190 | 30 except IndexError: | |
|
191 | 31 mode = 'div' | |
|
192 | ---> 33 bar(mode) | |
|
193 | mode = 'exit' | |
|
194 | <BLANKLINE> | |
|
195 | ... in bar(mode='exit') | |
|
196 | ... except: | |
|
197 | ... stat = 1 | |
|
198 | ---> ... sysexit(stat, mode) | |
|
199 | mode = 'exit' | |
|
200 | stat = 2 | |
|
201 | ... else: | |
|
202 | ... raise ValueError('Unknown mode') | |
|
203 | <BLANKLINE> | |
|
204 | ... in sysexit(stat=2, mode='exit') | |
|
205 | 10 def sysexit(stat, mode): | |
|
206 | ---> 11 raise SystemExit(stat, f"Mode = {mode}") | |
|
207 | stat = 2 | |
|
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 | """ | |
|
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