File: test_compiler.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (3487 lines) | stat: -rw-r--r-- 112,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
from __future__ import division
import py, sys
from pytest import raises
import pytest
from pypy.interpreter.astcompiler import codegen, symtable, optimize
from pypy.interpreter.pyparser import pyparse
from pypy.interpreter.pyparser.test import expressions
from pypy.interpreter.pycode import PyCode
from pypy.interpreter.pyparser.error import SyntaxError, IndentationError
from pypy.interpreter.error import OperationError
from pypy.tool import stdlib_opcode as ops

def compile_with_astcompiler(expr, mode, space, set_debug_flag=False):
    p = pyparse.PegParser(space)
    info = pyparse.CompileInfo("<test>", mode)
    ast = p.parse_source(expr, info)
    mod = optimize.optimize_ast(space, ast, info)
    return codegen.compile_ast(space, mod, info, set_debug_flag)

def generate_function_code(expr, space):
    from pypy.interpreter.astcompiler.ast import FunctionDef
    p = pyparse.PegParser(space)
    info = pyparse.CompileInfo("<test>", 'exec')
    ast = p.parse_source(expr, info)
    function_ast = optimize.optimize_ast(space, ast.body[0], info)
    function_ast = ast.body[0]
    assert isinstance(function_ast, FunctionDef)
    symbols = symtable.SymtableBuilder(space, ast, info)
    generator = codegen.FunctionCodeGenerator(
        space, 'function', function_ast, 1, symbols, info, qualname='function')
    blocks, size = generator._finalize_blocks()
    return generator, blocks

class BaseTestCompiler:
    """These tests compile snippets of code and check them by
    running them with our own interpreter.  These are thus not
    completely *unit* tests, but given that our interpreter is
    pretty stable now it is the best way I could find to check
    the compiler.
    """

    def run(self, source):
        import sys
        source = str(py.code.Source(source))
        space = self.space
        code = compile_with_astcompiler(source, 'exec', space)
        # 3.2 bytecode is too different, the standard `dis` module crashes
        # on older cpython versions
        if sys.version_info >= (3, 2):
            # this will only (maybe) work in the far future, when we run pypy
            # on top of Python 3. For now, it's just disabled
            print
            code.dump()
        w_dict = space.newdict()
        code.exec_code(space, w_dict, w_dict)
        return w_dict

    # on Python3 some reprs are different than Python2. Here is a collection
    # of how the repr should be on on Python3 for some objects
    PY3_REPR = {
        int: "<class 'int'>",
        float: "<class 'float'>",
        }

    def get_py3_repr(self, val):
        try:
            return self.PY3_REPR.get(val, repr(val))
        except TypeError:
            # e.g., for unhashable types
            return repr(val)

    def check(self, w_dict, evalexpr, expected):
        # for now, we compile evalexpr with CPython's compiler but run
        # it with our own interpreter to extract the data from w_dict
        space = self.space
        pyco_expr = space.createnewcompiler().compile(evalexpr, '<evalexpr>', 'eval', 0)
        w_res = space.exec_(pyco_expr, w_dict, w_dict)
        res = space.text_w(space.repr(w_res))
        expected_repr = self.get_py3_repr(expected)
        if isinstance(expected, float):
            # Float representation can vary a bit between interpreter
            # versions, compare the numbers instead.
            assert eval(res) == expected
        elif isinstance(expected, long):
            assert expected_repr.endswith('L')
            assert res == expected_repr[:-1] # in py3 we don't have the L suffix
        else:
            assert res == expected_repr

    def simple_test(self, source, evalexpr, expected):
        w_g = self.run(source)
        self.check(w_g, evalexpr, expected)

    st = simple_test

    def error_test(self, source, exc_type, msg_part=""):
        excinfo = py.test.raises(exc_type, self.simple_test, source, None, None)
        assert msg_part in excinfo.value.msg
        return excinfo.value


