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