Debian Codegraph @

mypyc/test-data/exceptions.test

raw
1 -- Test cases for exception handling insertion transform.
2 --
3 -- The result includes refcount handling since these two transforms interact.
4
5 [case testListGetAndUnboxError]
6 from typing import List
7 def f(x: List[int]) -> int:
8 return x[0]
9 [out]
10 def f(x):
11 x :: list
12 r0 :: object
13 r1, r2 :: int
14 L0:
15 r0 = CPyList_GetItemShort(x, 0)
16 if is_error(r0) goto L3 (error at f:3) else goto L1
17 L1:
18 r1 = unbox(int, r0)
19 dec_ref r0
20 if is_error(r1) goto L3 (error at f:3) else goto L2
21 L2:
22 return r1
23 L3:
24 r2 = <error> :: int
25 return r2
26
27 [case testListAppendAndSetItemError]
28 from typing import List
29 def f(x: List[int], y: int, z: int) -> None:
30 x.append(y)
31 x[y] = z
32 [out]
33 def f(x, y, z):
34 x :: list
35 y, z :: int
36 r0 :: object
37 r1 :: i32
38 r2 :: bit
39 r3 :: object
40 r4 :: bit
41 r5 :: None
42 L0:
43 inc_ref y :: int
44 r0 = box(int, y)
45 r1 = PyList_Append(x, r0)
46 dec_ref r0
47 r2 = r1 >= 0 :: signed
48 if not r2 goto L3 (error at f:3) else goto L1 :: bool
49 L1:
50 inc_ref z :: int
51 r3 = box(int, z)
52 r4 = CPyList_SetItem(x, y, r3)
53 if not r4 goto L3 (error at f:4) else goto L2 :: bool
54 L2:
55 return 1
56 L3:
57 r5 = <error> :: None
58 return r5
59
60 [case testOptionalHandling]
61 from typing import Optional
62
63 class A: pass
64
65 def f(x: Optional[A]) -> int:
66 if x is None:
67 return 1
68 if x is not None:
69 return 2
70 return 3
71 [out]
72 def f(x):
73 x :: union[__main__.A, None]
74 r0 :: object
75 r1 :: bit
76 r2 :: __main__.A
77 r3 :: object
78 r4 :: bit
79 r5 :: int
80 L0:
81 r0 = load_address _Py_NoneStruct
82 r1 = x == r0
83 if r1 goto L1 else goto L2 :: bool
84 L1:
85 return 2
86 L2:
87 r2 = borrow cast(__main__.A, x)
88 if is_error(r2) goto L6 (error at f:8) else goto L3
89 L3:
90 r3 = load_address _Py_NoneStruct
91 r4 = r2 != r3
92 if r4 goto L4 else goto L5 :: bool
93 L4:
94 return 4
95 L5:
96 return 6
97 L6:
98 r5 = <error> :: int
99 return r5
100
101 [case testListSum]
102 from typing import List
103 def sum(a: List[int], l: int) -> int:
104 sum = 0
105 i = 0
106 while i < l:
107 sum = sum + a[i]
108 i = i + 1
109 return sum
110 [out]
111 def sum(a, l):
112 a :: list
113 l, sum, i :: int
114 r0 :: bit
115 r1 :: object
116 r2, r3, r4, r5 :: int
117 L0:
118 sum = 0
119 i = 0
120 L1:
121 r0 = int_lt i, l
122 if r0 goto L2 else goto L7 :: bool
123 L2:
124 r1 = CPyList_GetItemBorrow(a, i)
125 if is_error(r1) goto L8 (error at sum:6) else goto L3
126 L3:
127 r2 = unbox(int, r1)
128 if is_error(r2) goto L8 (error at sum:6) else goto L4
129 L4:
130 r3 = CPyTagged_Add(sum, r2)
131 dec_ref sum :: int
132 dec_ref r2 :: int
133 sum = r3
134 r4 = CPyTagged_Add(i, 2)
135 dec_ref i :: int
136 i = r4
137 goto L1
138 L5:
139 return sum
140 L6:
141 r5 = <error> :: int
142 return r5
143 L7:
144 dec_ref i :: int
145 goto L5
146 L8:
147 dec_ref sum :: int
148 dec_ref i :: int
149 goto L6
150
151 [case testTryExcept]
152 def g() -> None:
153 try:
154 object()
155 except:
156 print("weeee")
157 [out]
158 def g():
159 r0 :: object
160 r1 :: str
161 r2, r3 :: object
162 r4 :: tuple[object, object, object]
163 r5 :: str
164 r6 :: object
165 r7 :: str
166 r8 :: object
167 r9 :: object[1]
168 r10 :: object_ptr
169 r11 :: object
170 r12 :: bit
171 r13 :: None
172 L0:
173 L1:
174 r0 = builtins :: module
175 r1 = 'object'
176 r2 = CPyObject_GetAttr(r0, r1)
177 if is_error(r2) goto L3 (error at g:3) else goto L2
178 L2:
179 r3 = PyObject_Vectorcall(r2, 0, 0, 0)
180 dec_ref r2
181 if is_error(r3) goto L3 (error at g:3) else goto L10
182 L3:
183 r4 = CPy_CatchError()
184 r5 = 'weeee'
185 r6 = builtins :: module
186 r7 = 'print'
187 r8 = CPyObject_GetAttr(r6, r7)
188 if is_error(r8) goto L6 (error at g:5) else goto L4
189 L4:
190 r9 = [r5]
191 r10 = load_address r9
192 r11 = PyObject_Vectorcall(r8, r10, 1, 0)
193 dec_ref r8
194 if is_error(r11) goto L6 (error at g:5) else goto L11
195 L5:
196 CPy_RestoreExcInfo(r4)
197 dec_ref r4
198 goto L8
199 L6:
200 CPy_RestoreExcInfo(r4)
201 dec_ref r4
202 r12 = CPy_KeepPropagating()
203 if not r12 goto L9 else goto L7 :: bool
204 L7:
205 unreachable
206 L8:
207 return 1
208 L9:
209 r13 = <error> :: None
210 return r13
211 L10:
212 dec_ref r3
213 goto L8
214 L11:
215 dec_ref r11
216 goto L5
217
218 [case testGenopsTryFinally]
219 def a() -> str:
220 try:
221 print()
222 return 'hi'
223 finally:
224 print('goodbye!')
225 [out]
226 def a():
227 r0 :: object
228 r1 :: str
229 r2, r3 :: object
230 r4, r5 :: str
231 r6, r7 :: tuple[object, object, object]
232 r8 :: str
233 r9 :: tuple[object, object, object]
234 r10 :: str
235 r11 :: object
236 r12 :: str
237 r13 :: object
238 r14 :: object[1]
239 r15 :: object_ptr
240 r16 :: object
241 r17 :: bit
242 r18 :: str
243 L0:
244 L1:
245 r0 = builtins :: module
246 r1 = 'print'
247 r2 = CPyObject_GetAttr(r0, r1)
248 if is_error(r2) goto L5 (error at a:3) else goto L2
249 L2:
250 r3 = PyObject_Vectorcall(r2, 0, 0, 0)
251 dec_ref r2
252 if is_error(r3) goto L5 (error at a:3) else goto L19
253 L3:
254 r4 = 'hi'
255 inc_ref r4
256 r5 = r4
257 L4:
258 r6 = <error> :: tuple[object, object, object]
259 r7 = r6
260 goto L6
261 L5:
262 r8 = <error> :: str
263 r5 = r8
264 r9 = CPy_CatchError()
265 r7 = r9
266 L6:
267 r10 = 'goodbye!'
268 r11 = builtins :: module
269 r12 = 'print'
270 r13 = CPyObject_GetAttr(r11, r12)
271 if is_error(r13) goto L20 (error at a:6) else goto L7
272 L7:
273 r14 = [r10]
274 r15 = load_address r14
275 r16 = PyObject_Vectorcall(r13, r15, 1, 0)
276 dec_ref r13
277 if is_error(r16) goto L20 (error at a:6) else goto L21
278 L8:
279 if is_error(r7) goto L11 else goto L22
280 L9:
281 CPy_Reraise()
282 if not 0 goto L13 else goto L23 :: bool
283 L10:
284 unreachable
285 L11:
286 if is_error(r5) goto L17 else goto L12
287 L12:
288 return r5
289 L13:
290 if is_error(r7) goto L15 else goto L14
291 L14:
292 CPy_RestoreExcInfo(r7)
293 xdec_ref r7
294 L15:
295 r17 = CPy_KeepPropagating()
296 if not r17 goto L18 else goto L16 :: bool
297 L16:
298 unreachable
299 L17:
300 unreachable
301 L18:
302 r18 = <error> :: str
303 return r18
304 L19:
305 dec_ref r3
306 goto L3
307 L20:
308 xdec_ref r5
309 goto L13
310 L21:
311 dec_ref r16
312 goto L8
313 L22:
314 xdec_ref r5
315 goto L9
316 L23:
317 xdec_ref r7
318 goto L10
319
320 [case testDocstring1]
321 def lol() -> None:
322 """Hello"""
323 pass
324 [out]
325 def lol():
326 L0:
327 return 1
328
329 [case testExceptUndefined1]
330 from typing import Any
331 def lol(x: Any) -> object:
332 try:
333 st = x.foo
334 except:
335 return ''
336 # No uninit check should be generated, since the exception branch always returns
337 return st
338 [out]
339 def lol(x):
340 x :: object
341 r0 :: str
342 r1, st :: object
343 r2 :: tuple[object, object, object]
344 r3 :: str
345 L0:
346 L1:
347 r0 = 'foo'
348 r1 = CPyObject_GetAttr(x, r0)
349 if is_error(r1) goto L3 (error at lol:4) else goto L2
350 L2:
351 st = r1
352 goto L4
353 L3:
354 r2 = CPy_CatchError()
355 r3 = ''
356 CPy_RestoreExcInfo(r2)
357 dec_ref r2
358 inc_ref r3
359 return r3
360 L4:
361 return st
362
363 [case testExceptUndefined2]
364 from typing import Any
365 def lol(x: Any) -> object:
366 try:
367 a = x.foo
368 b = x.bar
369 except:
370 pass
371 # uninit checks are needed, since the exception can skip initializing the vars
372 return a + b
373 [out]
374 def lol(x):
375 x, r0, a, r1, b :: object
376 r2 :: str
377 r3 :: object
378 r4 :: str
379 r5 :: object
380 r6 :: tuple[object, object, object]
381 r7, r8 :: bool
382 r9, r10 :: object
383 L0:
384 r0 = <error> :: object
385 a = r0
386 r1 = <error> :: object
387 b = r1
388 L1:
389 r2 = 'foo'
390 r3 = CPyObject_GetAttr(x, r2)
391 if is_error(r3) goto L4 (error at lol:4) else goto L15
392 L2:
393 a = r3
394 r4 = 'bar'
395 r5 = CPyObject_GetAttr(x, r4)
396 if is_error(r5) goto L4 (error at lol:5) else goto L16
397 L3:
398 b = r5
399 goto L6
400 L4:
401 r6 = CPy_CatchError()
402 L5:
403 CPy_RestoreExcInfo(r6)
404 dec_ref r6
405 L6:
406 if is_error(a) goto L17 else goto L9
407 L7:
408 r7 = raise UnboundLocalError('local variable "a" referenced before assignment')
409 if not r7 goto L14 (error at lol:9) else goto L8 :: bool
410 L8:
411 unreachable
412 L9:
413 if is_error(b) goto L18 else goto L12
414 L10:
415 r8 = raise UnboundLocalError('local variable "b" referenced before assignment')
416 if not r8 goto L14 (error at lol:9) else goto L11 :: bool
417 L11:
418 unreachable
419 L12:
420 r9 = PyNumber_Add(a, b)
421 xdec_ref a
422 xdec_ref b
423 if is_error(r9) goto L14 (error at lol:9) else goto L13
424 L13:
425 return r9
426 L14:
427 r10 = <error> :: object
428 return r10
429 L15:
430 xdec_ref a
431 goto L2
432 L16:
433 xdec_ref b
434 goto L3
435 L17:
436 xdec_ref b
437 goto L7
438 L18:
439 xdec_ref a
440 goto L10
441
442 [case testMaybeUninitVarExc]
443 def f(b: bool) -> None:
444 u = 'a'
445 while b:
446 v = 'b'
447 if v is not u:
448 break
449 print(v)
450 [out]
451 def f(b):
452 b :: bool
453 r0, v, r1, u, r2 :: str
454 r3, r4 :: bit
455 r5 :: object
456 r6 :: str
457 r7 :: object
458 r8 :: bool
459 r9 :: object[1]
460 r10 :: object_ptr
461 r11 :: object
462 r12 :: bool
463 r13 :: None
464 L0:
465 r0 = <error> :: str
466 v = r0
467 r1 = 'a'
468 inc_ref r1
469 u = r1
470 L1:
471 if b goto L13 else goto L14 :: bool
472 L2:
473 r2 = 'b'
474 inc_ref r2
475 v = r2
476 r3 = v == u
477 r4 = r3 ^ 1
478 if r4 goto L14 else goto L1 :: bool
479 L3:
480 r5 = builtins :: module
481 r6 = 'print'
482 r7 = CPyObject_GetAttr(r5, r6)
483 if is_error(r7) goto L15 (error at f:7) else goto L4
484 L4:
485 if is_error(v) goto L16 else goto L7
486 L5:
487 r8 = raise UnboundLocalError('local variable "v" referenced before assignment')
488 if not r8 goto L12 (error at f:7) else goto L6 :: bool
489 L6:
490 unreachable
491 L7:
492 r9 = [v]
493 r10 = load_address r9
494 r11 = PyObject_Vectorcall(r7, r10, 1, 0)
495 dec_ref r7
496 if is_error(r11) goto L15 (error at f:7) else goto L17
497 L8:
498 if is_error(v) goto L9 else goto L11
499 L9:
500 r12 = raise UnboundLocalError('local variable "v" referenced before assignment')
501 if not r12 goto L12 (error at f:7) else goto L10 :: bool
502 L10:
503 unreachable
504 L11:
505 xdec_ref v
506 return 1
507 L12:
508 r13 = <error> :: None
509 return r13
510 L13:
511 xdec_ref v
512 goto L2
513 L14:
514 dec_ref u
515 goto L3
516 L15:
517 xdec_ref v
518 goto L12
519 L16:
520 dec_ref r7
521 goto L5
522 L17:
523 dec_ref r11
524 goto L8
525
526 [case testExceptionWithOverlappingErrorValue]
527 from mypy_extensions import i64
528
529 def f() -> i64:
530 return 0
531
532 def g() -> i64:
533 return f()
534 [out]
535 def f():
536 L0:
537 return 0
538 def g():
539 r0 :: i64
540 r1 :: bit
541 r2 :: object
542 r3 :: i64
543 L0:
544 r0 = f()
545 r1 = r0 == -113
546 if r1 goto L2 else goto L1 :: bool
547 L1:
548 return r0
549 L2:
550 r2 = PyErr_Occurred()
551 if not is_error(r2) goto L3 (error at g:7) else goto L1
552 L3:
553 r3 = <error> :: i64
554 return r3
555
556 [case testExceptionWithNativeAttributeGetAndSet]
557 class C:
558 def __init__(self, x: int) -> None:
559 self.x = x
560
561 def foo(c: C, x: int) -> None:
562 c.x = x - c.x
563 [out]
564 def C.__init__(self, x):
565 self :: __main__.C
566 x :: int
567 L0:
568 inc_ref x :: int
569 self.x = x
570 return 1
571 def foo(c, x):
572 c :: __main__.C
573 x, r0, r1 :: int
574 r2 :: bool
575 L0:
576 r0 = borrow c.x
577 r1 = CPyTagged_Subtract(x, r0)
578 c.x = r1
579 return 1
580
581 [case testExceptionWithOverlappingFloatErrorValue]
582 def f() -> float:
583 return 0.0
584
585 def g() -> float:
586 return f()
587 [out]
588 def f():
589 L0:
590 return 0.0
591 def g():
592 r0 :: float
593 r1 :: bit
594 r2 :: object
595 r3 :: float
596 L0:
597 r0 = f()
598 r1 = r0 == -113.0
599 if r1 goto L2 else goto L1 :: bool
600 L1:
601 return r0
602 L2:
603 r2 = PyErr_Occurred()
604 if not is_error(r2) goto L3 (error at g:5) else goto L1
605 L3:
606 r3 = <error> :: float
607 return r3
608
609 [case testExceptionWithLowLevelIntAttribute]
610 from mypy_extensions import i32, i64
611
612 class C:
613 def __init__(self, x: i32, y: i64) -> None:
614 self.x = x
615 self.y = y
616
617 def f(c: C) -> None:
618 c.x
619 c.y
620 [out]
621 def C.__init__(self, x, y):
622 self :: __main__.C
623 x :: i32
624 y :: i64
625 L0:
626 self.x = x
627 self.y = y
628 return 1
629 def f(c):
630 c :: __main__.C
631 r0 :: i32
632 r1 :: i64
633 L0:
634 r0 = c.x
635 r1 = c.y
636 return 1
637
638 [case testConditionallyUndefinedI64]
639 from mypy_extensions import i64
640
641 def f(x: i64) -> i64:
642 if x:
643 y: i64 = 2
644 return y
645 [out]
646 def f(x):
647 x, r0, y :: i64
648 __locals_bitmap0 :: u32
649 r1 :: bit
650 r2, r3 :: u32
651 r4 :: bit
652 r5 :: bool
653 r6 :: i64
654 L0:
655 r0 = <error> :: i64
656 y = r0
657 __locals_bitmap0 = 0
658 r1 = x != 0
659 if r1 goto L1 else goto L2 :: bool
660 L1:
661 y = 2
662 r2 = __locals_bitmap0 | 1
663 __locals_bitmap0 = r2
664 L2:
665 r3 = __locals_bitmap0 & 1
666 r4 = r3 == 0
667 if r4 goto L3 else goto L5 :: bool
668 L3:
669 r5 = raise UnboundLocalError('local variable "y" referenced before assignment')
670 if not r5 goto L6 (error at f:6) else goto L4 :: bool
671 L4:
672 unreachable
673 L5:
674 return y
675 L6:
676 r6 = <error> :: i64
677 return r6
678
679 [case testExceptionWithFloatAttribute]
680 class C:
681 def __init__(self, x: float, y: float) -> None:
682 self.x = x
683 if x:
684 self.y = y
685
686 def f(c: C) -> float:
687 return c.x + c.y
688 [out]
689 def C.__init__(self, x, y):
690 self :: __main__.C
691 x, y :: float
692 r0 :: bit
693 L0:
694 self.x = x
695 r0 = x != 0.0
696 if r0 goto L1 else goto L2 :: bool
697 L1:
698 self.y = y
699 L2:
700 return 1
701 def f(c):
702 c :: __main__.C
703 r0, r1 :: float
704 r2 :: bit
705 r3 :: float
706 r4 :: object
707 r5 :: float
708 L0:
709 r0 = c.x
710 r1 = c.y
711 r2 = r1 == -113.0
712 if r2 goto L2 else goto L1 :: bool
713 L1:
714 r3 = r0 + r1
715 return r3
716 L2:
717 r4 = PyErr_Occurred()
718 if not is_error(r4) goto L3 (error at f:8) else goto L1
719 L3:
720 r5 = <error> :: float
721 return r5
722
723 [case testVecI64Remove]
724 from librt.vecs import vec, remove
725 from mypy_extensions import i64
726
727 def f(v: vec[i64], i: i64, n: i64) -> None:
728 v = remove(v, n)
729 [out]
730 def f(v, i, n):
731 v :: vec[i64]
732 i, n :: i64
733 r0 :: vec[i64]
734 r1 :: None
735 L0:
736 inc_ref v
737 r0 = VecI64Api.remove(v, n)
738 if is_error(r0) goto L2 (error at f:5) else goto L1
739 L1:
740 v = r0
741 dec_ref v
742 return 1
743 L2:
744 r1 = <error> :: None
745 return r1