class TestCompiler(BaseTestCompiler):

    def test_issue_713(self):
        func = "def f(_=2): return (_ if _ else _) if False else _"
        yield self.st, func, "f()", 2

    def test_return(self):
        for input in ("class x: return", "return"):
            yield self.error_test, input, SyntaxError, "return outside function"

    def test_long_jump(self):
        func = """def f(x):
    y = 0
    if x:
%s        return 1
    else:
        return 0""" % ("        y += 1\n" * 6700,)
        yield self.st, func, "f(1)", 1
        yield self.st, func, "f(0)", 0

    def test_argtuple(self):
        yield (self.error_test, "def f( x, (y,z) ): return x,y,z",
               SyntaxError)
        yield (self.error_test, "def f( x, (y,(z,t)) ): return x,y,z,t",
               SyntaxError)
        yield (self.error_test, "def f(((((x,),y),z),t),u): return x,y,z,t,u",
               SyntaxError)

    def test_constants(self):
        for c in expressions.constants:
            yield (self.simple_test, "x="+c, "x", eval(c))

    def test_literals_with_leading_zeros(self):
        # adapted from similar test in lib-python/3/test/test_compiler.py
        for arg in ["077787", "0xj", "0x.", "0e",  "090000000000000",
                    "080000000000000", "000000000000009", "000000000000008",
                    "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
                    "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777",
                    "000777", "000000000000007"]:
            yield(self.error_test, "x=%s" % arg, SyntaxError)
        yield(self.simple_test, "x=0xff", "x", 255)
        yield(self.simple_test, "x=0777.", "x", 777.0)
        yield(self.simple_test, "x=0777.0", "x", 777.0)
        yield(self.simple_test, "x=000000000000000000000000000000000000000000000000000777e0", "x", 777.0)
        yield(self.simple_test, "x=0777e1", "x", 7770.0)
        yield(self.simple_test, "x=0e0", "x", 0.0)
        yield(self.simple_test, "x=0000e-012", "x", 0.0)
        yield(self.simple_test, "x=09.5", "x", 9.5)
        yield(self.simple_test, "x=0777j", "x", 777j)
        yield(self.simple_test, "x=000", "x", 0)
        yield(self.simple_test, "x=00j", "x", 0j)
        yield(self.simple_test, "x=00.0", "x", 0.0)
        yield(self.simple_test, "x=0e3", "x", 0.0)
        yield(self.simple_test, "x=090000000000000.", "x", 90000000000000.)
        yield(self.simple_test, "x=090000000000000.0000000000000000000000", "x", 90000000000000.)
        yield(self.simple_test, "x=090000000000000e0", "x", 90000000000000.)
        yield(self.simple_test, "x=090000000000000e-0", "x", 90000000000000.)
        yield(self.simple_test, "x=090000000000000j", "x", 90000000000000j)
        yield(self.simple_test, "x=000000000000008.", "x", 8.)
        yield(self.simple_test, "x=000000000000009.", "x", 9.)
        yield(self.simple_test, "x=0b101010", "x", 42)
        yield(self.simple_test, "x=-0b000000000010", "x", -2)
        yield(self.simple_test, "x=0o777", "x", 511)
        yield(self.simple_test, "x=-0o0000010", "x", -8)
        yield (self.simple_test, "x=0_0_0", "x", 0)
        yield (self.simple_test, "x=0b_1_1", "x", 3)
        yield (self.simple_test, "x=0o_1_1", "x", 9)
        yield (self.simple_test, "x=0X_1_1", "x", 17)


    def test_int_limit(self):
        yield (self.simple_test, "x=0E0", "x", 0.0)
        from pypy.module.sys.system import DEFAULT_MAX_STR_DIGITS
        max_str_digits = DEFAULT_MAX_STR_DIGITS
        yield(self.error_test, "x=%s" % ('1' * (max_str_digits + 1)), SyntaxError)

    def test_const_underscore(self):
        yield (self.simple_test, "x=0xffff_ffff_ff20_0000", "x", 0xffffffffff200000)

    def test_neg_sys_maxint(self):
        import sys
        stmt = "x = %s" % (-sys.maxint-1)
        self.simple_test(stmt, "type(x)", int)

    def test_tuple_assign(self):
        yield self.simple_test, "() = []", "1", 1
        yield self.simple_test, "x,= 1,", "x", 1
        yield self.simple_test, "x,y = 1,2", "x,y", (1, 2)
        yield self.simple_test, "x,y,z = 1,2,3", "x,y,z", (1, 2, 3)
        yield self.simple_test, "x,y,z,t = 1,2,3,4", "x,y,z,t", (1, 2, 3, 4)
        yield self.simple_test, "x,y,x,t = 1,2,3,4", "x,y,t", (3, 2, 4)
        yield self.simple_test, "[] = []", "1", 1
        yield self.simple_test, "[x]= 1,", "x", 1
        yield self.simple_test, "[x,y] = [1,2]", "x,y", (1, 2)
        yield self.simple_test, "[x,y,z] = 1,2,3", "x,y,z", (1, 2, 3)
        yield self.simple_test, "[x,y,z,t] = [1,2,3,4]", "x,y,z,t", (1, 2, 3,4)
        yield self.simple_test, "[x,y,x,t] = 1,2,3,4", "x,y,t", (3, 2, 4)

    def test_tuple_assign_order(self):
        decl = py.code.Source("""
            class A:
                def __getattr__(self, name):
                    global seen
                    seen += name
                    return name
                def __setattr__(self, name, value):
                    global seen
                    seen += '%s=%s' % (name, value)
            seen = ''
            a = A()
        """)
        decl = str(decl) + '\n'
        yield self.st, decl+"a.x,= a.a,", 'seen', 'ax=a'
        yield self.st, decl+"a.x,a.y = a.a,a.b", 'seen', 'abx=ay=b'
        yield self.st, decl+"a.x,a.y,a.z = a.a,a.b,a.c", 'seen', 'abcx=ay=bz=c'
        yield self.st, decl+"a.x,a.y,a.x,a.t = a.a,a.b,a.c,a.d", 'seen', \
            'abcdx=ay=bx=ct=d'
        yield self.st, decl+"[a.x] = [a.a]", 'seen', 'ax=a'
        yield self.st, decl+"[a.x,a.y] = a.a,a.b", 'seen', 'abx=ay=b'
        yield self.st, decl+"[a.x,a.y,a.z] = [a.a,a.b,a.c]", 'seen', \
            'abcx=ay=bz=c'
        yield self.st, decl+"[a.x,a.y,a.x,a.t] = a.a,a.b,a.c,a.d", 'seen', \
            'abcdx=ay=bx=ct=d'

    def test_binary_operator(self):
        for operator in ['+', '-', '*', '**', '/', '&', '|', '^', '//',
                         '<<', '>>', 'and', 'or', '<', '>', '<=', '>=',
                         'is', 'is not']:
            expected = eval("17 %s 5" % operator)
            yield self.simple_test, "x = 17 %s 5" % operator, "x", expected
            expected = eval("0 %s 11" % operator)
            yield self.simple_test, "x = 0 %s 11" % operator, "x", expected

    def test_compare(self):
        yield self.st, "x = 2; y = 5; y; h = 1 < x >= 3 < x", "h", False

    def test_augmented_assignment(self):
        for operator in ['+', '-', '*', '**', '/', '&', '|', '^', '//',
                         '<<', '>>']:
            expected = eval("17 %s 5" % operator)
            yield self.simple_test, "x = 17; x %s= 5" % operator, "x", expected

    def test_subscript(self):
        yield self.simple_test, "d={2:3}; x=d[2]", "x", 3
        yield self.simple_test, "d={(2,):3}; x=d[2,]", "x", 3
        yield self.simple_test, "d={}; d[1]=len(d); x=d[len(d)]", "x", 0
        yield self.simple_test, "d={}; d[1]=3; del d[1]", "len(d)", 0

    def test_attribute(self):
        yield self.simple_test, """
            class A:
                pass
            a1 = A()
            a2 = A()
            a1.bc = A()
            a1.bc.de = a2
            a2.see = 4
            a1.bc.de.see += 3
            x = a1.bc.de.see
        """, 'x', 7

    def test_slice(self):
        decl = py.code.Source("""
            class A(object):
                def __getitem__(self, x):
                    global got
                    got = x
                def __setitem__(self, x, y):
                    global set
                    set = x
                def __delitem__(self, x):
                    global deleted
                    deleted = x
            a = A()
        """)
        decl = str(decl) + '\n'
        testcases = ['[:]',    '[:,9]',    '[8,:]',
                     '[2:]',   '[2:,9]',   '[8,2:]',
                     '[:2]',   '[:2,9]',   '[8,:2]',
                     '[4:7]',  '[4:7,9]',  '[8,4:7]',
                     '[::]',   '[::,9]',   '[8,::]',
                     '[2::]',  '[2::,9]',  '[8,2::]',
                     '[:2:]',  '[:2:,9]',  '[8,:2:]',
                     '[4:7:]', '[4:7:,9]', '[8,4:7:]',
                     '[::3]',  '[::3,9]',  '[8,::3]',
                     '[2::3]', '[2::3,9]', '[8,2::3]',
                     '[:2:3]', '[:2:3,9]', '[8,:2:3]',
                     '[4:7:3]','[4:7:3,9]','[8,4:7:3]',
                     ]
        class Checker(object):
            def __getitem__(self, x):
                self.got = x
        checker = Checker()
        for testcase in testcases:
            exec "checker" + testcase
            yield self.st, decl + "a" + testcase, "got", checker.got
            yield self.st, decl + "a" + testcase + ' = 5', "set", checker.got
            yield self.st, decl + "del a" + testcase, "deleted", checker.got

    def test_funccalls(self):
        decl = py.code.Source("""
            def f(*args, **kwds):
                kwds = sorted(kwds.items())
                return list(args) + kwds
        """)
        decl = str(decl) + '\n'
        yield self.st, decl + "x=f()", "x", []
        yield self.st, decl + "x=f(5)", "x", [5]
        yield self.st, decl + "x=f(5, 6, 7, 8)", "x", [5, 6, 7, 8]
        yield self.st, decl + "x=f(a=2, b=5)", "x", [('a',2), ('b',5)]
        yield self.st, decl + "x=f(5, b=2, *[6,7])", "x", [5, 6, 7, ('b', 2)]
        yield self.st, decl + "x=f(5, b=2, **{'a': 8})", "x", [5, ('a', 8),
                                                                  ('b', 2)]

    def test_funccalls_all_combinations(self):
        decl = """
def f(*args, **kwds):
    kwds = sorted(kwds.items())
    return list(args) + kwds

class A:
    def f(self, *args, **kwds):
        kwds = sorted(kwds.items())
        return ["meth"] + list(args) + kwds
a = A()
"""
        allres = []
        allcalls = []
        for meth in [False, True]:
            for starstarargs in [
                    [],
                    [[('x', 1), ('y', 12)]],
                    [[('w1', 1), ('w2', 12)], [('x1', 1), ('x2', -12)], [('y1', 10), ('y2', 123)]]
                    ]:
                for starargs in [[], [(2, 3)], [(2, 3), (4, 19), (23, 54, 123)]]:
                    for kwargs in [[], [('m', 1)], [('n', 1), ('o', 2), ('p', 3)]]:
                        for args in [(), (1, ), (1, 4, 5)]:
                            if not meth:
                                call = "f("
                                res = []
                            else:
                                call = "a.f("
                                res = ["meth"]
                            if args:
                                call += ", ".join(str(arg) for arg in args) + ","
                                res.extend(args)
                            if starargs:
                                for stararg in starargs:
                                    call += "*" + str(stararg) + ","
                                    res.extend(stararg)
                            if kwargs:
                                call += ", ".join("%s=%s" % (kw, arg) for (kw, arg) in kwargs) + ", "
                                res.extend(kwargs)
                            if starstarargs:
                                for starstar in starstarargs:
                                    call += "**dict(%s)" % starstar + ","
                                res.extend(sum(starstarargs, []))
                            call += ")"
                            allcalls.append(call)
                            allres.append(res)
                            print call
                            print res
        self.st(decl + "x=[" + "\n,".join(allcalls) + "]", "x", allres)


    def test_kwonly(self):
        decl = py.code.Source("""
            def f(a, *, b):
                return a, b
        """)
        decl = str(decl) + '\n'
        self.st(decl + "x=f(1, b=2)", "x", (1, 2))
        operr = py.test.raises(OperationError, 'self.st(decl + "x=f(1, 2)", "x", (1, 2))')
        assert operr.value.w_type is self.space.w_TypeError

    def test_listmakers(self):
        yield (self.st,
               "l = [(j, i) for j in range(10) for i in range(j)"
               + " if (i+j)%2 == 0 and i%3 == 0]",
               "l",
               [(2, 0), (4, 0), (5, 3), (6, 0),
                (7, 3), (8, 0), (8, 6), (9, 3)])

    def test_genexprs(self):
        yield (self.st,
               "l = list((j, i) for j in range(10) for i in range(j)"
               + " if (i+j)%2 == 0 and i%3 == 0)",
               "l",
               [(2, 0), (4, 0), (5, 3), (6, 0),
                (7, 3), (8, 0), (8, 6), (9, 3)])

    def test_comparisons(self):
        yield self.st, "x = 3 in {3: 5}", "x", True
        yield self.st, "x = 3 not in {3: 5}", "x", False
        yield self.st, "t = True; x = t is True", "x", True
        yield self.st, "t = True; x = t is False", "x", False
        yield self.st, "t = True; x = t is None", "x", False
        yield self.st, "n = None; x = n is True", "x", False
        yield self.st, "n = None; x = n is False", "x", False
        yield self.st, "n = None; x = n is None", "x", True
        yield self.st, "t = True; x = t is not True", "x", False
        yield self.st, "t = True; x = t is not False", "x", True
        yield self.st, "t = True; x = t is not None", "x", True
        yield self.st, "n = None; x = n is not True", "x", True
        yield self.st, "n = None; x = n is not False", "x", True
        yield self.st, "n = None; x = n is not None", "x", False

        yield self.st, "x = not (3 in {3: 5})", "x", False
        yield self.st, "x = not (3 not in {3: 5})", "x", True
        yield self.st, "t = True; x = not (t is True)", "x", False
        yield self.st, "t = True; x = not (t is False)", "x", True
        yield self.st, "t = True; x = not (t is None)", "x", True
        yield self.st, "n = None; x = not (n is True)", "x", True
        yield self.st, "n = None; x = not (n is False)", "x", True
        yield self.st, "n = None; x = not (n is None)", "x", False
        yield self.st, "t = True; x = not (t is not True)", "x", True
        yield self.st, "t = True; x = not (t is not False)", "x", False
        yield self.st, "t = True; x = not (t is not None)", "x", False
        yield self.st, "n = None; x = not (n is not True)", "x", False
        yield self.st, "n = None; x = not (n is not False)", "x", False
        yield self.st, "n = None; x = not (n is not None)", "x", True

    def test_multiexpr(self):
        yield self.st, "z = 2+3; x = y = z", "x,y,z", (5,5,5)

    def test_imports(self):
        import os
        yield self.st, "import sys", "sys.__name__", "sys"
        yield self.st, "import sys as y", "y.__name__", "sys"
        yield (self.st, "import sys, os",
               "sys.__name__, os.__name__", ("sys", "os"))
        yield (self.st, "import sys as x, os.path as y",
               "x.__name__, y.__name__", ("sys", os.path.__name__))
        yield self.st, 'import os.path', "os.path.__name__", os.path.__name__
        yield (self.st, 'import os.path, sys',
               "os.path.__name__, sys.__name__", (os.path.__name__, "sys"))
        yield (self.st, 'import sys, os.path as osp',
               "osp.__name__, sys.__name__", (os.path.__name__, "sys"))
        yield (self.st, 'import os.path as osp',
               "osp.__name__", os.path.__name__)
        yield (self.st, 'from os import path',
               "path.__name__", os.path.__name__)
        yield (self.st, 'from os import path, sep',
               "path.__name__, sep", (os.path.__name__, os.sep))
        yield (self.st, 'from os import path as p',
               "p.__name__", os.path.__name__)
        yield (self.st, 'from os import *',
               "path.__name__, sep", (os.path.__name__, os.sep))
        yield (self.st, '''
            class A(object):
                def m(self):
                    from __foo__.bar import x
            try:
                A().m()
            except ImportError as e:
                msg = str(e)
            ''', "msg", "No module named '__foo__'")

    def test_if_stmts(self):
        yield self.st, "a = 42\nif a > 10: a += 2", "a", 44
        yield self.st, "a=5\nif 0: a=7", "a", 5
        yield self.st, "a=5\nif 1: a=7", "a", 7
        yield self.st, "a=5\nif a and not not (a<10): a=7", "a", 7
        yield self.st, """
            lst = []
            for a in range(10):
                if a < 3:
                    a += 20
                elif a > 3 and a < 8:
                    a += 30
                else:
                    a += 40
                lst.append(a)
            """, "lst", [20, 21, 22, 43, 34, 35, 36, 37, 48, 49]
        yield self.st, """
            lst = []
            for a in range(10):
                b = (a & 7) ^ 1
                if a or 1 or b: lst.append('A')
                if a or 0 or b: lst.append('B')
                if a and 1 and b: lst.append('C')
                if a and 0 and b: lst.append('D')
                if not (a or 1 or b): lst.append('-A')
                if not (a or 0 or b): lst.append('-B')
                if not (a and 1 and b): lst.append('-C')
                if not (a and 0 and b): lst.append('-D')
                if (not a) or (not 1) or (not b): lst.append('A')
                if (not a) or (not 0) or (not b): lst.append('B')
                if (not a) and (not 1) and (not b): lst.append('C')
                if (not a) and (not 0) and (not b): lst.append('D')
            """, "lst", ['A', 'B', '-C', '-D', 'A', 'B', 'A', 'B', '-C',
                         '-D', 'A', 'B', 'A', 'B', 'C', '-D', 'B', 'A', 'B',
                         'C', '-D', 'B', 'A', 'B', 'C', '-D', 'B', 'A', 'B',
                         'C', '-D', 'B', 'A', 'B', 'C', '-D', 'B', 'A', 'B',
                         'C', '-D', 'B', 'A', 'B', 'C', '-D', 'B', 'A', 'B',
                         '-C', '-D', 'A', 'B']

    def test_docstrings(self):
        for source, expected in [
            ('''def foo(): return 1''',      None),
            ('''class foo: pass''',          None),
            ('''foo = lambda: 4''',          None),
            ('''foo = lambda: "foo"''',      None),
            ('''def foo(): 4''',             None),
            ('''class foo: "foo"''',         "foo"),
            ('''def foo():
                    """foo docstring"""
                    return 1
             ''',                            "foo docstring"),
            ('''def foo():
                    """foo docstring"""
                    a = 1
                    """bar"""
                    return a
             ''',                            "foo docstring"),
            ('''def foo():
                    """doc"""; assert 1
                    a=1
             ''',                            "doc"),
            ('''
                class Foo(object): pass
                foo = Foo()
                exec("'moduledoc'", foo.__dict__)
             ''',                            "moduledoc"),
            ('''def foo(): f"abc"''',        None),
            ]:
            yield self.simple_test, source, "foo.__doc__", expected

    def test_in(self):
        yield self.st, "n = 5; x = n in [3,4,5]", 'x', True
        yield self.st, "n = 5; x = n in [3,4,6]", 'x', False
        yield self.st, "n = 5; x = n in [3,4,n]", 'x', True
        yield self.st, "n = 5; x = n in [3,4,n+1]", 'x', False
        yield self.st, "n = 5; x = n in (3,4,5)", 'x', True
        yield self.st, "n = 5; x = n in (3,4,6)", 'x', False
        yield self.st, "n = 5; x = n in (3,4,n)", 'x', True
        yield self.st, "n = 5; x = n in (3,4,n+1)", 'x', False

    def test_for_loops(self):
        yield self.st, """
            total = 0
            for i in [2, 7, 5]:
                total += i
        """, 'total', 2 + 7 + 5
        yield self.st, """
            total = 0
            for i in (2, 7, 5):
                total += i
        """, 'total', 2 + 7 + 5
        yield self.st, """
            total = 0
            for i in [2, 7, total+5]:
                total += i
        """, 'total', 2 + 7 + 5
        yield self.st, "x = sum([n+2 for n in [6, 1, 2]])", 'x', 15
        yield self.st, "x = sum([n+2 for n in (6, 1, 2)])", 'x', 15
        yield self.st, "k=2; x = sum([n+2 for n in [6, 1, k]])", 'x', 15
        yield self.st, "k=2; x = sum([n+2 for n in (6, 1, k)])", 'x', 15
        yield self.st, "x = sum(n+2 for n in [6, 1, 2])", 'x', 15
        yield self.st, "x = sum(n+2 for n in (6, 1, 2))", 'x', 15
        yield self.st, "k=2; x = sum(n+2 for n in [6, 1, k])", 'x', 15
        yield self.st, "k=2; x = sum(n+2 for n in (6, 1, k))", 'x', 15

    def test_closure(self):
        decl = py.code.Source("""
            def make_adder(n):
                def add(m):
                    return n + m
                return add
        """)
        decl = str(decl) + "\n"
        yield self.st, decl + "x = make_adder(40)(2)", 'x', 42

        decl = py.code.Source("""
            def f(a, g, e, c):
                def b(n, d):
                    return (a, c, d, g, n)
                def f(b, a):
                    return (a, b, c, g)
                return (a, g, e, c, b, f)
            A, G, E, C, B, F = f(6, 2, 8, 5)
            A1, C1, D1, G1, N1 = B(7, 3)
            A2, B2, C2, G2 = F(1, 4)
        """)
        decl = str(decl) + "\n"
        yield self.st, decl, 'A,A1,A2,B2,C,C1,C2,D1,E,G,G1,G2,N1', \
                             (6,6 ,4 ,1 ,5,5 ,5 ,3 ,8,2,2 ,2 ,7 )

    def test_try_except(self):
        yield self.simple_test, """
        x = 42
        try:
            pass
        except:
            x = 0
        """, 'x', 42

    def test_try_except_finally(self):
        yield self.simple_test, """
            try:
                x = 5
                try:
                    if x > 2:
                        raise ValueError
                finally:
                    x += 1
            except ValueError:
                x *= 7
        """, 'x', 42

    def test_try_finally_bug(self):
        yield self.simple_test, """
        x = 0
        try:
            pass
        finally:
            x = 6
        print(None, None, None, None)
        x *= 7
        """, 'x', 42

    def test_with_stacksize_bug(self):
        compile_with_astcompiler("with a:\n  pass", 'exec', self.space)

    def test_with_bug(self):
        yield self.simple_test, """
        class ContextManager:
            def __enter__(self, *args):
                return self
            def __exit__(self, *args):
                pass

        x = 0
        with ContextManager():
            x = 6
        print(None, None, None, None)
        x *= 7
        """, 'x', 42

    def test_while_loop(self):
        yield self.simple_test, """
            comments = [42]
            comment = '# foo'
            while comment[:1] == '#':
                comments[:0] = [comment]
                comment = ''
        """, 'comments', ['# foo', 42]
        yield self.simple_test, """
             while 0:
                 pass
             else:
                 x = 1
        """, "x", 1

    def test_type_of_constants(self):
        yield self.simple_test, "x=[0, 0.]", 'type(x[1])', float
        yield self.simple_test, "x=[(1,0), (1,0.)]", 'type(x[1][1])', float
        yield self.simple_test, "x=['2?-', '2?-']", 'id(x[0])==id(x[1])', True

    def test_pprint(self):
        # a larger example that showed a bug with jumps
        # over more than 256 bytes
        decl = py.code.Source("""
            def _safe_repr(object, context, maxlevels, level):
                typ = type(object)
                if typ is str:
                    if 'locale' not in _sys.modules:
                        return repr(object), True, False
                    if "'" in object and '"' not in object:
                        closure = '"'
                        quotes = {'"': '\\"'}
                    else:
                        closure = "'"
                        quotes = {"'": "\\'"}
                    qget = quotes.get
                    sio = _StringIO()
                    write = sio.write
                    for char in object:
                        if char.isalpha():
                            write(char)
                        else:
                            write(qget(char, repr(char)[1:-1]))
                    return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False

                r = getattr(typ, "__repr__", None)
                if issubclass(typ, dict) and r is dict.__repr__:
                    if not object:
                        return "{}", True, False
                    objid = id(object)
                    if maxlevels and level > maxlevels:
                        return "{...}", False, objid in context
                    if objid in context:
                        return _recursion(object), False, True
                    context[objid] = 1
                    readable = True
                    recursive = False
                    components = []
                    append = components.append
                    level += 1
                    saferepr = _safe_repr
                    for k, v in object.items():
                        krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
                        vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
                        append("%s: %s" % (krepr, vrepr))
                        readable = readable and kreadable and vreadable
                        if krecur or vrecur:
                            recursive = True
                    del context[objid]
                    return "{%s}" % ', '.join(components), readable, recursive

                if (issubclass(typ, list) and r is list.__repr__) or \
                   (issubclass(typ, tuple) and r is tuple.__repr__):
                    if issubclass(typ, list):
                        if not object:
                            return "[]", True, False
                        format = "[%s]"
                    elif _len(object) == 1:
                        format = "(%s,)"
                    else:
                        if not object:
                            return "()", True, False
                        format = "(%s)"
                    objid = id(object)
                    if maxlevels and level > maxlevels:
                        return format % "...", False, objid in context
                    if objid in context:
                        return _recursion(object), False, True
                    context[objid] = 1
                    readable = True
                    recursive = False
                    components = []
                    append = components.append
                    level += 1
                    for o in object:
                        orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
                        append(orepr)
                        if not oreadable:
                            readable = False
                        if orecur:
                            recursive = True
                    del context[objid]
                    return format % ', '.join(components), readable, recursive

                rep = repr(object)
                return rep, (rep and not rep.startswith('<')), False
        """)
        decl = str(decl) + '\n'
        g = {}
        exec decl in g
        expected = g['_safe_repr']([5], {}, 3, 0)
        yield self.st, decl + 'x=_safe_repr([5], {}, 3, 0)', 'x', expected

    def test_mapping_test(self):
        decl = py.code.Source("""
            class X(object):
                reference = {1:2, "key1":"value1", "key2":(1,2,3)}
                key, value = reference.popitem()
                other = {key:value}
                key, value = reference.popitem()
                inmapping = {key:value}
                reference[key] = value
                def _empty_mapping(self):
                    return {}
                _full_mapping = dict
                def assertEqual(self, x, y):
                    assert x == y
                failUnlessRaises = staticmethod(raises)
                def assert_(self, x):
                    assert x
                def failIf(self, x):
                    assert not x

            def test_read(self):
                # Test for read only operations on mapping
                p = self._empty_mapping()
                p1 = dict(p) #workaround for singleton objects
                d = self._full_mapping(self.reference)
                if d is p:
                    p = p1
                #Indexing
                for key, value in self.reference.items():
                    self.assertEqual(d[key], value)
                knownkey = next(iter(self.other))
                self.failUnlessRaises(KeyError, lambda:d[knownkey])
                #len
                self.assertEqual(len(p), 0)
                self.assertEqual(len(d), len(self.reference))
                #has_key
                for k in self.reference:
                    self.assert_(k in d)
                for k in self.other:
                    self.failIf(k in d)
                #cmp
                self.assert_(p == p)
                self.assert_(d == d)
                self.failUnlessRaises(TypeError, lambda: p < d)
                self.failUnlessRaises(TypeError, lambda: d > p)
                #__non__zero__
                if p: self.fail("Empty mapping must compare to False")
                if not d: self.fail("Full mapping must compare to True")
                # keys(), items(), iterkeys() ...
                def check_iterandlist(iter, lst, ref):
                    self.assert_(hasattr(iter, '__next__'))
                    self.assert_(hasattr(iter, '__iter__'))
                    x = list(iter)
                    self.assert_(set(x)==set(lst)==set(ref))
                check_iterandlist(iter(d.keys()), d.keys(), self.reference.keys())
                check_iterandlist(iter(d), d.keys(), self.reference.keys())
                check_iterandlist(iter(d.values()), d.values(), self.reference.values())
                check_iterandlist(iter(d.items()), d.items(), self.reference.items())
                #get
                key, value = next(iter(d.items()))
                knownkey, knownvalue = next(iter(self.other.items()))
                self.assertEqual(d.get(key, knownvalue), value)
                self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
                self.failIf(knownkey in d)
                return 42
        """)
        decl = str(decl) + '\n'
        yield self.simple_test, decl + 'r = test_read(X())', 'r', 42

    def test_stack_depth_bug(self):
        decl = py.code.Source("""
        class A:
            def initialize(self):
                # install all the MultiMethods into the space instance
                if isinstance(mm, object):
                    def make_boundmethod(func=func):
                        def boundmethod(*args):
                            return func(self, *args)
        r = None
        """)
        decl = str(decl) + '\n'
        yield self.simple_test, decl, 'r', None

    def test_assert(self):
        decl = py.code.Source("""
        try:
            assert 0, 'hi'
        except AssertionError as e:
            msg = str(e)
        """)
        yield self.simple_test, decl, 'msg', 'hi'

    def test_indentation_error(self):
        source = py.code.Source("""
        x
         y
        """)
        try:
            self.simple_test(source, None, None)
        except IndentationError as e:
            assert e.msg == 'unexpected indent'
        else:
            raise Exception("DID NOT RAISE")

    def test_no_indent(self):
        source = py.code.Source("""
        def f():
        xxx
        """)
        try:
            self.simple_test(source, None, None)
        except IndentationError as e:
            assert e.msg == 'expected an indented block after function definition on line 2'
        else:
            raise Exception("DID NOT RAISE")

    def test_indent_error_filename(self):
        source = py.code.Source("""
        def f():
          x
         y
        """)
        try:
            self.simple_test(source, None, None)
        except IndentationError as e:
            assert e.filename == '<test>'
        else:
            raise Exception("DID NOT RAISE")

    def test_kwargs_last(self):
        py.test.raises(SyntaxError, self.simple_test, "int(base=10, '2')",
                       None, None)

    def test_starargs_after_starargs(self):
        #allowed since PEP 448 "Additional Unpacking Generalizations"
        source = py.code.Source("""
        def call(*arg):
            ret = []
            for i in arg:
                ret.append(i)
            return ret

        args = [4,5,6]
        res = call(*args, *args)
        """)
        self.simple_test(source, 'res', [4,5,6,4,5,6])

    def test_not_a_name(self):
        source = "call(a, b, c, 3=3)"
        py.test.raises(SyntaxError, self.simple_test, source, None, None)

    def test_assignment_to_call_func(self):
        source = "call(a, b, c) = 3"
        py.test.raises(SyntaxError, self.simple_test, source, None, None)

    def test_augassig_to_sequence(self):
        source = "a, b += 3"
        py.test.raises(SyntaxError, self.simple_test, source, None, None)

    def test_broken_setups(self):
        source = """if 1:
        try:
           break
        finally:
           pass
        """
        py.test.raises(SyntaxError, self.simple_test, source, None, None)

    def test_bare_except_not_last(self):
        source = """if 1:
        try:
           pass
        except:
            pass
        except ValueError:
            pass
        """
        with py.test.raises(SyntaxError):
            self.simple_test(source, None, None)

    def test_unpack_singletuple(self):
        source = """if 1:
        l = []
        for x, in [(1,), (2,)]:
            l.append(x)
        """
        self.simple_test(source, 'l', [1, 2])

    def test_unpack_wrong_stackeffect(self):
        source = """if 1:
        l = [1, 2]
        a, b = l
        a, b = l
        a, b = l
        a, b = l
        a, b = l
        a, b = l
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 2

    def test_stackeffect_bug3(self):
        source = """if 1:
        try: pass
        finally: pass
        try: pass
        finally: pass
        try: pass
        finally: pass
        try: pass
        finally: pass
        try: pass
        finally: pass
        try: pass
        finally: pass
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 2 # maybe should be 1

    def test_stackeffect_bug4(self):
        source = """if 1:
        with a: pass
        with a: pass
        with a: pass
        with a: pass
        with a: pass
        with a: pass
        with a: pass
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 4  # i.e. <= 7, there is no systematic leak

    def test_stackeffect_bug5(self):
        source = """if 1:
        a[:]; a[:]; a[:]; a[:]; a[:]; a[:]
        a[1:]; a[1:]; a[1:]; a[1:]; a[1:]; a[1:]
        a[:2]; a[:2]; a[:2]; a[:2]; a[:2]; a[:2]
        a[1:2]; a[1:2]; a[1:2]; a[1:2]; a[1:2]; a[1:2]
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 3

    def test_stackeffect_bug6(self):
        source = """if 1:
        {1}; {1}; {1}; {1}; {1}; {1}; {1}
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 1

    def test_stackeffect_bug7(self):
        source = '''def f():
            for i in a:
                return
        '''
        code = compile_with_astcompiler(source, 'exec', self.space)

    def test_stackeffect_try_except_precision(self):
        source = """if 1:
        try:
            g()
        except TypeError as e:
            h(i(e))
            if j:
                raise
        print(6)
        """
        code = compile_with_astcompiler(source, 'exec', self.space)
        assert code.co_stacksize == 4 # used to be 6, then 5

    def test_lambda(self):
        yield self.st, "y = lambda x: x", "y(4)", 4

    def test_backquote_repr(self):
        py.test.raises(SyntaxError, self.simple_test, "y = `0`", None, None)

    def test_deleting_attributes(self):
        test = """if 1:
        class X():
           x = 3
        del X.x
        try:
            X.x
        except AttributeError:
            pass
        else:
            raise AssertionError("attribute not removed")"""
        yield self.st, test, "X.__name__", "X"

    def test_nonlocal(self):
        test = """if 1:
        def f():
            y = 0
            def g(x):
                nonlocal y
                y = x + 1
            g(3)
            return y"""
        yield self.st, test, "f()", 4

    def test_nonlocal_from_arg(self):
        test = """if 1:
        def test1(x):
            def test2():
                nonlocal x
                def test3():
                    return x
                return test3()
            return test2()"""
        yield self.st, test, "test1(2)", 2

    def test_class_nonlocal_from_arg(self):
        test = """if 1:
        def f(x):
            class c:
                nonlocal x
                x += 1
                def get(self):
                    return x
            return c().get(), x"""
        yield self.st, test, "f(3)", (4, 4)

    @pytest.mark.xfail
    def test_nonlocal_class_nesting_bug(self):
        test = """\
