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