Debian Codegraph @

mypyc/test-data/run-exceptions.test

raw
1 # Test cases for exceptions (compile and run)
2
3 [case testException]
4 from typing import List
5 def f(x: List[int]) -> None:
6 g(x)
7
8 def g(x: List[int]) -> bool:
9 x[5] = 2
10 return True
11
12 def r1() -> None:
13 q1()
14
15 def q1() -> None:
16 raise Exception("test")
17
18 def r2() -> None:
19 q2()
20
21 def q2() -> None:
22 raise Exception
23
24 class A:
25 def __init__(self) -> None:
26 raise Exception
27
28 def hey() -> None:
29 A()
30
31 [file driver.py]
32 from native import f, r1, r2, hey
33 import traceback
34 try:
35 f([])
36 except IndexError:
37 traceback.print_exc()
38 try:
39 r1()
40 except Exception:
41 traceback.print_exc()
42 try:
43 r2()
44 except Exception:
45 traceback.print_exc()
46 try:
47 hey()
48 except Exception:
49 traceback.print_exc()
50 [out]
51 Traceback (most recent call last):
52 File "driver.py", line 4, in <module>
53 f([])
54 File "native.py", line 3, in f
55 g(x)
56 File "native.py", line 6, in g
57 x[5] = 2
58 IndexError: list assignment index out of range
59 Traceback (most recent call last):
60 File "driver.py", line 8, in <module>
61 r1()
62 File "native.py", line 10, in r1
63 q1()
64 File "native.py", line 13, in q1
65 raise Exception("test")
66 Exception: test
67 Traceback (most recent call last):
68 File "driver.py", line 12, in <module>
69 r2()
70 File "native.py", line 16, in r2
71 q2()
72 File "native.py", line 19, in q2
73 raise Exception
74 Exception
75 Traceback (most recent call last):
76 File "driver.py", line 16, in <module>
77 hey()
78 File "native.py", line 26, in hey
79 A()
80 File "native.py", line 23, in __init__
81 raise Exception
82 Exception
83 [out version>=3.13]
84 Traceback (most recent call last):
85 File "driver.py", line 4, in <module>
86 f([])
87 ~^^^^
88 File "native.py", line 3, in f
89 g(x)
90 File "native.py", line 6, in g
91 x[5] = 2
92 IndexError: list assignment index out of range
93 Traceback (most recent call last):
94 File "driver.py", line 8, in <module>
95 r1()
96 ~~^^
97 File "native.py", line 10, in r1
98 q1()
99 File "native.py", line 13, in q1
100 raise Exception("test")
101 Exception: test
102 Traceback (most recent call last):
103 File "driver.py", line 12, in <module>
104 r2()
105 ~~^^
106 File "native.py", line 16, in r2
107 q2()
108 File "native.py", line 19, in q2
109 raise Exception
110 Exception
111 Traceback (most recent call last):
112 File "driver.py", line 16, in <module>
113 hey()
114 ~~~^^
115 File "native.py", line 26, in hey
116 A()
117 File "native.py", line 23, in __init__
118 raise Exception
119 Exception
120
121 [case testTryExcept]
122 from typing import Any, Iterator
123 import wrapsys
124 def g(b: bool) -> None:
125 try:
126 if b:
127 x = [0]
128 x[1]
129 else:
130 raise Exception('hi')
131 except:
132 print("caught!")
133
134 def r(x: int) -> None:
135 if x == 0:
136 [0][1]
137 elif x == 1:
138 raise Exception('hi')
139 elif x == 2:
140 {1: 1}[0]
141 elif x == 3:
142 a = object() # type: Any
143 a.lol
144
145 def f(b: bool) -> None:
146 try:
147 r(int(b))
148 except AttributeError:
149 print('no')
150 except:
151 print(str(wrapsys.exc_info()[1]))
152 print(str(wrapsys.exc_info()[1]))
153
154 def h() -> None:
155 while True:
156 try:
157 raise Exception('gonna break')
158 except:
159 print(str(wrapsys.exc_info()[1]))
160 break
161 print(str(wrapsys.exc_info()[1]))
162
163 def i() -> None:
164 try:
165 r(0)
166 except:
167 print(type(wrapsys.exc_info()[1]))
168 raise
169
170 def j(n: int) -> None:
171 try:
172 r(n)
173 except (IndexError, KeyError):
174 print("lookup!")
175 except AttributeError as e:
176 print("attr! --", e)
177
178 def k() -> None:
179 try:
180 r(1)
181 except:
182 r(0)
183
184 def l() -> None:
185 try:
186 r(0)
187 except IndexError:
188 try:
189 r(2)
190 except KeyError as e:
191 print("key! --", e)
192
193 def m(x: object) -> int:
194 try:
195 st = id(x)
196 except Exception:
197 return -1
198 return st + 1
199
200 def iter_exception() -> Iterator[str]:
201 try:
202 r(0)
203 except KeyError as e:
204 yield 'lol'
205
206 [file wrapsys.py]
207 # This is a gross hack around some limitations of the test system/mypyc.
208 from typing import Any
209 import sys
210 def exc_info() -> Any:
211 return sys.exc_info() # type: ignore
212
213 [file driver.py]
214 import sys, traceback
215 from native import g, f, h, i, j, k, l, m, iter_exception
216 from testutil import assertRaises
217 print("== i ==")
218 try:
219 i()
220 except:
221 traceback.print_exc(file=sys.stdout)
222
223 print("== k ==")
224 try:
225 k()
226 except:
227 traceback.print_exc(file=sys.stdout)
228
229 print("== g ==")
230 g(True)
231 g(False)
232
233 print("== f ==")
234 f(True)
235 f(False)
236
237 print("== h ==")
238 h()
239
240 print("== j ==")
241 j(0)
242 j(2)
243 j(3)
244 try:
245 j(1)
246 except:
247 print("out!")
248
249 print("== l ==")
250 l()
251
252 m('lol')
253
254 with assertRaises(IndexError):
255 list(iter_exception())
256
257 [out]
258 == i ==
259 <class 'IndexError'>
260 Traceback (most recent call last):
261 File "driver.py", line 6, in <module>
262 i()
263 File "native.py", line 44, in i
264 r(0)
265 File "native.py", line 15, in r
266 [0][1]
267 IndexError: list index out of range
268 == k ==
269 Traceback (most recent call last):
270 File "native.py", line 59, in k
271 r(1)
272 File "native.py", line 17, in r
273 raise Exception('hi')
274 Exception: hi
275
276 During handling of the above exception, another exception occurred:
277
278 Traceback (most recent call last):
279 File "driver.py", line 12, in <module>
280 k()
281 File "native.py", line 61, in k
282 r(0)
283 File "native.py", line 15, in r
284 [0][1]
285 IndexError: list index out of range
286 == g ==
287 caught!
288 caught!
289 == f ==
290 hi
291 None
292 list index out of range
293 None
294 == h ==
295 gonna break
296 None
297 == j ==
298 lookup!
299 lookup!
300 attr! -- 'object' object has no attribute 'lol'
301 out!
302 == l ==
303 key! -- 0
304 [out version>=3.13]
305 == i ==
306 <class 'IndexError'>
307 Traceback (most recent call last):
308 File "driver.py", line 6, in <module>
309 i()
310 ~^^
311 File "native.py", line 44, in i
312 r(0)
313 File "native.py", line 15, in r
314 [0][1]
315 IndexError: list index out of range
316 == k ==
317 Traceback (most recent call last):
318 File "native.py", line 59, in k
319 r(1)
320 File "native.py", line 17, in r
321 raise Exception('hi')
322 Exception: hi
323
324 During handling of the above exception, another exception occurred:
325
326 Traceback (most recent call last):
327 File "driver.py", line 12, in <module>
328 k()
329 ~^^
330 File "native.py", line 61, in k
331 r(0)
332 File "native.py", line 15, in r
333 [0][1]
334 IndexError: list index out of range
335 == g ==
336 caught!
337 caught!
338 == f ==
339 hi
340 None
341 list index out of range
342 None
343 == h ==
344 gonna break
345 None
346 == j ==
347 lookup!
348 lookup!
349 attr! -- 'object' object has no attribute 'lol'
350 out!
351 == l ==
352 key! -- 0
353
354 [case testTryFinally]
355 from typing import Any
356 import wrapsys
357
358 def a(b1: bool, b2: int) -> None:
359 try:
360 if b1:
361 raise Exception('hi')
362 finally:
363 print('finally:', str(wrapsys.exc_info()[1]))
364 if b2 == 2:
365 return
366 if b2 == 1:
367 raise Exception('again!')
368
369 def b(b1: int, b2: int) -> str:
370 try:
371 if b1 == 1:
372 raise Exception('hi')
373 elif b1 == 2:
374 [0][1]
375 elif b1 == 3:
376 return 'try'
377 except IndexError:
378 print('except')
379 finally:
380 print('finally:', str(wrapsys.exc_info()[1]))
381 if b2 == 2:
382 return 'finally'
383 if b2 == 1:
384 raise Exception('again!')
385 return 'outer'
386
387 def c() -> str:
388 try:
389 try:
390 return 'wee'
391 finally:
392 print("out a")
393 finally:
394 print("out b")
395
396
397 [file wrapsys.py]
398 # This is a gross hack around some limitations of the test system/mypyc.
399 from typing import Any
400 import sys
401 def exc_info() -> Any:
402 return sys.exc_info() # type: ignore
403
404 [file driver.py]
405 import traceback
406 import sys
407 from native import a, b, c
408
409 def run(f):
410 try:
411 x = f()
412 if x:
413 print("returned:", x)
414 except Exception as e:
415 print("caught:", type(e).__name__ + ": " + str(e))
416
417 print("== a ==")
418 for i in range(3):
419 for b1 in [False, True]:
420 run(lambda: a(b1, i))
421
422 print("== b ==")
423 for i in range(4):
424 for j in range(3):
425 run(lambda: b(i, j))
426
427 print("== b ==")
428 print(c())
429
430 [out]
431 == a ==
432 finally: None
433 finally: hi
434 caught: Exception: hi
435 finally: None
436 caught: Exception: again!
437 finally: hi
438 caught: Exception: again!
439 finally: None
440 finally: hi
441 == b ==
442 finally: None
443 returned: outer
444 finally: None
445 caught: Exception: again!
446 finally: None
447 returned: finally
448 finally: hi
449 caught: Exception: hi
450 finally: hi
451 caught: Exception: again!
452 finally: hi
453 returned: finally
454 except
455 finally: None
456 returned: outer
457 except
458 finally: None
459 caught: Exception: again!
460 except
461 finally: None
462 returned: finally
463 finally: None
464 returned: try
465 finally: None
466 caught: Exception: again!
467 finally: None
468 returned: finally
469 == b ==
470 out a
471 out b
472 wee
473
474 [case testCustomException]
475 from typing import List
476
477 class ListOutOfBounds(IndexError):
478 pass
479
480 class UserListWarning(UserWarning):
481 pass
482
483 def f(l: List[int], k: int) -> int:
484 try:
485 return l[k]
486 except IndexError:
487 raise ListOutOfBounds("Ruh-roh from f!")
488
489 def g(l: List[int], k: int) -> int:
490 try:
491 return f([1,2,3], 3)
492 except ListOutOfBounds:
493 raise ListOutOfBounds("Ruh-roh from g!")
494
495 def k(l: List[int], k: int) -> int:
496 try:
497 return g([1,2,3], 3)
498 except IndexError:
499 raise UserListWarning("Ruh-roh from k!")
500
501 def h() -> int:
502 try:
503 return k([1,2,3], 3)
504 except UserWarning:
505 return -1
506
507 [file driver.py]
508 from native import h
509 assert h() == -1
510
511 [case testExceptionAtModuleTopLevel]
512 from typing import Any
513
514 def f(x: int) -> None: pass
515
516 y: Any = ''
517 f(y)
518
519 [file driver.py]
520 import traceback
521 try:
522 import native
523 except TypeError:
524 traceback.print_exc()
525 else:
526 assert False
527
528 [out]
529 Traceback (most recent call last):
530 File "driver.py", line 3, in <module>
531 import native
532 File "native.py", line 6, in <module>
533 f(y)
534 TypeError: int object expected; got str