def foo():
    var = 0
    class C:
        def wrapper():
            nonlocal var
            var = 1
        wrapper()
        nonlocal var
    return var
"""
        self.st(test, "foo()", 1)

    def test_lots_of_loops(self):
        source = "for x in y: pass\n" * 1000
        compile_with_astcompiler(source, 'exec', self.space)

    def test_assign_to_empty_list_1(self):
        source = """if 1:
        for i in range(5):
            del []
            [] = ()
            [] = []
            [] = [] = []
        ok = 1
        """
        self.simple_test(source, 'ok', 1)

    def test_assign_to_empty_list_2(self):
        source = """if 1:
        for i in range(5):
            try: [] = 1, 2, 3
            except ValueError: pass
            else: raise AssertionError
            try: [] = a = 1
            except TypeError: pass
            else: raise AssertionError
            try: [] = _ = iter(['foo'])
            except ValueError: pass
            else: raise AssertionError
            try: [], _ = iter(['foo']), 1
            except ValueError: pass
            else: raise AssertionError
        ok = 1
        """
        self.simple_test(source, 'ok', 1)

    @py.test.mark.parametrize('expr, result', [
        ("f1.__doc__", None),
        ("f2.__doc__", 'docstring'),
        ("f2()", 'docstring'),
        ("f3.__doc__", None),
        ("f3()", 'bar'),
        ("C1.__doc__", None),
        ("C2.__doc__", 'docstring'),
        ("C3.field", 'not docstring'),
        ("C4.field", 'docstring'),
        ("C4.__doc__", 'docstring'),
        ("C4.__doc__", 'docstring'),
        ("__doc__", None),])

    def test_remove_docstring(self, expr, result):
        source = '"module_docstring"\n' + """if 1:
        def f1():
            'docstring'
        def f2():
            'docstring'
            return 'docstring'
        def f3():
            'foo'
            return 'bar'
        class C1():
            'docstring'
        class C2():
            __doc__ = 'docstring'
        class C3():
            field = 'not docstring'
        class C4():
            'docstring'
            field = 'docstring'
        """
        code_w = compile_with_astcompiler(source, 'exec', self.space)
        code_w.remove_docstrings(self.space)
        dict_w = self.space.newdict();
        code_w.exec_code(self.space, dict_w, dict_w)
        self.check(dict_w, expr, result)

    def test_dont_fold_equal_code_objects(self):
        yield self.st, "f=lambda:1;g=lambda:1.0;x=g()", 'type(x)', float
        yield (self.st, "x=(lambda: (-0.0, 0.0), lambda: (0.0, -0.0))[1]()",
                        'repr(x)', '(0.0, -0.0)')

    def test_raise_from(self):
        test = """if 1:
        def f():
            try:
                raise TypeError() from ValueError()
            except TypeError as e:
                assert isinstance(e.__cause__, ValueError)
                return 42
        """
        yield self.st, test, "f()", 42
        test = """if 1:
        def f():
            try:
                raise TypeError from ValueError
            except TypeError as e:
                assert isinstance(e.__cause__, ValueError)
                return 42
        """
        yield self.st, test, "f()", 42
    # This line is needed for py.code to find the source.

    def test_extended_unpacking(self):
        func = """def f():
            (a, *b, c) = 1, 2, 3, 4, 5
            return a, b, c
        """
        yield self.st, func, "f()", (1, [2, 3, 4], 5)
        func = """def f():
            [a, *b, c] = 1, 2, 3, 4, 5
            return a, b, c
        """
        yield self.st, func, "f()", (1, [2, 3, 4], 5)
        func = """def f():
            *a, = [1, 2, 3]
            return a
        """
        yield self.st, func, "f()", [1, 2, 3]
        func = """def f():
            for a, *b, c in [(1, 2, 3, 4)]:
                return a, b, c
        """
        yield self.st, func, "f()", (1, [2, 3], 4)

    def test_unpacking_while_building(self):
        func = """def f():
            b = [4,5,6]
            a = (*b, 7)
            return a
        """
        yield self.st, func, "f()", (4, 5, 6, 7)

        func = """def f():
            b = [4,5,6]
            a = [*b, 7]
            return a
        """
        yield self.st, func, "f()", [4, 5, 6, 7]

        func = """def f():
            b = [4,]
            x, y = (*b, 7)
            return x
        """
        yield self.st, func, "f()", 4


    def test_extended_unpacking_fail(self):
        exc = py.test.raises(SyntaxError, self.simple_test, "*a, *b = [1, 2]",
                             None, None).value
        assert exc.msg == "multiple starred expressions in assignment"
        exc = py.test.raises(SyntaxError, self.simple_test,
                             "[*b, *c] = range(10)", None, None).value
        assert exc.msg == "multiple starred expressions in assignment"
        assert exc.offset == 6

        exc = py.test.raises(SyntaxError, self.simple_test, "for *a in x: pass",
                             None, None).value
        assert exc.msg == "starred assignment target must be in a list or tuple"

        s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
        exc = py.test.raises(SyntaxError, self.simple_test, s, None,
                             None).value
        assert exc.msg == "too many expressions in star-unpacking assignment"
        s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)"
        exc = py.test.raises(SyntaxError, self.simple_test, s, None,
                             None).value
        assert exc.msg == "too many expressions in star-unpacking assignment"

    def test_list_compr_or(self):
        yield self.st, 'x = list(d for d in [1] or [])', 'x', [1]
        yield self.st, 'y = [d for d in [1] or []]', 'y', [1]

    def test_yield_from(self):
        test = """if 1:
        def f():
            yield from range(3)
        def g():
            return list(f())
        """
        yield self.st, test, "g()", range(3)

    def test__class__global(self):
        source = """if 1:
        class X:
           global __class__
        """
        py.test.raises(SyntaxError, self.simple_test, source, None, None)
        # XXX this raises "'global __class__' inside a class statement
        # is not implemented in PyPy".  The reason it is not is that it
        # seems we need to refactor some things to implement it exactly
        # like CPython, and I seriously don't think there is a point
        #
        # Another case which so far works on CPython but not on PyPy:
        #class X:
        #    __class__ = 42
        #    def f(self):
        #        return __class__
        #assert X.__dict__['__class__'] == 42
        #assert X().f() is X

    def test_error_message_1(self):
        source = """if 1:
        async def f():
            {await a for a in b}
        """
        self.simple_test(source, "None", None)

    def test_await_in_nested(self):
        source = """if 1:
        async def foo():
            def bar():
                [i for i in await items]
        """
        e = py.test.raises(SyntaxError, self.simple_test, source, "None", None)

    def test_async_in_nested(self):
        source = """if 1:
        async def foo():
            def bar():
                [i async for i in items]
        """
        e = py.test.raises(SyntaxError, self.simple_test, source, "None", None)
        source = """if 1:
        async def foo():
            def bar():
                {i async for i in items}
        """
        e = py.test.raises(SyntaxError, self.simple_test, source, "None", None)
        source = """if 1:
        async def foo():
            def bar():
                {i: i+1 async for i in items}
        """
        e = py.test.raises(SyntaxError, self.simple_test, source, "None", None)
        source = """if 1:
        async def foo():
            def bar():
                (i async for i in items)
        """
        # ok!
        self.simple_test(source, "None", None)

    def test_not_async_function_error(self):
        source = """
