Debian Codegraph @

mypyc/test-data/irbuild-tuple.test

raw
1 [case testTupleGet]
2 from typing import Tuple
3
4 def f(x: Tuple[Tuple[int, bool], bool]) -> int:
5 return x[0][0]
6 [out]
7 def f(x):
8 x :: tuple[tuple[int, bool], bool]
9 r0 :: tuple[int, bool]
10 r1 :: int
11 L0:
12 r0 = x[0]
13 r1 = r0[0]
14 return r1
15
16 [case testTupleNew]
17 from typing import Tuple
18
19 def f() -> int:
20 t = (True, 1)
21 return t[1]
22 [out]
23 def f():
24 r0, t :: tuple[bool, int]
25 r1 :: int
26 L0:
27 r0 = (1, 2)
28 t = r0
29 r1 = t[1]
30 return r1
31
32 [case testTupleLen]
33 from typing import Tuple
34 def f(x: Tuple[bool, bool, int]) -> int:
35 return len(x)
36 [out]
37 def f(x):
38 x :: tuple[bool, bool, int]
39 L0:
40 return 6
41
42 [case testSequenceTuple]
43 from typing import List
44 def f(x: List[bool]) -> bool:
45 return tuple(x)[1]
46 [out]
47 def f(x):
48 x :: list
49 r0 :: tuple
50 r1 :: object
51 r2 :: bool
52 L0:
53 r0 = PyList_AsTuple(x)
54 r1 = CPySequenceTuple_GetItem(r0, 2)
55 r2 = unbox(bool, r1)
56 return r2
57
58 [case testSequenceTupleLen]
59 from typing import Tuple
60 def f(x: Tuple[int, ...]) -> int:
61 return len(x)
62 [out]
63 def f(x):
64 x :: tuple
65 r0 :: native_int
66 r1 :: short_int
67 L0:
68 r0 = var_object_size x
69 r1 = r0 << 1
70 return r1
71
72 [case testSequenceTupleForced]
73 from typing import Tuple
74 def f() -> int:
75 t = (1, 2) # type: Tuple[int, ...]
76 return t[1]
77 [out]
78 def f():
79 r0 :: tuple[int, int]
80 r1 :: object
81 t :: tuple
82 r2 :: object
83 r3 :: int
84 L0:
85 r0 = (2, 4)
86 r1 = box(tuple[int, int], r0)
87 t = r1
88 r2 = CPySequenceTuple_GetItem(t, 2)
89 r3 = unbox(int, r2)
90 return r3
91
92 [case testTupleDisplay]
93 from typing import Sequence, Tuple
94 def f(x: Sequence[int], y: Sequence[int]) -> Tuple[int, ...]:
95 return (1, 2, *x, *y, 3)
96 [out]
97 def f(x, y):
98 x, y :: object
99 r0 :: list
100 r1, r2 :: object
101 r3 :: ptr
102 r4, r5, r6 :: object
103 r7 :: i32
104 r8 :: bit
105 r9 :: tuple
106 L0:
107 r0 = PyList_New(2)
108 r1 = object 1
109 r2 = object 2
110 r3 = list_items r0
111 buf_init_item r3, 0, r1
112 buf_init_item r3, 1, r2
113 keep_alive r0
114 r4 = CPyList_Extend(r0, x)
115 r5 = CPyList_Extend(r0, y)
116 r6 = object 3
117 r7 = PyList_Append(r0, r6)
118 r8 = r7 >= 0 :: signed
119 r9 = PyList_AsTuple(r0)
120 return r9
121
122 [case testTupleFor]
123 from typing import Tuple, List
124 def f(xs: Tuple[str, ...]) -> None:
125 for x in xs:
126 pass
127 [out]
128 def f(xs):
129 xs :: tuple
130 r0, r1 :: native_int
131 r2 :: bit
132 r3 :: object
133 r4, x :: str
134 r5 :: native_int
135 L0:
136 r0 = var_object_size xs
137 r1 = 0
138 L1:
139 r2 = r1 < r0 :: signed
140 if r2 goto L2 else goto L4 :: bool
141 L2:
142 r3 = CPySequenceTuple_GetItemUnsafe(xs, r1)
143 r4 = cast(str, r3)
144 x = r4
145 L3:
146 r5 = r1 + 1
147 r1 = r5
148 goto L1
149 L4:
150 return 1
151
152 [case testNamedTupleAttribute]
153 from typing import NamedTuple
154
155 NT = NamedTuple('NT', [('x', int), ('y', int)])
156
157 def f(nt: NT, b: bool) -> int:
158 if b:
159 return nt.x
160 return nt.y
161 [out]
162 def f(nt, b):
163 nt :: tuple
164 b :: bool
165 r0 :: object
166 r1 :: int
167 r2 :: object
168 r3 :: int
169 L0:
170 if b goto L1 else goto L2 :: bool
171 L1:
172 r0 = CPySequenceTuple_GetItem(nt, 0)
173 r1 = unbox(int, r0)
174 return r1
175 L2:
176 r2 = CPySequenceTuple_GetItem(nt, 2)
177 r3 = unbox(int, r2)
178 return r3
179
180
181 [case testTupleOperatorIn]
182 def f(i: int) -> bool:
183 return i in [1, 2, 3]
184 [out]
185 def f(i):
186 i :: int
187 r0, r1, r2 :: bit
188 r3 :: bool
189 L0:
190 r0 = int_eq i, 2
191 if r0 goto L4 else goto L1 :: bool
192 L1:
193 r1 = int_eq i, 4
194 if r1 goto L4 else goto L2 :: bool
195 L2:
196 r2 = int_eq i, 6
197 if r2 goto L4 else goto L3 :: bool
198 L3:
199 r3 = 0
200 goto L5
201 L4:
202 r3 = 1
203 L5:
204 return r3
205
206 [case testTupleOperatorNotIn]
207 def x() -> int:
208 return 1
209 def y() -> int:
210 return 2
211 def z() -> int:
212 return 3
213
214 def f() -> bool:
215 return z() not in (x(), y())
216 [out]
217 def x():
218 L0:
219 return 2
220 def y():
221 L0:
222 return 4
223 def z():
224 L0:
225 return 6
226 def f():
227 r0, r1, r2 :: int
228 r3, r4 :: bit
229 r5 :: bool
230 L0:
231 r0 = z()
232 r1 = x()
233 r2 = y()
234 r3 = int_ne r0, r1
235 if r3 goto L1 else goto L3 :: bool
236 L1:
237 r4 = int_ne r0, r2
238 if r4 goto L2 else goto L3 :: bool
239 L2:
240 r5 = 1
241 goto L4
242 L3:
243 r5 = 0
244 L4:
245 return r5
246
247 [case testTupleOperatorInFinalTuple]
248 from typing import Final
249
250 tt: Final = (1, 2)
251
252 def f(x: int) -> bool:
253 return x in tt
254 [out]
255 def f(x):
256 x :: int
257 r0 :: tuple[int, int]
258 r1 :: bool
259 r2, r3 :: int
260 r4, r5 :: bit
261 r6 :: bool
262 L0:
263 r0 = __main__.tt :: static
264 if is_error(r0) goto L1 else goto L2
265 L1:
266 r1 = raise NameError('value for final name "tt" was not set')
267 unreachable
268 L2:
269 r2 = r0[0]
270 r3 = r0[1]
271 r4 = int_eq x, r2
272 if r4 goto L5 else goto L3 :: bool
273 L3:
274 r5 = int_eq x, r3
275 if r5 goto L5 else goto L4 :: bool
276 L4:
277 r6 = 0
278 goto L6
279 L5:
280 r6 = 1
281 L6:
282 return r6
283
284 [case testTupleBuiltFromList]
285 def f(val: int) -> bool:
286 return val % 2 == 0
287
288 def test() -> None:
289 source = [1, 2, 3]
290 a = tuple(f(x) for x in source)
291 [out]
292 def f(val):
293 val, r0 :: int
294 r1 :: bit
295 L0:
296 r0 = CPyTagged_Remainder(val, 4)
297 r1 = int_eq r0, 0
298 return r1
299 def test():
300 r0 :: list
301 r1, r2, r3 :: object
302 r4 :: ptr
303 source :: list
304 r5 :: native_int
305 r6 :: tuple
306 r7, r8 :: native_int
307 r9 :: bit
308 r10 :: object
309 r11, x :: int
310 r12 :: bool
311 r13 :: object
312 r14 :: native_int
313 a :: tuple
314 L0:
315 r0 = PyList_New(3)
316 r1 = object 1
317 r2 = object 2
318 r3 = object 3
319 r4 = list_items r0
320 buf_init_item r4, 0, r1
321 buf_init_item r4, 1, r2
322 buf_init_item r4, 2, r3
323 keep_alive r0
324 source = r0
325 r5 = var_object_size source
326 r6 = PyTuple_New(r5)
327 r7 = 0
328 L1:
329 r8 = var_object_size source
330 r9 = r7 < r8 :: signed
331 if r9 goto L2 else goto L4 :: bool
332 L2:
333 r10 = list_get_item_unsafe source, r7
334 r11 = unbox(int, r10)
335 x = r11
336 r12 = f(x)
337 r13 = box(bool, r12)
338 CPySequenceTuple_SetItemUnsafe(r6, r7, r13)
339 L3:
340 r14 = r7 + 1
341 r7 = r14
342 goto L1
343 L4:
344 a = r6
345 return 1
346
347 [case testTupleBuiltFromStr]
348 def f2(val: str) -> str:
349 return val + "f2"
350
351 def test() -> None:
352 source = "abc"
353 a = tuple(f2(x) for x in source)
354 [out]
355 def f2(val):
356 val, r0, r1 :: str
357 L0:
358 r0 = 'f2'
359 r1 = PyUnicode_Concat(val, r0)
360 return r1
361 def test():
362 r0, source :: str
363 r1 :: native_int
364 r2 :: bit
365 r3 :: tuple
366 r4 :: native_int
367 r5 :: bit
368 r6, x, r7 :: str
369 r8 :: native_int
370 a :: tuple
371 L0:
372 r0 = 'abc'
373 source = r0
374 r1 = CPyStr_Size_size_t(source)
375 r2 = r1 >= 0 :: signed
376 r3 = PyTuple_New(r1)
377 r4 = 0
378 L1:
379 r5 = r4 < r1 :: signed
380 if r5 goto L2 else goto L4 :: bool
381 L2:
382 r6 = CPyStr_GetItemUnsafe(source, r4)
383 x = r6
384 r7 = f2(x)
385 CPySequenceTuple_SetItemUnsafe(r3, r4, r7)
386 L3:
387 r8 = r4 + 1
388 r4 = r8
389 goto L1
390 L4:
391 a = r3
392 return 1
393
394 [case testTupleBuiltFromStrExpr]
395 def f2(val: str) -> str:
396 return val + "f2"
397
398 def test() -> None:
399 a = tuple(f2(x) for x in "abc")
400
401 [out]
402 def f2(val):
403 val, r0, r1 :: str
404 L0:
405 r0 = 'f2'
406 r1 = PyUnicode_Concat(val, r0)
407 return r1
408 def test():
409 r0 :: str
410 r1 :: tuple
411 r2 :: native_int
412 r3 :: bit
413 r4, x, r5 :: str
414 r6 :: native_int
415 a :: tuple
416 L0:
417 r0 = 'abc'
418 r1 = PyTuple_New(3)
419 r2 = 0
420 goto L2
421 L1:
422 r3 = r2 < 3 :: signed
423 if r3 goto L2 else goto L4 :: bool
424 L2:
425 r4 = CPyStr_GetItemUnsafe(r0, r2)
426 x = r4
427 r5 = f2(x)
428 CPySequenceTuple_SetItemUnsafe(r1, r2, r5)
429 L3:
430 r6 = r2 + 1
431 r2 = r6
432 goto L1
433 L4:
434 a = r1
435 return 1
436
437 [case testTupleBuiltFromConstantFolding]
438 from typing import Final
439
440 c: Final = "c"
441
442 def f2(val: str) -> str:
443 return val + "f2"
444
445 def test() -> None:
446 # `"ab" + c` should constant fold to "abc"
447 a = tuple(f2(x) for x in "ab" + c)
448
449 [out]
450 def f2(val):
451 val, r0, r1 :: str
452 L0:
453 r0 = 'f2'
454 r1 = PyUnicode_Concat(val, r0)
455 return r1
456 def test():
457 r0 :: str
458 r1 :: tuple
459 r2 :: native_int
460 r3 :: bit
461 r4, x, r5 :: str
462 r6 :: native_int
463 a :: tuple
464 L0:
465 r0 = 'abc'
466 r1 = PyTuple_New(3)
467 r2 = 0
468 goto L2
469 L1:
470 r3 = r2 < 3 :: signed
471 if r3 goto L2 else goto L4 :: bool
472 L2:
473 r4 = CPyStr_GetItemUnsafe(r0, r2)
474 x = r4
475 r5 = f2(x)
476 CPySequenceTuple_SetItemUnsafe(r1, r2, r5)
477 L3:
478 r6 = r2 + 1
479 r2 = r6
480 goto L1
481 L4:
482 a = r1
483 return 1
484
485 [case testTupleBuiltFromFinalStr]
486 from typing import Final
487
488 source: Final = "abc"
489
490 def f2(val: str) -> str:
491 return val + "f2"
492
493 def test() -> None:
494 a = tuple(f2(x) for x in source)
495 [out]
496 def f2(val):
497 val, r0, r1 :: str
498 L0:
499 r0 = 'f2'
500 r1 = PyUnicode_Concat(val, r0)
501 return r1
502 def test():
503 r0 :: str
504 r1 :: tuple
505 r2 :: native_int
506 r3 :: bit
507 r4, x, r5 :: str
508 r6 :: native_int
509 a :: tuple
510 L0:
511 r0 = 'abc'
512 r1 = PyTuple_New(3)
513 r2 = 0
514 goto L2
515 L1:
516 r3 = r2 < 3 :: signed
517 if r3 goto L2 else goto L4 :: bool
518 L2:
519 r4 = CPyStr_GetItemUnsafe(r0, r2)
520 x = r4
521 r5 = f2(x)
522 CPySequenceTuple_SetItemUnsafe(r1, r2, r5)
523 L3:
524 r6 = r2 + 1
525 r2 = r6
526 goto L1
527 L4:
528 a = r1
529 return 1
530
531 [case testTupleBuiltFromBytes_64bit]
532 def f2(val: int) -> int:
533 return val + 2
534
535 def test() -> None:
536 source = b"abc"
537 a = tuple(f2(x) for x in source)
538
539 [out]
540 def f2(val):
541 val, r0 :: int
542 L0:
543 r0 = CPyTagged_Add(val, 4)
544 return r0
545 def test():
546 r0, source :: bytes
547 r1 :: native_int
548 r2 :: tuple
549 r3 :: native_int
550 r4, r5, r6 :: bit
551 r7, r8, r9, r10 :: int
552 r11 :: object
553 r12, x, r13 :: int
554 r14 :: object
555 r15 :: native_int
556 a :: tuple
557 L0:
558 r0 = b'abc'
559 source = r0
560 r1 = var_object_size source
561 r2 = PyTuple_New(r1)
562 r3 = 0
563 L1:
564 r4 = r3 < r1 :: signed
565 if r4 goto L2 else goto L8 :: bool
566 L2:
567 r5 = r3 <= 4611686018427387903 :: signed
568 if r5 goto L3 else goto L4 :: bool
569 L3:
570 r6 = r3 >= -4611686018427387904 :: signed
571 if r6 goto L5 else goto L4 :: bool
572 L4:
573 r7 = CPyTagged_FromInt64(r3)
574 r8 = r7
575 goto L6
576 L5:
577 r9 = r3 << 1
578 r8 = r9
579 L6:
580 r10 = CPyBytes_GetItem(source, r8)
581 r11 = box(int, r10)
582 r12 = unbox(int, r11)
583 x = r12
584 r13 = f2(x)
585 r14 = box(int, r13)
586 CPySequenceTuple_SetItemUnsafe(r2, r3, r14)
587 L7:
588 r15 = r3 + 1
589 r3 = r15
590 goto L1
591 L8:
592 a = r2
593 return 1
594
595 [case testTupleBuiltFromBytesExpr_64bit]
596 def f2(val: int) -> int:
597 return val + 2
598
599 def test() -> None:
600 a = tuple(f2(x) for x in b"abc")
601
602 [out]
603 def f2(val):
604 val, r0 :: int
605 L0:
606 r0 = CPyTagged_Add(val, 4)
607 return r0
608 def test():
609 r0 :: bytes
610 r1 :: tuple
611 r2 :: native_int
612 r3, r4, r5 :: bit
613 r6, r7, r8, r9 :: int
614 r10 :: object
615 r11, x, r12 :: int
616 r13 :: object
617 r14 :: native_int
618 a :: tuple
619 L0:
620 r0 = b'abc'
621 r1 = PyTuple_New(3)
622 r2 = 0
623 goto L2
624 L1:
625 r3 = r2 < 3 :: signed
626 if r3 goto L2 else goto L8 :: bool
627 L2:
628 r4 = r2 <= 4611686018427387903 :: signed
629 if r4 goto L3 else goto L4 :: bool
630 L3:
631 r5 = r2 >= -4611686018427387904 :: signed
632 if r5 goto L5 else goto L4 :: bool
633 L4:
634 r6 = CPyTagged_FromInt64(r2)
635 r7 = r6
636 goto L6
637 L5:
638 r8 = r2 << 1
639 r7 = r8
640 L6:
641 r9 = CPyBytes_GetItem(r0, r7)
642 r10 = box(int, r9)
643 r11 = unbox(int, r10)
644 x = r11
645 r12 = f2(x)
646 r13 = box(int, r12)
647 CPySequenceTuple_SetItemUnsafe(r1, r2, r13)
648 L7:
649 r14 = r2 + 1
650 r2 = r14
651 goto L1
652 L8:
653 a = r1
654 return 1
655
656 [case testTupleBuiltFromFinalBytes_64bit]
657 from typing import Final
658
659 source: Final = b"abc"
660
661 def f2(val: int) -> int:
662 return val + 2
663
664 def test() -> None:
665 a = tuple(f2(x) for x in source)
666
667 [out]
668 def f2(val):
669 val, r0 :: int
670 L0:
671 r0 = CPyTagged_Add(val, 4)
672 return r0
673 def test():
674 r0 :: bytes
675 r1 :: bool
676 r2 :: native_int
677 r3 :: tuple
678 r4 :: native_int
679 r5, r6, r7 :: bit
680 r8, r9, r10, r11 :: int
681 r12 :: object
682 r13, x, r14 :: int
683 r15 :: object
684 r16 :: native_int
685 a :: tuple
686 L0:
687 r0 = __main__.source :: static
688 if is_error(r0) goto L1 else goto L2
689 L1:
690 r1 = raise NameError('value for final name "source" was not set')
691 unreachable
692 L2:
693 r2 = var_object_size r0
694 r3 = PyTuple_New(r2)
695 r4 = 0
696 L3:
697 r5 = r4 < r2 :: signed
698 if r5 goto L4 else goto L10 :: bool
699 L4:
700 r6 = r4 <= 4611686018427387903 :: signed
701 if r6 goto L5 else goto L6 :: bool
702 L5:
703 r7 = r4 >= -4611686018427387904 :: signed
704 if r7 goto L7 else goto L6 :: bool
705 L6:
706 r8 = CPyTagged_FromInt64(r4)
707 r9 = r8
708 goto L8
709 L7:
710 r10 = r4 << 1
711 r9 = r10
712 L8:
713 r11 = CPyBytes_GetItem(r0, r9)
714 r12 = box(int, r11)
715 r13 = unbox(int, r12)
716 x = r13
717 r14 = f2(x)
718 r15 = box(int, r14)
719 CPySequenceTuple_SetItemUnsafe(r3, r4, r15)
720 L9:
721 r16 = r4 + 1
722 r4 = r16
723 goto L3
724 L10:
725 a = r3
726 return 1
727
728 [case testTupleBuiltFromFixedLengthTuple]
729 def f(val: int) -> bool:
730 return val % 2 == 0
731
732 def test() -> None:
733 source = (1, 2, 3)
734 a = tuple(f(x) for x in source)
735 [out]
736 def f(val):
737 val, r0 :: int
738 r1 :: bit
739 L0:
740 r0 = CPyTagged_Remainder(val, 4)
741 r1 = int_eq r0, 0
742 return r1
743 def test():
744 r0, source :: tuple[int, int, int]
745 r1, r2, r3 :: int
746 r4, r5, r6 :: object
747 r7, r8 :: tuple
748 r9 :: native_int
749 r10 :: bit
750 r11 :: object
751 r12, x :: int
752 r13 :: bool
753 r14 :: object
754 r15 :: native_int
755 a :: tuple
756 L0:
757 r0 = (2, 4, 6)
758 source = r0
759 r1 = source[0]
760 r2 = source[1]
761 r3 = source[2]
762 r4 = box(int, r1)
763 r5 = box(int, r2)
764 r6 = box(int, r3)
765 r7 = PyTuple_Pack(3, r4, r5, r6)
766 r8 = PyTuple_New(3)
767 r9 = 0
768 goto L2
769 L1:
770 r10 = r9 < 3 :: signed
771 if r10 goto L2 else goto L4 :: bool
772 L2:
773 r11 = CPySequenceTuple_GetItemUnsafe(r7, r9)
774 r12 = unbox(int, r11)
775 x = r12
776 r13 = f(x)
777 r14 = box(bool, r13)
778 CPySequenceTuple_SetItemUnsafe(r8, r9, r14)
779 L3:
780 r15 = r9 + 1
781 r9 = r15
782 goto L1
783 L4:
784 a = r8
785 return 1
786
787 [case testTupleBuiltFromFinalFixedLengthTuple]
788 from typing import Final
789
790 source: Final = (1, 2, 3)
791
792 def f(val: int) -> bool:
793 return val % 2 == 0
794
795 def test() -> None:
796 a = tuple(f(x) for x in source)
797 [out]
798 def f(val):
799 val, r0 :: int
800 r1 :: bit
801 L0:
802 r0 = CPyTagged_Remainder(val, 4)
803 r1 = int_eq r0, 0
804 return r1
805 def test():
806 r0 :: tuple[int, int, int]
807 r1 :: bool
808 r2, r3, r4 :: int
809 r5, r6, r7 :: object
810 r8, r9 :: tuple
811 r10 :: native_int
812 r11 :: bit
813 r12 :: object
814 r13, x :: int
815 r14 :: bool
816 r15 :: object
817 r16 :: native_int
818 a :: tuple
819 L0:
820 r0 = __main__.source :: static
821 if is_error(r0) goto L1 else goto L2
822 L1:
823 r1 = raise NameError('value for final name "source" was not set')
824 unreachable
825 L2:
826 r2 = r0[0]
827 r3 = r0[1]
828 r4 = r0[2]
829 r5 = box(int, r2)
830 r6 = box(int, r3)
831 r7 = box(int, r4)
832 r8 = PyTuple_Pack(3, r5, r6, r7)
833 r9 = PyTuple_New(3)
834 r10 = 0
835 goto L4
836 L3:
837 r11 = r10 < 3 :: signed
838 if r11 goto L4 else goto L6 :: bool
839 L4:
840 r12 = CPySequenceTuple_GetItemUnsafe(r8, r10)
841 r13 = unbox(int, r12)
842 x = r13
843 r14 = f(x)
844 r15 = box(bool, r14)
845 CPySequenceTuple_SetItemUnsafe(r9, r10, r15)
846 L5:
847 r16 = r10 + 1
848 r10 = r16
849 goto L3
850 L6:
851 a = r9
852 return 1
853
854 [case testTupleBuiltFromVariableLengthTuple]
855 from typing import Tuple
856
857 def f(val: bool) -> bool:
858 return not val
859
860 def test(source: Tuple[bool, ...]) -> None:
861 a = tuple(f(x) for x in source)
862 [out]
863 def f(val):
864 val, r0 :: bool
865 L0:
866 r0 = val ^ 1
867 return r0
868 def test(source):
869 source :: tuple
870 r0 :: native_int
871 r1 :: tuple
872 r2 :: native_int
873 r3 :: bit
874 r4 :: object
875 r5, x, r6 :: bool
876 r7 :: object
877 r8 :: native_int
878 a :: tuple
879 L0:
880 r0 = var_object_size source
881 r1 = PyTuple_New(r0)
882 r2 = 0
883 L1:
884 r3 = r2 < r0 :: signed
885 if r3 goto L2 else goto L4 :: bool
886 L2:
887 r4 = CPySequenceTuple_GetItemUnsafe(source, r2)
888 r5 = unbox(bool, r4)
889 x = r5
890 r6 = f(x)
891 r7 = box(bool, r6)
892 CPySequenceTuple_SetItemUnsafe(r1, r2, r7)
893 L3:
894 r8 = r2 + 1
895 r2 = r8
896 goto L1
897 L4:
898 a = r1
899 return 1
900
901 [case testTupleBuiltFromLengthCheckable]
902 from typing import Tuple
903
904 def f(val: bool) -> bool:
905 return not val
906
907 def test() -> None:
908 # this tuple is created from a very complex genexp but we can still compute the length and preallocate the tuple
909 a = tuple(
910 x
911 for x
912 in zip(
913 map(str, range(5)),
914 enumerate(sorted(reversed(tuple("abcdefg"))))
915 )
916 )
917 [out]
918 def f(val):
919 val, r0 :: bool
920 L0:
921 r0 = val ^ 1
922 return r0
923 def test():
924 r0 :: list
925 r1, r2, r3 :: object
926 r4 :: object[1]
927 r5 :: object_ptr
928 r6 :: object
929 r7 :: range
930 r8 :: object
931 r9 :: str
932 r10 :: object
933 r11 :: object[2]
934 r12 :: object_ptr
935 r13 :: object
936 r14 :: str
937 r15 :: tuple
938 r16 :: object
939 r17 :: str
940 r18 :: object
941 r19 :: object[1]
942 r20 :: object_ptr
943 r21 :: object
944 r22 :: list
945 r23 :: object
946 r24 :: str
947 r25 :: object
948 r26 :: object[1]
949 r27 :: object_ptr
950 r28, r29 :: object
951 r30 :: str
952 r31 :: object
953 r32 :: object[2]
954 r33 :: object_ptr
955 r34, r35, r36 :: object
956 r37, x :: tuple[str, tuple[int, str]]
957 r38 :: object
958 r39 :: i32
959 r40, r41 :: bit
960 r42, a :: tuple
961 L0:
962 r0 = PyList_New(0)
963 r1 = load_address PyUnicode_Type
964 r2 = load_address PyRange_Type
965 r3 = object 5
966 r4 = [r3]
967 r5 = load_address r4
968 r6 = PyObject_Vectorcall(r2, r5, 1, 0)
969 keep_alive r3
970 r7 = cast(range, r6)
971 r8 = builtins :: module
972 r9 = 'map'
973 r10 = CPyObject_GetAttr(r8, r9)
974 r11 = [r1, r7]
975 r12 = load_address r11
976 r13 = PyObject_Vectorcall(r10, r12, 2, 0)
977 keep_alive r1, r7
978 r14 = 'abcdefg'
979 r15 = PySequence_Tuple(r14)
980 r16 = builtins :: module
981 r17 = 'reversed'
982 r18 = CPyObject_GetAttr(r16, r17)
983 r19 = [r15]
984 r20 = load_address r19
985 r21 = PyObject_Vectorcall(r18, r20, 1, 0)
986 keep_alive r15
987 r22 = CPySequence_Sort(r21)
988 r23 = builtins :: module
989 r24 = 'enumerate'
990 r25 = CPyObject_GetAttr(r23, r24)
991 r26 = [r22]
992 r27 = load_address r26
993 r28 = PyObject_Vectorcall(r25, r27, 1, 0)
994 keep_alive r22
995 r29 = builtins :: module
996 r30 = 'zip'
997 r31 = CPyObject_GetAttr(r29, r30)
998 r32 = [r13, r28]
999 r33 = load_address r32
1000 r34 = PyObject_Vectorcall(r31, r33, 2, 0)
1001 keep_alive r13, r28
1002 r35 = PyObject_GetIter(r34)
1003 L1:
1004 r36 = PyIter_Next(r35)
1005 if is_error(r36) goto L4 else goto L2
1006 L2:
1007 r37 = unbox(tuple[str, tuple[int, str]], r36)
1008 x = r37
1009 r38 = box(tuple[str, tuple[int, str]], x)
1010 r39 = PyList_Append(r0, r38)
1011 r40 = r39 >= 0 :: signed
1012 L3:
1013 goto L1
1014 L4:
1015 r41 = CPy_NoErrOccurred()
1016 L5:
1017 r42 = PyList_AsTuple(r0)
1018 a = r42
1019 return 1
1020
1021 [case testTupleBuiltFromStars]
1022 from typing import Final
1023
1024 abc: Final = "abc"
1025
1026 def test() -> None:
1027 a = tuple(str(x) for x in [*abc, *"def", *b"ghi", ("j", "k"), *("l", "m", "n")])
1028
1029 [out]
1030 def test():
1031 r0, r1 :: str
1032 r2 :: bytes
1033 r3, r4 :: str
1034 r5 :: tuple[str, str]
1035 r6, r7, r8 :: str
1036 r9 :: tuple[str, str, str]
1037 r10 :: list
1038 r11, r12, r13, r14 :: object
1039 r15 :: i32
1040 r16 :: bit
1041 r17, r18 :: object
1042 r19 :: tuple
1043 r20, r21 :: native_int
1044 r22 :: bit
1045 r23, x :: object
1046 r24 :: str
1047 r25 :: native_int
1048 a :: tuple
1049 L0:
1050 r0 = 'abc'
1051 r1 = 'def'
1052 r2 = b'ghi'
1053 r3 = 'j'
1054 r4 = 'k'
1055 r5 = (r3, r4)
1056 r6 = 'l'
1057 r7 = 'm'
1058 r8 = 'n'
1059 r9 = (r6, r7, r8)
1060 r10 = PyList_New(0)
1061 r11 = CPyList_Extend(r10, r0)
1062 r12 = CPyList_Extend(r10, r1)
1063 r13 = CPyList_Extend(r10, r2)
1064 r14 = box(tuple[str, str], r5)
1065 r15 = PyList_Append(r10, r14)
1066 r16 = r15 >= 0 :: signed
1067 r17 = box(tuple[str, str, str], r9)
1068 r18 = CPyList_Extend(r10, r17)
1069 r19 = PyTuple_New(13)
1070 r20 = 0
1071 goto L2
1072 L1:
1073 r21 = var_object_size r10
1074 r22 = r20 < r21 :: signed
1075 if r22 goto L2 else goto L4 :: bool
1076 L2:
1077 r23 = list_get_item_unsafe r10, r20
1078 x = r23
1079 r24 = PyObject_Str(x)
1080 CPySequenceTuple_SetItemUnsafe(r19, r20, r24)
1081 L3:
1082 r25 = r20 + 1
1083 r20 = r25
1084 goto L1
1085 L4:
1086 a = r19
1087 return 1
1088
1089 [case testTupleAdd]
1090 from typing import Tuple
1091 def f(a: Tuple[int, ...], b: Tuple[int, ...]) -> None:
1092 c = a + b
1093 d = a + (1, 2)
1094 def g(a: Tuple[int, int], b: Tuple[int, int]) -> None:
1095 c = a + b
1096 [out]
1097 def f(a, b):
1098 a, b, r0, c :: tuple
1099 r1 :: tuple[int, int]
1100 r2 :: object
1101 r3, d :: tuple
1102 L0:
1103 r0 = PySequence_Concat(a, b)
1104 c = r0
1105 r1 = (2, 4)
1106 r2 = box(tuple[int, int], r1)
1107 r3 = PySequence_Concat(a, r2)
1108 d = r3
1109 return 1
1110 def g(a, b):
1111 a, b :: tuple[int, int]
1112 r0, r1 :: object
1113 r2 :: tuple
1114 r3, c :: tuple[int, int, int, int]
1115 L0:
1116 r0 = box(tuple[int, int], a)
1117 r1 = box(tuple[int, int], b)
1118 r2 = PySequence_Concat(r0, r1)
1119 r3 = unbox(tuple[int, int, int, int], r2)
1120 c = r3
1121 return 1
1122
1123 [case testTupleMultiply]
1124 from typing import Tuple
1125 def f(a: Tuple[int]) -> None:
1126 b = a * 2
1127 c = 3 * (2,)
1128 def g(a: Tuple[int, ...]) -> None:
1129 b = a * 2
1130 [out]
1131 def f(a):
1132 a :: tuple[int]
1133 r0 :: object
1134 r1 :: tuple
1135 r2, b :: tuple[int, int]
1136 r3 :: tuple[int]
1137 r4 :: object
1138 r5 :: tuple
1139 r6, c :: tuple[int, int, int]
1140 L0:
1141 r0 = box(tuple[int], a)
1142 r1 = CPySequence_Multiply(r0, 4)
1143 r2 = unbox(tuple[int, int], r1)
1144 b = r2
1145 r3 = (4)
1146 r4 = box(tuple[int], r3)
1147 r5 = CPySequence_RMultiply(6, r4)
1148 r6 = unbox(tuple[int, int, int], r5)
1149 c = r6
1150 return 1
1151 def g(a):
1152 a, r0, b :: tuple
1153 L0:
1154 r0 = CPySequence_Multiply(a, 4)
1155 b = r0
1156 return 1
1157
1158 [case testTupleFloatElementComparison]
1159 def f(x: tuple[float], y: tuple[float]) -> bool:
1160 return x == y
1161
1162 [out]
1163 def f(x, y):
1164 x, y :: tuple[float]
1165 r0, r1 :: float
1166 r2 :: bit
1167 r3 :: bool
1168 L0:
1169 r0 = x[0]
1170 r1 = y[0]
1171 r2 = r0 == r1
1172 if not r2 goto L1 else goto L2 :: bool
1173 L1:
1174 r3 = 0
1175 goto L3
1176 L2:
1177 r3 = 1
1178 L3:
1179 return r3
1180
1181 [case testTupleTypeAliasFromGenerator]
1182 X = tuple[str, ...]
1183
1184 def f() -> tuple[str, ...]:
1185 return X(str(x) for x in range(5))
1186
1187 def f2() -> tuple[str, ...]:
1188 return tuple(str(x) for x in range(5))
1189 [out]
1190 def f():
1191 r0 :: list
1192 r1 :: short_int
1193 x :: int
1194 r2 :: bit
1195 r3 :: str
1196 r4 :: i32
1197 r5 :: bit
1198 r6 :: short_int
1199 r7 :: tuple
1200 L0:
1201 r0 = PyList_New(0)
1202 r1 = 0
1203 x = r1
1204 L1:
1205 r2 = int_lt r1, 10
1206 if r2 goto L2 else goto L4 :: bool
1207 L2:
1208 x = r1
1209 r3 = CPyTagged_Str(x)
1210 r4 = PyList_Append(r0, r3)
1211 r5 = r4 >= 0 :: signed
1212 L3:
1213 r6 = r1 + 2
1214 r1 = r6
1215 goto L1
1216 L4:
1217 r7 = PyList_AsTuple(r0)
1218 return r7
1219 def f2():
1220 r0 :: list
1221 r1 :: short_int
1222 x :: int
1223 r2 :: bit
1224 r3 :: str
1225 r4 :: i32
1226 r5 :: bit
1227 r6 :: short_int
1228 r7 :: tuple
1229 L0:
1230 r0 = PyList_New(0)
1231 r1 = 0
1232 x = r1
1233 L1:
1234 r2 = int_lt r1, 10
1235 if r2 goto L2 else goto L4 :: bool
1236 L2:
1237 x = r1
1238 r3 = CPyTagged_Str(x)
1239 r4 = PyList_Append(r0, r3)
1240 r5 = r4 >= 0 :: signed
1241 L3:
1242 r6 = r1 + 2
1243 r1 = r6
1244 goto L1
1245 L4:
1246 r7 = PyList_AsTuple(r0)
1247 return r7