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