async with x:
    pass
"""
        with py.test.raises(SyntaxError):
            self.simple_test(source, "None", None)

        source = """
async for i in x:
    pass
"""
        with py.test.raises(SyntaxError):
            self.simple_test(source, "None", None)

        source = """
def f():
    async with x:
        pass
"""
        with py.test.raises(SyntaxError):
            self.simple_test(source, "None", None)

        source = """
def f():
    async for i in x:
        pass
"""
        with py.test.raises(SyntaxError):
            self.simple_test(source, "None", None)

    def test_load_classderef(self):
        source = """if 1:
        def f():
            x = 42
            class X:
                locals()["x"] = 43
                y = x
            return X.y
        """
        yield self.st, source, "f()", 43

    def test_fstring(self):
        yield self.st, """x = 42; z = f'ab{x}cd'""", 'z', 'ab42cd'
        yield self.st, """z = f'{{'""", 'z', '{'
        yield self.st, """z = f'}}'""", 'z', '}'
        yield self.st, """z = f'x{{y'""", 'z', 'x{y'
        yield self.st, """z = f'x}}y'""", 'z', 'x}y'
        yield self.st, """z = f'{{{4*10}}}'""", 'z', '{40}'
        yield self.st, r"""z = fr'x={4*10}\n'""", 'z', 'x=40\\n'

        yield self.st, """x = 'hi'; z = f'{x}'""", 'z', 'hi'
        yield self.st, """x = 'hi'; z = f'{x!s}'""", 'z', 'hi'
        yield self.st, """x = 'hi'; z = f'{x!r}'""", 'z', "'hi'"
        yield self.st, """x = 'hi'; z = f'{x!a}'""", 'z', "'hi'"

        yield self.st, """x = 'hi'; z = f'''{\nx}'''""", 'z', 'hi'

        yield self.st, """x = 'hi'; z = f'{x:5}'""", 'z', 'hi   '
        yield self.st, """x = 42;   z = f'{x:5}'""", 'z', '   42'
        yield self.st, """x = 2; z = f'{5:{x:+1}0}'""", 'z', (' ' * 18 + '+5')

        yield self.st, """z=f'{"}"}'""", 'z', '}'

        yield self.st, """z=f'{f"{0}"*3}'""", 'z', '000'

    def test_fstring_debugging(self):
        yield self.st, """x = 1;z = f'T: {x = }'""", 'z', 'T: x = 1'

    def test_fstring_error(self):
        py.test.raises(SyntaxError, self.run, "f'{}'")
        py.test.raises(SyntaxError, self.run, "f'{   \t   }'")
        py.test.raises(SyntaxError, self.run, "f'{5#}'")
        py.test.raises(SyntaxError, self.run, "f'{5)#}'")
        py.test.raises(SyntaxError, self.run, "f'''{5)\n#}'''")
        py.test.raises(SyntaxError, self.run, "f'\\x'")

    def test_fstring_encoding(self):
        src = """# -*- coding: latin-1 -*-\nz=ord(f'{"\xd8"}')\n"""
        yield self.st, src, 'z', 0xd8
        src = """# -*- coding: utf-8 -*-\nz=ord(f'{"\xc3\x98"}')\n"""
        yield self.st, src, 'z', 0xd8

        src = """z=ord(f'\\xd8')"""
        yield self.st, src, 'z', 0xd8
        src = """z=ord(f'\\u00d8')"""
        yield self.st, src, 'z', 0xd8

        src = """# -*- coding: latin-1 -*-\nz=ord(f'\xd8')\n"""
        yield self.st, src, 'z', 0xd8
        src = """# -*- coding: utf-8 -*-\nz=ord(f'\xc3\x98')\n"""
        yield self.st, src, 'z', 0xd8

    def test_fstring_encoding_r(self):
        src = """# -*- coding: latin-1 -*-\nz=ord(fr'{"\xd8"}')\n"""
        yield self.st, src, 'z', 0xd8
        src = """# -*- coding: utf-8 -*-\nz=ord(rf'{"\xc3\x98"}')\n"""
        yield self.st, src, 'z', 0xd8

        src = """z=fr'\\xd8'"""
        yield self.st, src, 'z', "\\xd8"
        src = """z=rf'\\u00d8'"""
        yield self.st, src, 'z', "\\u00d8"

        src = """# -*- coding: latin-1 -*-\nz=ord(rf'\xd8')\n"""
        yield self.st, src, 'z', 0xd8
        src = """# -*- coding: utf-8 -*-\nz=ord(fr'\xc3\x98')\n"""
        yield self.st, src, 'z', 0xd8

    def test_fstring_bug(self):
        yield self.st, "count=5; x = f'time{\"s\" if count > 1 else \"\"}'", "x", "times"

    def test_func_defaults_lineno(self):
        # like CPython 3.6.9 (at least), check that '''def f(
        #            x = 5,
        #            y = 6,
        #            ):'''
        # generates the tuple (5, 6) as a constant for the defaults,
        # but with the lineno for the last item (here the 6).  There
        # is no lineno for the other items, of course, because the
        # complete tuple is loaded with just one LOAD_CONST.
        yield self.simple_test, """\
            def fdl():      # line 1
                def f(      # line 2
                    x = 5,  # line 3
                    y = 6   # line 4
                    ):      # line 5
                    pass    # line 6
            import dis
            co = fdl.__code__
            x = [y for (x, y) in dis.findlinestarts(co)]
        """, 'x', [4]

    def test_instruction_positions(self):
        yield self.simple_test, """\
            def function():
                return (
                    a +
                    b +
                       c * (
                        d + 2
                       )
                )

            co = function.__code__
            positions = co.co_positions()
        """, 'positions', [
            (3, 3, 8, 9),
            (4, 4, 8, 9),
            (3, 4, 8, 9),
            (5, 5, 11, 12),
            (6, 6, 12, 13),
            (6, 6, 16, 17),
            (6, 6, 12, 17),
            (5, 7, 11, 12),
            (3, 7, 8, 12),
            (2, 8, 4, 5)
        ]

    def test_many_args(self):
        args = ["a%i" % i for i in range(300)]
        argdef = ", ".join(args)
        res = "+".join(args)
        callargs = ", ".join(str(i) for i in range(300))

        source1 = """def f(%s):
            return %s
x = f(%s)
        """ % (argdef, res, callargs)
        source2 = """def f(%s):
            return %s
x = f(*(%s))
        """ % (argdef, res, callargs)

        yield self.simple_test, source1, 'x', sum(range(300))
        yield self.simple_test, source2, 'x', sum(range(300))

    def test_bug_crash_annotations(self):
        yield self.simple_test, """\
            def func():
                bar = None
                class Foo:
                    bar: int = 0  # removing type annotation make the error disappear
                    def get_bar(self):
                        return bar
        """, '1', 1

    def test_walrus_operator(self):
        yield (self.simple_test, "(x := 1)", "x", 1)
        yield (self.simple_test, "y = (x := 1) + 5", "x+y", 7)
        yield (self.simple_test, "len(foobar := [])", "foobar", [])

        yield (self.error_test, "(l[1] := 5)", SyntaxError)

        yield (self.simple_test, """\
def foo():
    [(y := x) for x in range(5)]
    return y
""", "foo()", 4)

        yield (self.simple_test, """\
def foo():
    global y
    [(y := x) for x in range(5)]
    return y
""", "foo() + y", 8)

        yield (self.simple_test, """\
[(y := x) for x in range(5)]
""", "y", 4)

        yield (self.error_test, """\
class A:
    [(y := x) for y in range(5)]""", SyntaxError)

        yield (self.error_test, "[(x := 5) for x in range(5)]", SyntaxError)

        yield (self.error_test, "[i for i in range(5) if (j := 0) for j in range(5)]", SyntaxError)

        yield (self.error_test, "[i for i in (i := range(5))]", SyntaxError)

    def test_walrus_operator_error_msg(self):
        with raises(SyntaxError) as info:
            self.simple_test("(() := 1)", None, None)
        assert info.value.msg == "cannot use assignment expressions with tuple"
        with raises(SyntaxError) as info:
            self.simple_test("((lambda : 1) := 1)", None, None)
        assert info.value.msg == "cannot use assignment expressions with lambda"

    def test_extended_unpacking_on_flow_statements(self):
        yield (self.simple_test, """\
def foo(*args):
    return 1, *args
""", "foo(2, 3)", (1, 2, 3))
        yield (self.simple_test, """\
def foo(*args):
    yield 1, *args
""", "next(foo(2, 3))", (1, 2, 3))

    def test_extended_unpacking_on_flow_statements_invalid(self):
        with raises(SyntaxError) as info:
            self.simple_test("""\
def foo(*args):
    yield from 1, *args
""", None, None)

    def test_dict_comprehension_evaluation_order(self):
        yield (self.simple_test, """\
def f():
    l = [1, 2, 3, 4, 5, 6]
    return {l.pop() : l.pop() for i in range(3)}
        """, "f()", {6: 5, 4: 3, 2: 1})

    def test_var_annot_rhs(self):
        yield (self.simple_test, "x: tuple = 1, 2", "x", (1, 2))
        yield (self.simple_test, """\
def f():
    x: int = yield 'hel'
    yield x

gen = f()
""", "next(gen) + gen.send('lo')", "hello")
        yield (self.simple_test, """\
rest = 2, 3
x: tuple = 1, *rest, 4
""", "x", (1, 2, 3, 4))

    def test_newbytecode_for_loop(self):
        func = """def f():
    res = 0
    for i in range(10):
        res += i
    return res
"""
        yield self.st, func, "f()", 45

    def test_newbytecode_for_loop_break(self):
        func = """def f():
    res = 0
    for i in range(10000):
        if i >= 10:
            break
        res += i
    return res
"""
        yield self.st, func, "f()", 45

    def test_newbytecode_for_loop_continue(self):
        func = """def f():
    res = 0
    for i in range(20):
        if i >= 10:
            continue
        res += i
    return res
"""
        yield self.st, func, "f()", 45

    def test_newbytecode_while_loop_break(self):
        func = """def f():
    res = 0
    i = 0
    while i < 10000:
        if i >= 10:
            break
        res += i
        i += 1
    return res
"""
        yield self.st, func, "f()", 45

    def test_newbytecode_for_loop_return(self):
        func = """def f():
    res = 0
    for i in range(10000):
        if i >= 10:
            return res
        res += i
"""
        yield self.st, func, "f()", 45

    def test_newbytecode_finally(self):
        func = """def f():
    global a
    try:
        return
    finally:
        a = 5

def g():
    f()
    return a
"""
        yield self.st, func, "g()", 5

    def test_newbytecode_finally_exception(self):
        func = """def f():
    global a
    try:
        raise ValueError
    finally:
        a = 5

def g():
    try:
        f()
    except Exception:
        pass
    return a
"""
        yield self.st, func, "g()", 5

    def test_newbytecode_break_in_except(self):
        func = """def g():
    res = 0
    for i in range(100):
        try:
            h(i)
        except ValueError:
            break
        res += i
    return res

def h(i):
    if i >= 10:
        raise ValueError
"""
        yield self.st, func, "g()", 45

    def test_newbytecode_break_in_except_named(self):
        func = """def g():
    res = 0
    for i in range(100):
        try:
            h(i)
        except ValueError as e:
            break
        res += i
    return res

def h(i):
    if i >= 10:
        raise ValueError
"""
        yield self.st, func, "g()", 45

    def test_newbytecode_return_in_except(self):
        func = """def g():
    res = 0
    for i in range(100):
        try:
            h(i)
        except ValueError:
            return res
        res += i

def h(i):
    if i >= 10:
        raise ValueError
"""
        yield self.st, func, "g()", 45

    def test_newbytecode_return_in_except_named(self):
        func = """def g():
    res = 0
    for i in range(100):
        try:
            h(i)
        except ValueError as e:
            return res
        res += i
    return res

def h(i):
    if i >= 10:
        raise ValueError
"""
        yield self.st, func, "g()", 45

    def test_newbytecode_return_in_except_body(self):
        func = """def g():
    res = 0
    for i in range(20):
        try:
            return i
        except:
            pass
    return res
"""
        yield self.st, func, "g()", 0

    def test_newbytecode_continue_in_try_finally(self):
        func = """def g():
    res = 0
    for i in range(20):
        try:
            continue
        finally:
            res += i
    return res
"""
        yield self.st, func, "g()", 190

    def test_newbytecode_continue_in_finally(self):
        func = """def g():
    res = 0
    for i in range(20):
        try:
            h(i)
        finally:
            res += i
            continue
    return res

def h(i):
    if i >= 10:
        raise ValueError
"""
        yield self.st, func, "g()", 190

    def test_newbytecode_blocktype_try2(self):
        func = """def g():
    res = 0
    for i in range(20):
        try:
            return res
        finally:
            res += i
            if i < 10:
                continue
    return res
"""
        yield self.st, func, "g()", 45

    def test_newbytecode_named_try_bug(self):
        func = """def g():
    try:
        raise StopIteration
    except StopIteration as e:
        assert 1
"""
        self.st(func, "g()", None)

    def test_newbytecode_with_basic(self):
        func = """def g():
        class ContextManager:
            def __enter__(self, *args):
                return self
            def __exit__(self, *args):
                pass

        x = 0
        with ContextManager():
            x = 6
        return x
"""
        self.st(func, "g()", 6)

    def test_newbytecode_with_return(self):
        func = """class ContextManager:
    def __enter__(self, *args):
        return self
    def __exit__(self, *args):
        pass

def g():
        with ContextManager():
            return 8
"""
        self.st(func, "g()", 8)

    def test_newbytecode_with_continue(self):
        func = """def g():
    class ContextManager:
        def __enter__(self, *args):
            return self
        def __exit__(self, typ, val, tb):
            nonlocal res
            res += i
    res = 0
    for i in range(20):
        with ContextManager() as b:
            continue
    return res
"""
        self.st(func, "g()", 190)

    def test_newbytecode_async_for_break(self):
        func = """def g():
    class X:
        def __aiter__(self):
            return MyAIter()

    class MyAIter:
        async def __anext__(self):
            return 42
    async def f(x):
        sum = 0
        async for a in x:
            sum += a
            if sum > 100:
                break
        return sum
    cr = f(X())
    try:
        cr.send(None)
    except StopIteration as e:
        return e.value
    else:
        assert False, "should have raised"
"""
        self.st(func, "g()", 3 * 42)

    def test_newbytecode_async_for(self):
        func = """def g():
    class X:
        def __aiter__(self):
            return MyAIter()
    class MyAIter:
        count = 0
        async def __anext__(self):
            if self.count == 3:
                raise StopAsyncIteration
            self.count += 1
            return 42
    async def f(x):
        sum = 0
        async for a in x:
            sum += a
        return sum
    cr = f(X())
    try:
        cr.send(None)
    except StopIteration as e:
        assert e.value == 42 * 3
    else:
        assert False, "should have raised"
"""
        self.st(func, "g()", None)

    def test_newbytecode_async_for_other_exception(self):
        func = """def g():
    class X:
        def __aiter__(self):
            return MyAIter()
    class MyAIter:
        count = 0
        async def __anext__(self):
            if self.count == 3:
                1/0
            self.count += 1
            return 42
    async def f(x):
        sum = 0
        async for a in x:
            sum += a
        return sum
    cr = f(X())
    try:
        cr.send(None)
    except ZeroDivisionError:
        pass
    else:
        assert False, "should have raised"
"""
        self.st(func, "g()", None)

    def test_newbytecode_async_genexpr(self):
        func = """def g():
    def run_async(coro):
        buffer = []
        result = None
        while True:
            try:
                buffer.append(coro.send(None))
            except StopIteration as ex:
                result = ex.args[0] if ex.args else None
                break
        return buffer, result

    async def f(it):
        for i in it:
            yield i

    async def run_gen():
        gen = (i + 1 async for i in f([10, 20]))
        return [g + 100 async for g in gen]

    assert run_async(run_gen()) == ([], [111, 121])
"""
        self.st(func, "g()", None)

    def test_newbytecode_async_with(self):
        func = """def g():
    seen = []
    class X:
        async def __aenter__(self):
            seen.append('aenter')
        async def __aexit__(self, *args):
            seen.append('aexit')
    async def f(x):
        async with x:
            return 42
    c = f(X())
    try:
        c.send(None)
    except StopIteration as e:
        assert e.value == 42
    else:
        assert False, "should have raised"
    assert seen == ['aenter', 'aexit']
"""
        self.st(func, "g()", None)

    def test_newbytecode_reraise_no_match(self):
        space = self.space
        space.raises_w(space.w_KeyError,
            space.appexec, [], r"""():
            try:
                {}[1]
            except TypeError:
                return 2
            return 1
        """)

    def test_newbytecode_reraise_finally(self):
        space = self.space
        space.raises_w(space.w_KeyError,
            space.appexec, [], r"""():
            try:
                raise KeyError
            finally:
                pass
            return 4
        """)

    def test_newbytecode_reraise_return(self):
        space = self.space
        w_res = space.appexec([], r"""():
            try:
                raise KeyError
            finally:
                x = 7
                return x + 1 # swallow exception
            return 4
        """)
        assert space.int_w(w_res) == 8

    def test_newbytecode_reraise_named_except_finally(self):
        space = self.space
        space.raises_w(space.w_KeyError,
            space.appexec, [], r"""():
            try:
                raise KeyError
            except KeyError as e:
                raise
            return 4
        """)

    def test_newbytecode_raise_in_except_bug(self):
        space = self.space
        w_res = space.appexec([], r"""():
            try:
                try:
                    raise KeyError
                except TypeError:
                    for i in range(10):
                        pass
                    else:
                        raise KeyError
            except KeyError:
                return 10
            return 0""")
        assert space.int_w(w_res) == 10

    def test_newbytecode_syntaxerror_attrs(self):
        w_args = self.space.appexec([], r"""():
            try:
                exec('if 1:\n  x\n y\n')
            except SyntaxError as e:
                return e.args
        """)
        assert self.space.unwrap(w_args) == (
            'unindent does not match any outer indentation level',
            ('<string>', 3, 2, ' y\n', None, None)) # XXX this could be , 3, 2 as well

    def test_finally_lineno_wrong(self):
        func = """def f(x): # 1
    def f(func):
        return func
    return f

@f(1)
def finally_wrong_lineno():
    try: # 8
        return print(1) # 9
    finally:
        print(2) # 11
    print(3) # 12
import dis
co = finally_wrong_lineno.__code__
linestarts = list(dis.findlinestarts(co))
x = [lineno for addr, lineno in linestarts]
    """
        self.st(func, "x", [8, 9, 11])

    def test_lineno1_eval_bug(self):
        func = """c = compile('z', '<string>', 'eval')
import dis
x = [lineno for addr, lineno in dis.findlinestarts(c)]
"""
        self.st(func, "x", [1])

    def test_with_lineno_wrong(self):
        func = """def with_wrong_lineno():
    with ABC():
        g()
import dis
co = with_wrong_lineno.__code__
linestarts = list(dis.findlinestarts(co))
x = [lineno for addr, lineno in linestarts]
    """
        self.st(func, "x", [2, 3, 2])


    def test_error_in_dead_code(self):
        self.error_test("if 0: break", SyntaxError)
        self.error_test("while 0: lambda x, x: 1", SyntaxError)
        self.error_test("if 0:\n if 0:\n  [x async for x in b]", SyntaxError)
        self.error_test("[(i, j) for i in range(5) for j in range(5) if True or (i:=10)]", SyntaxError)

    def test_bug_lnotab(self):
        func = """
def buggy_lnotab():
    for i in x:







        1
x = [c for c in buggy_lnotab.__code__.co_lnotab]
"""
        self.st(func, "x", [0, 1, 8, 8, 2, 248])

    def test_lnotab_backwards_in_expr(self):
        func = """
def expr_lines(x):
    return (x +
        1)
x = [c for c in expr_lines.__code__.co_lnotab]
"""
        self.st(func, "x", [0, 1, 2, 1, 2, 255])

    def test_lineno_docstring_class(self):
        func = """
def expr_lines(x):
    class A:
        "abc"
x = [c for c in expr_lines.__code__.co_consts[1].co_lnotab]
"""
        self.st(func, "x", [8, 1])

    def test_lineno_funcdef(self):
        func = '''def f():
    @decorator
    def my_function(
        x=x
    ):
        pass
x = [c for c in f.__code__.co_lnotab]
'''
        self.st(func, 'x', [0, 1, 2, 2, 2, 255])


    def test_revdb_metavar(self):
        self.error_test("7 * $0", SyntaxError)

    def test_bug_arguments(self):
        func = """
def brokenargs(a=1, /, b=2, *, c):
    return [a, b, c]
x = brokenargs(c=3)
"""
        self.st(func, "x", [1, 2, 3])

    def test_keyword_repeated(self):
        yield self.error_test, "f(a=c, a=d)", SyntaxError, "keyword argument repeated: 'a'"
        yield self.error_test, "class A(metaclass=c, metaclass=d): pass", SyntaxError, "keyword argument repeated: 'metaclass'"

    def test_while_false_break(self):
        self.st("x=1\nwhile False: break", "x", 1)

    def test_cant_annotate_debug(self):
        self.error_test("__debug__ : int", SyntaxError, "cannot assign to __debug__")
        self.error_test("object.__debug__ : int", SyntaxError, "cannot assign to __debug__")

    def test_match(self):
        func = """
def f(x):
    class A:
        y = 13
    match x:
        case 1.1+2.2j: return "???"
        case True: return "True"
        case 1: return "hello"
        case 3 | 4: return "3 | 4"
        case [True]: return "[True]"
        case [1,2,3]: return "list[1,2,3]"
        case [1]: return "list[1]"
        case []: return "emptylist"
        case [_]: return "list"
        case True | False as b: return b
        case a if a: return a
        case A.y as z: return z

res=(
        f(1),
        f(2),
        f(['']),
        f([1]),
        f([1,2,3]),
        f([1,2,4]),
        f([1,3,4]),
        f([2,3,4]),
        f([1, 2]),
        f([]),
        f(True),
        f(3),
        f(0),
        f(False),
        f(13),
        f(1.1+2.2j),
        f([True]),
)
"""
        self.st(func, "res", (
            "hello",
            2,
            "list",
            "list[1]",
            "list[1,2,3]",
            [1,2,4],
            [1,3,4],
            [2,3,4],
            [1,2],
            "emptylist",
            "True",
            "3 | 4",
            None,
            False,
            13,
            '???',
            '[True]',
        ))

    def test_match_sequence_star(self):
        func = """
def f(x):
    match x:
        case [1, *rest, 4, 5]: return rest
        case [1, *rest]: return rest
        case [*rest, 5]: return rest
        case [*rest]: return rest
res=(
    f([1, 2, 3, 4, 5]),
    f([1, 2, 3]),
    f([3, 4, 5]),
    f([2, 3, 4]),
)
"""
        self.st(func, "res", (
            [2, 3],
            [2, 3],
            [3, 4],
            [2, 3, 4],
        ))

    def test_match_mapping(self):
        func = """
def f(x):
    match x:
        case {'x': 42, 'y': 13}: return "{'x': 42, 'y': 13}"
        case {'x': 13, **rest}: return rest
        case {'x': 7}: return "{'x': 7}"
        case {True: 7}: return "{True: 7}"
        case {}: return "{}"
        case _: return "_"
res=(
    f([]), # not a dict: _
    f({}), # empty: {}
    f({'y': 42}), # no match for key: {}
    f({'x': 21}), # no match for value: {}
    f({'x': 42, 'y': 7}), # no match for second member's value: {}
    f({'x': 42, 'y': 13}), # successful match with 2 members: {'x': 42, 'y': 13}
    f({'x': 42, 'y': 13, 'z': 0}), # successful match with 2 members extraneous property: {'x': 42, 'y': 13}
    f({'x': 7}), # successful match with a single member: {'x': 7}
    f({'x': 7, 'y': 13}), # successful match with a single member and extraneous property: {'x': 7}
    f({'x': 13, 'y': 7}), # successful match with rest capture: {'y': 7}
    f({True: 7})
)
"""
        self.st(func, "res", (
            "_",
            "{}",
            "{}",
            "{}",
            "{}",
            "{'x': 42, 'y': 13}",
            "{'x': 42, 'y': 13}",
            "{'x': 7}",
            "{'x': 7}",
            {'y': 7},
            "{True: 7}",
        ))

    def test_match_mapping_more(self):
        func = """
def f(x):
    match x:
        case {f.x: 42}: return "self-attribute"
        case _: return "_"
f.x = "u"

res=(
    f({"u": 42}),
)
"""
        self.st(func, "res", (
            "self-attribute",
        ))


    def test_match_class(self):
        func = """
class C:
    def __init__(self, x):
        self.x = x
C.__match_args__ = ('x',)

def f(x):
    match x:
        case C(x) if x is True: return "C(True)"
        case C(x=y) if y is False: return "C(False)"
        case C(x=z): return "C(x={})".format(z)
        case bool(b) if b: return "True"
        case bool(): return "False"
res=(
    f(True),
    f(False),
    f(C(True)),
    f(C(False)),
    f(C(None)),
)
"""
        self.st(func, "res", (
            "True",
            "False",
            "C(True)",
            "C(False)",
            "C(x=None)",
        ))

    def test_match_class_attribute_missing(self):
        func = """
class C: pass

def f(x):
    match x:
        case C(attr=y): return y
c1 = C()
c2 = C()
c2.attr = 12
res=(
    f(c1),
    f(c2),
)
"""
        self.st(func, "res", (
            None,
            12,
        ))

    def test_match_complex(self):
        func = """
class C: pass

def f(x):
    match x:
        case 0 + 0j:
            return 1
res=(
    f(0+0j),
    f(1+0j),
)
"""
        self.st(func, "res", (
            1,
            None,
        ))

    def test_match_errors(self):
        self.error_test("""
match x:
    case a: pass
    case 1: pass
""", SyntaxError, "name capture 'a' makes remaining patterns unreachable")

    def test_kwonly_crash(self):
        exc = self.error_test("def f(p1, *k1, k1=100): pass", SyntaxError)
        if exc:
            assert exc.offset == 12
            assert exc.end_offset == 14

    def test_unpack_stararg_msg(self):
        exc = self.error_test("(*abc) = 1", SyntaxError, "cannot")

    def test_intliteral_as_it_always_was(self):
        self.st("x = [0x1for x in [1, 2]]", "x", [31])

    def test_bug_comprehension_optimization(self):
        self.st("x = sorted({j*k for i in range(4) for j, k in [(i+1, i+2)]})", "x", [2, 6, 12, 20])
        self.st("x = sorted({j*k for i in range(4) for j in [i+1] for k in [j+1]})", "x", [2, 6, 12, 20])
        self.st("[*x] = (j*k for i in range(4) for j in [i+1] for k in [j+1])", "x", [2, 6, 12, 20])

    def test_type_with_star_311(self):
        self.st("def func1(*args: *(1, )): pass", "func1.__annotations__['args']", 1)


