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