##// END OF EJS Templates
Revive doctest_tb_sysexit...
Nikita Kniazev -
Show More
@@ -1,32 +1,32 b''
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, 'Mode = %s' % mode)
11 raise SystemExit(stat, f'Mode = {mode}')
12
12
13 def bar(mode):
13 def bar(mode):
14 "bar"
14 "bar"
15 if mode=='div':
15 if mode=='div':
16 div0()
16 div0()
17 elif mode=='exit':
17 elif mode=='exit':
18 try:
18 try:
19 stat = int(sys.argv[2])
19 stat = int(sys.argv[2])
20 except:
20 except:
21 stat = 1
21 stat = 1
22 sysexit(stat, mode)
22 sysexit(stat, mode)
23 else:
23 else:
24 raise ValueError('Unknown mode')
24 raise ValueError('Unknown mode')
25
25
26 if __name__ == '__main__':
26 if __name__ == '__main__':
27 try:
27 try:
28 mode = sys.argv[1]
28 mode = sys.argv[1]
29 except IndexError:
29 except IndexError:
30 mode = 'div'
30 mode = 'div'
31
31
32 bar(mode)
32 bar(mode)
@@ -1,241 +1,233 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 #-----------------------------------------------------------------------------
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 32, in <module>
48 ...line 32, in <module>
49 bar(mode)
49 bar(mode)
50 ...line 16, in bar
50 ...line 16, in bar
51 div0()
51 div0()
52 ...line 8, in div0
52 ...line 8, 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 29 except IndexError:
68 29 except IndexError:
69 30 mode = 'div'
69 30 mode = 'div'
70 ---> 32 bar(mode)
70 ---> 32 bar(mode)
71 <BLANKLINE>
71 <BLANKLINE>
72 ... in bar(mode)
72 ... in bar(mode)
73 14 "bar"
73 14 "bar"
74 15 if mode=='div':
74 15 if mode=='div':
75 ---> 16 div0()
75 ---> 16 div0()
76 17 elif mode=='exit':
76 17 elif mode=='exit':
77 18 try:
77 18 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 """
85 """
86
86
87
87
88 def doctest_tb_verbose():
88 def doctest_tb_verbose():
89 """
89 """
90 In [5]: xmode verbose
90 In [5]: xmode verbose
91 Exception reporting mode: Verbose
91 Exception reporting mode: Verbose
92
92
93 In [6]: run simpleerr.py
93 In [6]: run simpleerr.py
94 ---------------------------------------------------------------------------
94 ---------------------------------------------------------------------------
95 ZeroDivisionError Traceback (most recent call last)
95 ZeroDivisionError Traceback (most recent call last)
96 <BLANKLINE>
96 <BLANKLINE>
97 ... in <module>
97 ... in <module>
98 29 except IndexError:
98 29 except IndexError:
99 30 mode = 'div'
99 30 mode = 'div'
100 ---> 32 bar(mode)
100 ---> 32 bar(mode)
101 mode = 'div'
101 mode = 'div'
102 <BLANKLINE>
102 <BLANKLINE>
103 ... in bar(mode='div')
103 ... in bar(mode='div')
104 14 "bar"
104 14 "bar"
105 15 if mode=='div':
105 15 if mode=='div':
106 ---> 16 div0()
106 ---> 16 div0()
107 17 elif mode=='exit':
107 17 elif mode=='exit':
108 18 try:
108 18 try:
109 <BLANKLINE>
109 <BLANKLINE>
110 ... in div0()
110 ... in div0()
111 6 x = 1
111 6 x = 1
112 7 y = 0
112 7 y = 0
113 ----> 8 x/y
113 ----> 8 x/y
114 x = 1
114 x = 1
115 y = 0
115 y = 0
116 <BLANKLINE>
116 <BLANKLINE>
117 ZeroDivisionError: ...
117 ZeroDivisionError: ...
118 """
118 """
119
119
120
120
121 # TODO : Marc 2021 – this seem to fail due
121 def doctest_tb_sysexit():
122 # to upstream changes in CI for whatever reason.
122 """
123 # Commenting for now, to revive someday (maybe?)
123 In [17]: %xmode plain
124 # nose won't work in 3.10 anyway and we'll have to disable iptest.
124 Exception reporting mode: Plain
125 # thus this likely need to bemigrated to pytest.
125
126
126 In [18]: %run simpleerr.py exit
127
127 An exception has occurred, use %tb to see the full traceback.
128 # def doctest_tb_sysexit():
128 SystemExit: (1, 'Mode = exit')
129 # """
129
130 # In [17]: %xmode plain
130 In [19]: %run simpleerr.py exit 2
131 # Exception reporting mode: Plain
131 An exception has occurred, use %tb to see the full traceback.
132 #
132 SystemExit: (2, 'Mode = exit')
133 # In [18]: %run simpleerr.py exit
133
134 # An exception has occurred, use %tb to see the full traceback.
134 In [20]: %tb
135 # SystemExit: (1, 'Mode = exit')
135 Traceback (most recent call last):
136 #
136 File ... in <module>
137 # In [19]: %run simpleerr.py exit 2
137 bar(mode)
138 # An exception has occurred, use %tb to see the full traceback.
138 File ... line 22, in bar
139 # SystemExit: (2, 'Mode = exit')
139 sysexit(stat, mode)
140 #
140 File ... line 11, in sysexit
141 # In [20]: %tb
141 raise SystemExit(stat, f'Mode = {mode}')
142 # Traceback (most recent call last):
142 SystemExit: (2, 'Mode = exit')
143 # File ... in <module>
143
144 # bar(mode)
144 In [21]: %xmode context
145 # File ... line 22, in bar
145 Exception reporting mode: Context
146 # sysexit(stat, mode)
146
147 # File ... line 11, in sysexit
147 In [22]: %tb
148 # raise SystemExit(stat, 'Mode = %s' % mode)
148 ---------------------------------------------------------------------------
149 # SystemExit: (2, 'Mode = exit')
149 SystemExit Traceback (most recent call last)
150 #
150 <BLANKLINE>
151 # In [21]: %xmode context
151 ...<module>
152 # Exception reporting mode: Context
152 29 except IndexError:
153 #
153 30 mode = 'div'
154 # In [22]: %tb
154 ---> 32 bar(mode)
155 # ---------------------------------------------------------------------------
155 <BLANKLINE>
156 # SystemExit Traceback (most recent call last)
156 ...bar(mode)
157 # <BLANKLINE>
157 20 except:
158 # ...<module>
158 21 stat = 1
159 # 29 except IndexError:
159 ---> 22 sysexit(stat, mode)
160 # 30 mode = 'div'
160 23 else:
161 # ---> 32 bar(mode)
161 24 raise ValueError('Unknown mode')
162 # <BLANKLINE>
162 <BLANKLINE>
163 # ...bar(mode)
163 ...sysexit(stat, mode)
164 # 20 except:
164 10 def sysexit(stat, mode):
165 # 21 stat = 1
165 ---> 11 raise SystemExit(stat, f'Mode = {mode}')
166 # ---> 22 sysexit(stat, mode)
166 <BLANKLINE>
167 # 23 else:
167 SystemExit: (2, 'Mode = exit')
168 # 24 raise ValueError('Unknown mode')
168
169 # <BLANKLINE>
169 In [23]: %xmode verbose
170 # ...sysexit(stat, mode)
170 Exception reporting mode: Verbose
171 # 10 def sysexit(stat, mode):
171
172 # ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
172 In [24]: %tb
173 # <BLANKLINE>
173 ---------------------------------------------------------------------------
174 # SystemExit: (2, 'Mode = exit')
174 SystemExit Traceback (most recent call last)
175 #
175 <BLANKLINE>
176 # In [23]: %xmode verbose
176 ... in <module>
177 # Exception reporting mode: Verbose
177 29 except IndexError:
178 #
178 30 mode = 'div'
179 # In [24]: %tb
179 ---> 32 bar(mode)
180 # ---------------------------------------------------------------------------
180 mode = 'exit'
181 # SystemExit Traceback (most recent call last)
181 <BLANKLINE>
182 # <BLANKLINE>
182 ... in bar(mode='exit')
183 # ... in <module>
183 20 except:
184 # 29 except IndexError:
184 21 stat = 1
185 # 30 mode = 'div'
185 ---> 22 sysexit(stat, mode)
186 # ---> 32 bar(mode)
186 mode = 'exit'
187 # mode = 'exit'
187 stat = 2
188 # <BLANKLINE>
188 23 else:
189 # ... in bar(mode='exit')
189 24 raise ValueError('Unknown mode')
190 # 20 except:
190 <BLANKLINE>
191 # 21 stat = 1
191 ... in sysexit(stat=2, mode='exit')
192 # ---> 22 sysexit(stat, mode)
192 10 def sysexit(stat, mode):
193 # mode = 'exit'
193 ---> 11 raise SystemExit(stat, f'Mode = {mode}')
194 # stat = 2
194 stat = 2
195 # 23 else:
195 <BLANKLINE>
196 # 24 raise ValueError('Unknown mode')
196 SystemExit: (2, 'Mode = exit')
197 # <BLANKLINE>
197 """
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 # """
206
198
207
199
208 def test_run_cell():
200 def test_run_cell():
209 import textwrap
201 import textwrap
210
202
211 ip.run_cell("a = 10\na+=1")
203 ip.run_cell("a = 10\na+=1")
212 ip.run_cell("assert a == 11\nassert 1")
204 ip.run_cell("assert a == 11\nassert 1")
213
205
214 assert ip.user_ns["a"] == 11
206 assert ip.user_ns["a"] == 11
215 complex = textwrap.dedent(
207 complex = textwrap.dedent(
216 """
208 """
217 if 1:
209 if 1:
218 print "hello"
210 print "hello"
219 if 1:
211 if 1:
220 print "world"
212 print "world"
221
213
222 if 2:
214 if 2:
223 print "foo"
215 print "foo"
224
216
225 if 3:
217 if 3:
226 print "bar"
218 print "bar"
227
219
228 if 4:
220 if 4:
229 print "bar"
221 print "bar"
230
222
231 """)
223 """)
232 # Simply verifies that this kind of input is run
224 # Simply verifies that this kind of input is run
233 ip.run_cell(complex)
225 ip.run_cell(complex)
234
226
235
227
236 def test_db():
228 def test_db():
237 """Test the internal database used for variable persistence."""
229 """Test the internal database used for variable persistence."""
238 ip.db["__unittest_"] = 12
230 ip.db["__unittest_"] = 12
239 assert ip.db["__unittest_"] == 12
231 assert ip.db["__unittest_"] == 12
240 del ip.db["__unittest_"]
232 del ip.db["__unittest_"]
241 assert "__unittest_" not in ip.db
233 assert "__unittest_" not in ip.db
General Comments 0
You need to be logged in to leave comments. Login now