class TestLinenoChanges310(object):
    def get_line_numbers(self, source, expected, function=False):
        from pypy.tool.dis3 import findlinestarts
        space = self.space
        code = compile_with_astcompiler(source, 'exec', space, set_debug_flag=False)
        if function:
            code = code.co_consts[0]
        lines = [line for (start, line) in findlinestarts(code)]
        min_line = min(lines) - function
        got = [line - min_line for line in lines]
        # check that there are no two nops with the same lineno
        assert got == expected
        return code

    def test_linestarts_pass(self):
        code = self.get_line_numbers("""if x:
    pass; pass; pass; pass
else:
    pass; pass; pass; pass; pass; pass; pass
pass; pass; pass; pass; pass; pass; pass
pass; pass; pass; pass
pass""", [0, 1, 3, 4, 5, 6])
        assert len(code.co_code) == 16 # check that the NOPs have been reduced

    def test_while_1(self):
        code = self.get_line_numbers("""while 1:
    if f(x):
        pass
    else:
        pass
""", [0, 1, 2, 4])
        assert len(code.co_code) <= 20 # check that the NOPs have been reduced

    def test_duplicate_returns(self):
        code = self.get_line_numbers("""
if x:
    pass
else:
    pass
""", [0, 1, 3])

    def test_duplicate_reraise(self):
        code = self.get_line_numbers("""
try:
    g()
finally:
    if h():
        pass
    else:
        pass
""", [0, 1, 3, 4, 6, 3, 4, 6])

    def test_loop_return(self):
        code = self.get_line_numbers("""
x = 1               # 0
for a in range(2):  # 1
    if a:           # 2
        x = 1       # 3
    else:           # 4
        x = 1       # 5
""", [0, 1, 2, 3, 5, 1])

    def test_dont_propage_through_exception_handler(self):
        code = self.get_line_numbers("""
try:
    raise Exception("abc")
except Exception as e:
    if reraise:
        raise
    print("after raise")
""", [0, 1, 2, 3, 4, 5, 2])

    def test_nop_for_if0_if1(self):
        code = self.get_line_numbers("""
x = 1
while 0:
    pass
if 0:
    pass
if 1:
    pass
while 1:
    break
x = 2
""", [0, 1, 3, 5, 6, 7, 8, 9])

    def test_async_for(self):
        code = self.get_line_numbers("""
async def doit_async():
    async for letter in AsyncIteratorWrapper("abc"):
        x = letter
    y = 42
""", [1, 2, 1, 3], function=True)

    def test_break_in_finally(self):
        code = self.get_line_numbers("""
for i in range(3):
    try:
        break                   # line 7
    finally:
        f()
""", [0, 1, 2, 4, 0])

    def test_return_in_finally(self):
        code = self.get_line_numbers("""
def return_in_finally():
    try:
        return
    finally:
        f()
""", [1, 2, 4], function=True)

    def test_break_to_break(self):
        code = self.get_line_numbers("""
TRUE = 1
while TRUE:
    while TRUE:
        break
    break
""", [0, 1, 2, 3, 4, 1])

    def test_dead_code_shouldnt_impact_positions(self):
        code = self.get_line_numbers("""
def func():
    try:
        if False:
            pass
    except Exception:
        X
""", [1, 2, 4, 5, 4], function=True)

    def test_nested_if_confusion(self):
        code = self.get_line_numbers("""
if p:
    if a:
        if z:
            pass
        else:
            pass
else:
    pass
""", [0, 1, 2, 3, 5, 7, 1])

    def test_nested_for(self):
        code = self.get_line_numbers("""
for i in range(2):
    for j in range(3):
        x += i * j
print(x)
""", [0, 1, 2, 1, 3])

    def test_ignored_const_produces_lineno(self):
        code = self.get_line_numbers("""
1
'abc'
(1, 2, 3, None)
""", [0, 1, 2])

    def test_assert_bug(self):
        code = self.get_line_numbers("""
try:
    assert 0, 'hi'
except AssertionError as e:
    msg = str(e)
""", [0, 1, 2, 3, 2])

    def test_assert_looks_at_constants(self):
        code = self.get_line_numbers("""
assert 0,\\
        "abc"
pass
""", [0, 1, 0])

    def test_match_optimized(self):
        code = self.get_line_numbers("""
match x:
    case 1 if (

            True):
        pass
    case _:
        pass
print()
""", [0, 1, 3, 1, 4, 5, 6, 7])

    def test_crash_ifelse_in_except(self):
        code = self.get_line_numbers("""
def buggy():
    try:
        pass
    except OSError as exc:
        if a:
            pass
        elif b:
            pass
    else:
        f
""", [1, 2, 3, 4, 5, 6, 7, 3, 9, 6], function=True)

    def test_return_in_with(self):
        code = self.get_line_numbers("""
def withreturn():
    with C():
        return
""", [1, 2, 1], function=True)

    def test_func_without_code(self):
        code = self.get_line_numbers("""def emptybody():
            'abc'
        """, [1], function=True)

    def test_assignment(self):
        code = self.get_line_numbers("""(




            o.
            a
        ) = (
            v + w
        )
        """, [3, 0, 1])

