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