class TestErrorPositions(BaseTestCompiler):
    def test_import_star_in_function_position(self):
        src = "def f(): from _ import *"
        exc = self.error_test(src, SyntaxError)
        assert exc.offset == src.find("*") + 1

    def test_pos_inner_node_nonsense_walrus(self):
        exc = self.error_test("[i for i in range(5) if (juhuu := 0) for juhuu in range(5)]", SyntaxError)
        assert exc.offset == 42
        assert exc.end_offset == 47

class TestDeadCodeGetsRemoved(TestCompiler):
    # check that there is no code emitted when putting all kinds of code into an "if 0:" block
    def simple_test(self, source, evalexpr, expected):
        from pypy.tool import dis3
        c = py.code.Source(source)
        source = "if 0:\n" + str(c.indent())

        space = self.space
        code = compile_with_astcompiler(source, 'exec', space)
        dis3.dis(code)
        assert len(code.co_code) == 4 # load None, return
        assert len(code.co_consts_w) == 1

    st = simple_test

    def error_test(self, *args):
        pass

    test_fstring_encoding = test_fstring_encoding_r = test_kwonly = \
        test_no_indent = test_many_args = test_var_annot_rhs = \
        test_extended_unpacking_fail = lambda self: None

class TestCompilerRevDB(BaseTestCompiler):
    spaceconfig = {"translation.reverse_debugger": True}

    def test_revdb_metavar(self):
        from pypy.interpreter.reverse_debugging import dbstate, setup_revdb
        self.space.reverse_debugging = True
        try:
            setup_revdb(self.space)
            dbstate.standard_code = False
            dbstate.metavars = [self.space.wrap(6)]
            self.simple_test("x = 7*$0", "x", 42)
            dbstate.standard_code = True
            self.error_test("x = 7*$0", SyntaxError)
        finally:
            self.space.reverse_debugging = False


class AppTestCompiler:

    def test_docstring_not_loaded(self):
        import io, dis, sys
        ns = {}
        exec("def f():\n    'hi'", ns)
        f = ns["f"]
        save = sys.stdout
        sys.stdout = output = io.StringIO()
        try:
            dis.dis(f)
        finally:
            sys.stdout = save
        assert "0 ('hi')" not in output.getvalue()

    def test_assert_with_tuple_arg(self):
        try:
            assert False, (3,)
        except AssertionError as e:
            assert str(e) == "(3,)"

    # BUILD_LIST_FROM_ARG is PyPy specific
    @py.test.mark.skipif('config.option.runappdirect')
    def test_build_list_from_arg_length_hint(self):
        hint_called = [False]
        class Foo(object):
            def __iter__(self):
                return FooIter()
        class FooIter:
            def __init__(self):
                self.i = 0
            def __length_hint__(self):
                hint_called[0] = True
                return 5
            def __iter__(self):
                return self
            def __next__(self):
                if self.i < 5:
                    res = self.i
                    self.i += 1
                    return res
                raise StopIteration
        l = [a for a in Foo()]
        assert hint_called[0]
        assert l == list(range(5))

    def test_unicode_in_source(self):
        import sys
        d = {}
        exec('# -*- coding: utf-8 -*-\n\nu = "\xf0\x9f\x92\x8b"', d)
        assert len(d['u']) == 4

    def test_kw_defaults_None(self):
        import _ast
        source = "def foo(self, *args, name): pass"
        ast = compile(source, '', 'exec', _ast.PyCF_ONLY_AST)
        # compiling the produced AST previously triggered a crash
        compile(ast, '', 'exec')

    def test_error_yield(self):
        # These are OK!
        compile("def g(): [x for x in [(yield 1)]]", "<test case>", "exec")
        compile("def g(): [x for x in [(yield from ())]]", "<test case>", "exec")

        def check(snippet, error_msg, offset=-1, end_offset=-1):
            try:
                compile(snippet, "<test case>", "exec")
            except SyntaxError as exc:
                assert exc.msg == error_msg
                assert exc.lineno == 1
                if offset != -1:
                    assert exc.offset == offset
                if end_offset != -1:
                    assert exc.end_offset == end_offset
            else:
                assert False, snippet

        check("def g(): [(yield x) for x in ()]",
              "'yield' inside list comprehension", 12, 19)
        check("def g(): [x for x in () if not (yield x)]",
              "'yield' inside list comprehension", 33, 40)
        check("def g(): [y for x in () for y in [(yield x)]]",
              "'yield' inside list comprehension", 36, 43)
        check("def g(): {(yield x) for x in ()}",
              "'yield' inside set comprehension", 12, 19)
        check("def g(): {(yield x): x for x in ()}",
              "'yield' inside dict comprehension", 12, 19)
        check("def g(): {x: (yield x) for x in ()}",
              "'yield' inside dict comprehension", 15, 22)
        check("def g(): ((yield x) for x in ())",
              "'yield' inside generator expression", 12, 19)
        check("def g(): [(yield from x) for x in ()]",
              "'yield' inside list comprehension", 12, 24)
        check("class C: [(yield x) for x in ()]",
              "'yield' inside list comprehension", 12, 19)
        check("[(yield abcdefghi) for abcdefghi in ()]",
              "'yield' inside list comprehension", 3, 18)

    def test_syntax_warnings_missing_comma(self):
        import warnings

        cases = [
            '[(1, 2) (3, 4)]',
            '[[1, 2] (3, 4)]',
            '[{1, 2} (3, 4)]',
            '[{1: 2} (3, 4)]',
            '[[i for i in range(5)] (3, 4)]',
            '[{i for i in range(5)} (3, 4)]',
            '[(i for i in range(5)) (3, 4)]',
            '[{i: i for i in range(5)} (3, 4)]',
            '[f"{1}" (3, 4)]',
            '["abc" (3, 4)]',
            '[b"abc" (3, 4)]',
            '[123 (3, 4)]',
            '[12.3 (3, 4)]',
            '[12.3j (3, 4)]',
            '[None (3, 4)]',
            '[True (3, 4)]',
            '[... (3, 4)]',
            '[{1, 2} [i, j]]',
            '[{i for i in range(5)} [i, j]]',
            '[(i for i in range(5)) [i, j]]',
            '[(lambda x, y: x) [i, j]]',
            '[123 [i, j]]',
            '[12.3 [i, j]]',
            '[12.3j [i, j]]',
            '[None [i, j]]',
            '[True [i, j]]',
            '[... [i, j]]',
            '(1,2,3)[...]'
        ]
        for case in cases:
            with warnings.catch_warnings(record=True) as w:
                ns = {'i': 1, 'j': 1}
                exec("def foo(): %s" % case, ns)
                try:
                    ns['foo']()
                except TypeError as exc:
                    exc_message = exc.args[0]
                else:
                    exc_message = None

                assert len(w) == 1, case
                assert issubclass(w[-1].category, SyntaxWarning)
                assert exc_message is not None
                initial_part, _, info_part = w[-1].message.args[0].partition("not ")
                assert initial_part in exc_message

    def test_syntax_warnings_is_with_literal(self):
        import warnings

        cases = [
            ("x is 1", "is"),
            ("x is 'thing'", "is"),
            ("1 is x", "is"),
            ("x is y is 1", "is"),
            ("x is not 1", "is not")
        ]
        for case, operator in cases:
            with warnings.catch_warnings(record=True) as w:
                compile(case, '<testcase>', 'eval')
                assert len(w) == 1, case
                assert issubclass(w[-1].category, SyntaxWarning)
                assert operator in w[-1].message.args[0]

    def test_syntax_warnings_assertions(self):
        import warnings
        with warnings.catch_warnings(record=True) as w:
            compile("assert (a, b)", '<testcase>', 'exec')
            assert len(w) == 1, case
            assert issubclass(w[-1].category, SyntaxWarning)
            assert "assertion is always true" in w[-1].message.args[0]

    def test_syntax_warnings_false_positives(self):
        import warnings

        with warnings.catch_warnings():
            warnings.filterwarnings('error', category=SyntaxWarning)
            compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')
            compile('[[1, 2] [i]]', '<testcase>', 'exec')
            compile('[[1, 2] [0]]', '<testcase>', 'exec')
            compile('[[1, 2] [True]]', '<testcase>', 'exec')
            compile('[[1, 2] [1:2]]', '<testcase>', 'exec')
            compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec')
            compile('x is some_other_stuff', '<testcase>', 'exec')
            compile('x is True', '<testcase>', 'exec')
            compile('None is x', '<testcase>', 'exec')
            compile('x is y is False', '<testcase>', 'exec')
            compile('x is y is ...', '<testcase>', 'exec')
            compile('assert a, b', '<testcase>', 'exec')
            compile('assert (), b', '<testcase>', 'exec')

    def test_top_level_async(self):
        import _ast
        import inspect
        import textwrap

        statements = [
            """
            await x
            """,
            """
            foo = bar(await x)
            """,
            """
            async for x in y:
                print(x)
            """,
            """
            async with bar as baz:
                print(await baz.show())
            """,
            """
            [x async for x in y]
            foo = await bar(x async for x in y)
            baz = {x async for x in z} | {x: y async for x, y in z.items()}.keys()
            """,
        ]
        for statement in statements:
            code = compile(
                textwrap.dedent(statement),
                '<testcast>',
                'exec',
                flags=_ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
            )
            assert code.co_flags & inspect.CO_COROUTINE

        for statement in statements:
            try:
                code = compile(
                    textwrap.dedent(statement),
                    '<test case>',
                    'exec'
                )
            except SyntaxError as exc:
                pass
            else:
                assert False, "this case shouldn't compile: %s" % statement

    def test_top_level_async_invalid_cases(self):
        import _ast
        import textwrap

        statements = [
            """def f():  await arange(10)\n""",
            """def f():  [x async for x in arange(10)]\n""",
            """def f():  [await x async for x in arange(10)]\n""",
            """def f():
                   async for i in arange(1):
                       a = 1
            """,
            """def f():
                   async with asyncio.Lock() as l:
                       a = 1
            """,
        ]

        for flags in [0, _ast.PyCF_ALLOW_TOP_LEVEL_AWAIT]:
            for statement in statements:
                try:
                    code = compile(
                        textwrap.dedent(statement),
                        '<test case>',
                        'exec',
                        flags=flags
                    )
                except SyntaxError as exc:
                    pass
                else:
                    assert False, "this case shouldn't compile: %s" % statement

    def test_top_level_async_ensure_generator(self):
        import _ast
        import textwrap
        from types import AsyncGeneratorType

        source = textwrap.dedent("""
            async def ticker():
                for i in range(10):
                    yield i
                    await asyncio.sleep(0)
        """)

        code = compile(
            source,
            '<test case>',
            'exec',
            flags=_ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
        )
        namespace = {}
        exec(code, namespace)
        ticker = namespace['ticker']
        assert isinstance(ticker(), AsyncGeneratorType)

    def test_bug_hanging_compiler(self):
        source = '''
def endless_looping():
    while True: continue
        '''
        compile(source, '<test>', 'exec')


class TestOptimizations:
    def count_instructions(self, source):
        code, blocks = generate_function_code(source, self.space)
        self._code = code
        self._blocks = blocks
        instrs = []
        for block in blocks:
            instrs.extend(block.instructions)
        # print instrs
        counts = {}
        for instr in instrs:
            counts[instr.opcode] = counts.get(instr.opcode, 0) + 1
        return counts

    def test_elim_jump_to_return(self):
        source = """def f():
        return true_value if cond else false_value
        """
        counts = self.count_instructions(source)
        assert ops.JUMP_FORWARD not in counts
        assert ops.JUMP_ABSOLUTE not in counts
        assert counts[ops.RETURN_VALUE] == 2

    def test_forward_cond_jump_to_jump(self):
        source1 = """def jumpymcjumpface():
            if a:
                if (c
                    or d):
                    foo()
            else:
                baz()
            return
        """
        source2 = """def springer():
                while a:
                    # Intentionally use two-line expression to test issue37213.
                    if (c
                        or d):
                        a = foo()
                return
        """
        for source in source1, source2:
            code, blocks = generate_function_code(source, self.space)
            instrs = []
            for block in blocks:
                instrs.extend(block.instructions)
            offset = 0
            offsets = {}
            for instr in instrs:
                offsets[offset] = instr
                offset += instr.size()
            for instr in instrs:
                if instr.opcode == ops.POP_JUMP_IF_FALSE:
                    if instr.arg * 2 == offset: # points to end, return will be inserted later
                        continue
                    target = offsets[instr.arg * 2]
                    assert target.opcode != ops.JUMP_FORWARD and target.opcode != ops.JUMP_ABSOLUTE

    def test_const_fold_subscr(self):
        source = """def f():
        return (0, 1)[0]
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST: 1, ops.RETURN_VALUE: 1}

        source = """def f():
        return (0, 1)[:2]
        """
        # Just checking this doesn't crash out
        self.count_instructions(source)

    def test_const_fold_unicode_subscr(self, monkeypatch):
        source = """def f():
        return "abc"[0]
        """
        counts = self.count_instructions(source)
        if 0:   # xxx later?
            assert counts == {ops.LOAD_CONST: 1, ops.RETURN_VALUE: 1}

        # getitem outside of the BMP should not be optimized
        source = """def f():
        return "\U00012345"[0]
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST: 2, ops.BINARY_SUBSCR: 1,
                          ops.RETURN_VALUE: 1}

        source = """def f():
        return "\U00012345abcdef"[3]
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST: 2, ops.BINARY_SUBSCR: 1,
                          ops.RETURN_VALUE: 1}

        monkeypatch.setattr(optimize, "MAXUNICODE", 0xFFFF)
        source = """def f():
        return "\uE01F"[0]
        """
        counts = self.count_instructions(source)
        if 0:   # xxx later?
            assert counts == {ops.LOAD_CONST: 1, ops.RETURN_VALUE: 1}
        monkeypatch.undo()

        # getslice is not yet optimized.
        # Still, check a case which yields the empty string.
        source = """def f():
        return "abc"[:0]
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST: 3, ops.BUILD_SLICE: 1,
                          ops.BINARY_SUBSCR: 1, ops.RETURN_VALUE: 1}

    def test_remove_dead_code(self):
        source = """def f(x):
            return 5
            x += 1
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST:1, ops.RETURN_VALUE: 1}

    def test_remove_dead_code_after_raise(self):
        source = """def f(x):
            raise ValueError
            x += 1
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_GLOBAL:1, ops.RAISE_VARARGS: 1}


    def test_remove_dead_jump_after_return(self):
        source = """def f(x, y, z):
            if x:
                return y
            else:
                return z
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_FAST: 3,
                          ops.POP_JUMP_IF_FALSE: 1,
                          ops.RETURN_VALUE: 2}

    def test_remove_dead_yield(self):
        source = """def f(x):
            return
            yield 6
        """
        counts = self.count_instructions(source)
        assert counts == {ops.LOAD_CONST:1, ops.RETURN_VALUE: 1}
        #
        space = self.space
        w_generator = space.appexec([], """():
            d = {}
            exec('''def f(x):
                return
                yield 6
            ''', d)
            return d['f'](5)
        """)
        assert 'generator' in space.text_w(space.repr(w_generator))

    def test_folding_of_list_constants(self):
        for source in (
            # in/not in constants with BUILD_LIST should be folded to a tuple:
            'a in [1,2,3]',
            'a not in ["a","b","c"]',
            'a in [None, 1, None]',
            'a not in [(1, 2), 3, 4]',
            ):
            source = 'def f(): %s' % source
            counts = self.count_instructions(source)
            assert ops.BUILD_LIST not in counts
            assert ops.LOAD_CONST in counts

    def test_folding_of_set_constants(self):
        for source in (
            # in/not in constants with BUILD_SET should be folded to a frozenset:
            'a in {1,2,3}',
            'a not in {"a","b","c"}',
            'a in {None, 1, None}',
            'a not in {(1, 2), 3, 4}',
            'a in {1, 2, 3, 3, 2, 1}',
            ):
            source = 'def f(): %s' % source
            counts = self.count_instructions(source)
            assert ops.BUILD_SET not in counts
            assert ops.LOAD_CONST in counts

    def test_dont_fold_huge_powers(self):
        for source, op in (
                ("2 ** 3000", ops.BINARY_POWER),  # not constant-folded: too big
                ("(-2) ** 3000", ops.BINARY_POWER),
                ("5 << 1000", ops.BINARY_LSHIFT),
            ):
            source = 'def f(): %s' % source
            counts = self.count_instructions(source)
            assert op in counts

        for source in (
            "2 ** 2000",         # constant-folded
            "2 ** -3000",
            "1.001 ** 3000",
            "1 ** 3000.0",
            ):
            source = 'def f(): %s' % source
            counts = self.count_instructions(source)
            assert ops.BINARY_POWER not in counts

    def test_call_function_var(self):
        source = """def f():
            call(*me)
        """
        code, blocks = generate_function_code(source, self.space)
        # there is a stack computation error
        assert blocks[0].instructions[3].arg == 0

    def test_fstring(self):
        source = """def f(x):
            return f'ab{x}cd'
        """
        code, blocks = generate_function_code(source, self.space)

    def test_empty_tuple_target(self):
        source = """def f():
            () = ()
            del ()
            [] = []
            del []
        """
        generate_function_code(source, self.space)

    def test_make_constant_map(self):
        source = """def f():
            return {"A": 1, "b": 2}
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_MAP not in counts
        source = """def f():
            return {"a": 1, "b": {}, 1: {"a": x}}
        """
        counts = self.count_instructions(source)
        assert counts[ops.BUILD_MAP] == 1 # the empty dict
        assert counts[ops.BUILD_CONST_KEY_MAP] == 2

    def test_annotation_issue2884(self):
        source = """def f():
            a: list = [j for j in range(10)]
        """
        generate_function_code(source, self.space)

    def test_constant_tuples(self):
        source = """def f():
            return ((u"a", 1), 2)
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts
        # also for bytes
        source = """def f():
            return ((b"a", 5), 5, 7, 8)
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

    def test_fold_defaults_tuple(self):
        source = """def f():
            def g(a, b=2, c=None, d='foo'):
                return None
            return g
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

        source = """def f():
            g = lambda a, b=2, c=None, d='foo': None
            return g
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

        source = """def f():
            def g(a, b=2, c=None, d=[]):
                return None
            return g
        """
        counts = self.count_instructions(source)
        assert counts[ops.BUILD_TUPLE] == 1

    def test_constant_tuples_star(self):
        source = """def f(a, c):
            return (u"a", 1, *a, 3, 5, 3, *c)
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

        source = """def f(a, c, d):
            return (u"a", 1, *a, c, 1, *d, 1, 2, 3)
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

    def test_constant_tuples_star_bug(self):
        source = """def f(a, c):
            return (*a, *c)
        """
        # very annoying bug: this was turned into
        # (*(), *a, *(), *c) :-(
        counts = self.count_instructions(source)
        assert ops.LOAD_CONST not in counts

    def test_constant_list_star(self):
        source = """def f(a, c):
            return [u"a", 1, *a, 3, 5, 3, *c]
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

        source = """def f(a, c, d):
            return [u"a", 1, *a, c, 1, *d, 1, 2, 3]
        """
        counts = self.count_instructions(source)
        assert ops.BUILD_TUPLE not in counts

    def test_call_bytecodes(self):
        # check that the expected bytecodes are generated
        source = """def f(): x(a, b, c)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_FUNCTION] == 1

        source = """def f(): x(a, b, c, x=1, y=2)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_FUNCTION_KW] == 1

        source = """def f(): x(a, b, c, *(d, 2), x=1, y=2)"""
        counts = self.count_instructions(source)
        assert counts[ops.BUILD_TUPLE] == 1
        assert counts[ops.LIST_TO_TUPLE] == 1
        assert counts[ops.CALL_FUNCTION_EX] == 1

        source = """def f(): x(a, b, c, **kwargs)"""
        counts = self.count_instructions(source)
        assert counts[ops.BUILD_TUPLE] == 1
        assert counts[ops.CALL_FUNCTION_EX] == 1

        source = """def f(): x(**kwargs)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_FUNCTION_EX] == 1
        assert ops.DICT_MERGE not in counts
        assert ops.BUILD_MAP not in counts

        source = """def f(): x(a, b, c, **kwargs)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_FUNCTION_EX] == 1
        assert ops.DICT_MERGE not in counts
        assert ops.BUILD_MAP not in counts

        source = """def f(): x.m(a, b, c)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_METHOD] == 1

        source = """def f(): x.m(a, b, c, y=1)"""
        counts = self.count_instructions(source)
        assert counts[ops.CALL_METHOD_KW] == 1

    def test_dont_emit_dead_blocks(self):
        source = """def f():
        if A:
            if B:
                if C:
                    if D:
                        return False
            else:
                return False
        elif E and F:
            return True
        """
        counts = self.count_instructions(source)
        assert counts[ops.RETURN_VALUE] == 7

    def test_assert_looks_at_constants(self):
        source = """def assert0():
            assert 0,\\
                    "abc"
        """
        counts = self.count_instructions(source)
        assert counts.get(ops.POP_JUMP_IF_TRUE, 0) == 0

    def test_match_optimized(self):
        source = """def f():
            match x:
                case 1 if (

                        True):
                    pass
                case _:
                    pass
            print()
        """
        counts = self.count_instructions(source)
        assert counts.get(ops.JUMP_FORWARD, 0) == 1

    @pytest.mark.xfail
    def test_match_optimize_default(self):
        source = """def f():
            match x:
                case 1:
                    return 1
                case _:
                    return 2
        """
        counts = self.count_instructions(source)
        assert counts.get(ops.POP_TOP, 0) == 0

    def test_jump_if_false_or_pop_to_same(self):
        source = """def f(a, b, c):
            return ((a and b)
                    and c)
        """
        counts = self.count_instructions(source)
        assert self._blocks[0].instructions[1].jump.instructions[0].opcode != ops.JUMP_IF_FALSE_OR_POP
        assert self._blocks[0].instructions[1].jump is self._blocks[-1]


    def test_jump_if_true_or_pop_to_same(self):
        source = """def f(a, b, c):
            return ((a or b)
                    or c)
        """
        counts = self.count_instructions(source)
        assert self._blocks[0].instructions[1].jump.instructions[0].opcode != ops.JUMP_IF_TRUE_OR_POP
        assert self._blocks[0].instructions[1].jump is self._blocks[-1]

    def test_listcomp_assignment_hack(self):
        self.count_instructions("""def listcomp_assignment_hack():
            return [y for x in a for y in [f(x)]]
        """)
        assert self._code.consts_w[1].co_code.count(chr(ops.FOR_ITER)) == 1
        self.count_instructions("""def listcomp_assignment_hack2():
            return [y for x in [1, 2, 3] for y in []]
        """)
        # it's really pointless to optimize this
        assert self._code.consts_w[1].co_code.count(chr(ops.FOR_ITER)) == 2


class TestHugeStackDepths:
    def run_and_check_stacksize(self, source):
        space = self.space
        code = compile_with_astcompiler("a = " + source, 'exec', space)
        assert code.co_stacksize < 100
        w_dict = space.newdict()
        code.exec_code(space, w_dict, w_dict)
        return space.getitem(w_dict, space.newtext("a"))

    def test_tuple(self):
        source = "(" + ",".join([str(i) for i in range(200)]) + ")\n"
        w_res = self.run_and_check_stacksize(source)
        assert self.space.unwrap(w_res) == tuple(range(200))

    def test_list(self):
        source = "[" + ",".join([str(i) for i in range(200)]) + "]\n"
        w_res = self.run_and_check_stacksize(source)
        assert self.space.unwrap(w_res) == range(200)

    def test_list_unpacking(self):
        space = self.space
        source = "[" + ",".join(['b%d' % i for i in range(200)]) + "] = a\n"
        code = compile_with_astcompiler(source, 'exec', space)
        assert code.co_stacksize == 200   # xxx remains big
        w_dict = space.newdict()
        space.setitem(w_dict, space.newtext("a"), space.wrap(range(42, 242)))
        code.exec_code(space, w_dict, w_dict)
        assert space.unwrap(space.getitem(w_dict, space.newtext("b0"))) == 42
        assert space.unwrap(space.getitem(w_dict, space.newtext("b199"))) == 241

    def test_set(self):
        source = "{" + ",".join([str(i) for i in range(200)]) + "}\n"
        w_res = self.run_and_check_stacksize(source)
        space = self.space
        assert [space.int_w(w_x)
                    for w_x in space.unpackiterable(w_res)] == range(200)

    def test_dict(self):
        source = "{" + ",".join(['%s: None' % (i, ) for i in range(200)]) + "}\n"
        w_res = self.run_and_check_stacksize(source)
        assert self.space.unwrap(w_res) == dict.fromkeys(range(200))

    def test_dict_bug(self):
        source = s = "1\ndef f(): l = list(range(400)); return {%s}\na = f()" % (
            ", ".join(["l.pop(): l.pop()"] * 200))
        w_res = self.run_and_check_stacksize(source)
        l = list(range(400))
        d = {}
        while l:
            key = l.pop()
            value = l.pop()
            d[key] = value
        assert self.space.unwrap(w_res) == d

    def test_callargs(self):
        source = "(lambda *args: args)(" + ", ".join([str(i) for i in range(200)]) + ")\n"
        w_res = self.run_and_check_stacksize(source)
        assert self.space.unwrap(w_res) == tuple(range(200))

        source = "(lambda **args: args)(" + ", ".join(["s%s=None" % i for i in range(200)]) + ")\n"
        w_res = self.run_and_check_stacksize(source)
        assert self.space.unwrap(w_res) == dict.fromkeys(["s" + str(i) for i in range(200)])

    def test_call_method_args(self):
        args = "1, " * 217
        source = '(type("A", (), {"f": lambda self, *args: len(args)}))().f(%s)' % args
        w_res = self.run_and_check_stacksize(source)
        assert self.space.int_w(w_res) == 217