File: pyopcode.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 (2255 lines) | stat: -rw-r--r-- 93,211 bytes parent folder | download
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
"""
Implementation of a part of the standard Python opcodes.

The rest, dealing with variables in optimized ways, is in nestedscope.py.
"""
import sys

from rpython.rlib import jit, rstackovf, rstring
from rpython.rlib.debug import check_nonneg
from rpython.rlib.objectmodel import (
    we_are_translated, always_inline, dont_inline, not_rpython)
from rpython.rlib.rarithmetic import r_uint, intmask
from rpython.tool.sourcetools import func_with_new_name

from pypy.interpreter import (
    gateway, function, eval, pyframe, pytraceback, pycode
)
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt, oefmt_name_error, raise_import_error
from pypy.interpreter.nestedscope import Cell
from pypy.interpreter.pycode import PyCode, BytecodeCorruption
from pypy.tool.stdlib_opcode import bytecode_spec

CANNOT_CATCH_MSG = ("catching classes that do not inherit from BaseException "
                    "is not allowed")

@not_rpython
def unaryoperation(operationname):
    def opimpl(self, *ignored):
        operation = getattr(self.space, operationname)
        w_1 = self.popvalue()
        w_result = operation(w_1)
        self.pushvalue(w_result)
    opimpl.unaryop = operationname

    return func_with_new_name(opimpl, "opcode_impl_for_%s" % operationname)

@not_rpython
def binaryoperation(operationname):
    def opimpl(self, *ignored):
        operation = getattr(self.space, operationname)
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        w_result = operation(w_1, w_2)
        self.pushvalue(w_result)
    opimpl.binop = operationname

    return func_with_new_name(opimpl, "opcode_impl_for_%s" % operationname)


opcodedesc = bytecode_spec.opcodedesc
HAVE_ARGUMENT = bytecode_spec.HAVE_ARGUMENT

class __extend__(pyframe.PyFrame):
    """A PyFrame that knows about interpretation of standard Python opcodes
    minus the ones related to nested scopes."""

    ### opcode dispatch ###

    def dispatch(self, pycode, next_instr, ec):
        # For the sequel, force 'next_instr' to be unsigned for performance
        next_instr = r_uint(next_instr)
        co_code = pycode.co_code
        try:
            while True:
                assert next_instr & 1 == 0
                next_instr = self.handle_bytecode(co_code, next_instr, ec)
        except ExitFrame:
            return self.popvalue()

    def handle_bytecode(self, co_code, next_instr, ec):
        try:
            next_instr = self.dispatch_bytecode(co_code, next_instr, ec)
        except OperationError as operr:
            try:
                operr.record_context(self.space, ec)
            except OperationError as operr2:
                # this should be unreachable! but we don't want to crash if it
                # still happens
                w_value = operr.normalize_exception(self.space)
                w_value2 = operr2.normalize_exception(self.space)
                # NB: don't *raise* it, it's handled by the
                # handle_operation_error call below (otherwise some higher up
                # except block handles it)
                operr = oefmt(
                    self.space.w_TypeError,
                    "couldn't record exception context for exception '%T', got: %R",
                    w_value,
                    w_value2)
            next_instr = self.handle_operation_error(ec, operr)
        except RaiseWithExplicitTraceback as e:
            next_instr = self.handle_operation_error(ec, e.operr,
                                                     attach_tb=False)
        except KeyboardInterrupt:
            next_instr = self.handle_asynchronous_error(ec,
                self.space.w_KeyboardInterrupt)
        except MemoryError:
            next_instr = self.handle_asynchronous_error(ec,
                self.space.w_MemoryError)
        except rstackovf.StackOverflow as e:
            # Note that this case catches AttributeError!
            rstackovf.check_stack_overflow()
            next_instr = self.handle_asynchronous_error(ec,
                self.space.w_RecursionError,
                self.space.newtext("maximum recursion depth exceeded"))
        return next_instr

    def handle_asynchronous_error(self, ec, w_type, w_value=None):
        # catch asynchronous exceptions and turn them
        # into OperationErrors
        if w_value is None:
            w_value = self.space.w_None
        operr = OperationError(w_type, w_value)
        return self.handle_operation_error(ec, operr)

    def handle_generator_error(self, operr):
        # for generator.py
        ec = self.space.getexecutioncontext()
        return self.handle_operation_error(ec, operr)

    def handle_operation_error(self, ec, operr, attach_tb=True):
        if attach_tb:
            if 1:
                # xxx this is a hack.  It allows bytecode_trace() to
                # call a signal handler which raises, and catch the
                # raised exception immediately.  See test_alarm_raise in
                # pypy/module/signal/test/test_signal.py.  Without the
                # next four lines, if an external call (like
                # socket.accept()) is interrupted by a signal, it raises
                # an exception carrying EINTR which arrives here,
                # entering the next "except" block -- but the signal
                # handler is then called on the next call to
                # dispatch_bytecode(), causing the real exception to be
                # raised after the exception handler block was popped.
                try:
                    trace = self.get_w_f_trace()
                    if trace is not None:
                        self.getorcreatedebug().w_f_trace = None
                    try:
                        ec.bytecode_trace_after_exception(self)
                    finally:
                        if trace is not None:
                            self.getorcreatedebug().w_f_trace = trace
                except OperationError as e:
                    operr = e
            pytraceback.record_application_traceback(
                self.space, operr, self, self.last_instr)
            ec.exception_trace(self, operr)

        block = self.unrollstack()
        if block is None:
            # no handler found for the OperationError
            if we_are_translated():
                raise operr
            else:
                # try to preserve the CPython-level traceback
                import sys
                tb = sys.exc_info()[2]
                raise OperationError, operr, tb
        else:
            unroller = SApplicationException(operr)
            next_instr = block.handle(self, unroller)
            return next_instr

    def call_contextmanager_exit_function(self, w_func, w_typ, w_val, w_tb):
        return self.space.call_function(w_func, w_typ, w_val, w_tb)

    @jit.unroll_safe
    def dispatch_bytecode(self, co_code, next_instr, ec):
        while True:
            assert next_instr & 1 == 0
            self.last_instr = intmask(next_instr)
            if jit.we_are_jitted():
                ec.bytecode_only_trace(self)
            else:
                ec.bytecode_trace(self)
            next_instr = r_uint(self.last_instr)
            assert next_instr & 1 == 0
            opcode = ord(co_code[next_instr])
            oparg = ord(co_code[next_instr + 1])
            next_instr += 2

            # note: the structure of the code here is such that it makes
            # (after translation) a big "if/elif" chain, which is then
            # turned into a switch().

            while opcode == opcodedesc.EXTENDED_ARG.index:
                opcode = ord(co_code[next_instr])
                arg = ord(co_code[next_instr + 1])
                if opcode < HAVE_ARGUMENT:
                    raise BytecodeCorruption
                next_instr += 2
                oparg = (oparg * 256) | arg

            if opcode == opcodedesc.RETURN_VALUE.index:
                assert not self.blockstack_non_empty()
                self.frame_finished_execution = True  # for generators
                raise Return
            elif opcode == opcodedesc.JUMP_ABSOLUTE.index:
                return self.jump_absolute(oparg, next_instr, ec)
            elif opcode == opcodedesc.RERAISE.index:
                return self.RERAISE(oparg, next_instr)
            elif opcode == opcodedesc.FOR_ITER.index:
                next_instr = self.FOR_ITER(oparg, next_instr)
            elif opcode == opcodedesc.JUMP_FORWARD.index:
                next_instr = self.JUMP_FORWARD(oparg, next_instr)
            elif opcode == opcodedesc.JUMP_IF_FALSE_OR_POP.index:
                return self.JUMP_IF_FALSE_OR_POP(oparg, next_instr, ec)
            elif opcode == opcodedesc.JUMP_IF_TRUE_OR_POP.index:
                return self.JUMP_IF_TRUE_OR_POP(oparg, next_instr, ec)
            elif opcode == opcodedesc.POP_JUMP_IF_FALSE.index:
                return self.POP_JUMP_IF_FALSE(oparg, next_instr, ec)
            elif opcode == opcodedesc.POP_JUMP_IF_TRUE.index:
                return self.POP_JUMP_IF_TRUE(oparg, next_instr, ec)
            elif opcode == opcodedesc.JUMP_IF_NOT_EXC_MATCH.index:
                next_instr = self.JUMP_IF_NOT_EXC_MATCH(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_ADD.index:
                self.BINARY_ADD(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_AND.index:
                self.BINARY_AND(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_FLOOR_DIVIDE.index:
                self.BINARY_FLOOR_DIVIDE(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_MATRIX_MULTIPLY.index:
                self.BINARY_MATRIX_MULTIPLY(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_LSHIFT.index:
                self.BINARY_LSHIFT(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_MODULO.index:
                self.BINARY_MODULO(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_MULTIPLY.index:
                self.BINARY_MULTIPLY(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_OR.index:
                self.BINARY_OR(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_POWER.index:
                self.BINARY_POWER(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_RSHIFT.index:
                self.BINARY_RSHIFT(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_SUBSCR.index:
                self.BINARY_SUBSCR(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_SUBTRACT.index:
                self.BINARY_SUBTRACT(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_TRUE_DIVIDE.index:
                self.BINARY_TRUE_DIVIDE(oparg, next_instr)
            elif opcode == opcodedesc.BINARY_XOR.index:
                self.BINARY_XOR(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_CONST_KEY_MAP.index:
                self.BUILD_CONST_KEY_MAP(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_LIST.index:
                self.BUILD_LIST(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_LIST_FROM_ARG.index:
                self.BUILD_LIST_FROM_ARG(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_MAP.index:
                self.BUILD_MAP(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_SET.index:
                self.BUILD_SET(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_SLICE.index:
                self.BUILD_SLICE(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_TUPLE.index:
                self.BUILD_TUPLE(oparg, next_instr)
            elif opcode == opcodedesc.CALL_FUNCTION.index:
                self.CALL_FUNCTION(oparg, next_instr)
            elif opcode == opcodedesc.CALL_FUNCTION_KW.index:
                self.CALL_FUNCTION_KW(oparg, next_instr)
            elif opcode == opcodedesc.CALL_FUNCTION_EX.index:
                self.CALL_FUNCTION_EX(oparg, next_instr)
            elif opcode == opcodedesc.CALL_METHOD.index:
                self.CALL_METHOD(oparg, next_instr)
            elif opcode == opcodedesc.CALL_METHOD_KW.index:
                self.CALL_METHOD_KW(oparg, next_instr)
            elif opcode == opcodedesc.CHECK_EG_MATCH.index:
                self.CHECK_EG_MATCH(oparg, next_instr)
            elif opcode == opcodedesc.COMPARE_OP.index:
                self.COMPARE_OP(oparg, next_instr)
            elif opcode == opcodedesc.IS_OP.index:
                self.IS_OP(oparg, next_instr)
            elif opcode == opcodedesc.CONTAINS_OP.index:
                self.CONTAINS_OP(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_ATTR.index:
                self.DELETE_ATTR(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_DEREF.index:
                self.DELETE_DEREF(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_FAST.index:
                self.DELETE_FAST(oparg, next_instr)
            elif opcode == opcodedesc.SETUP_ANNOTATIONS.index:
                self.SETUP_ANNOTATIONS(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_GLOBAL.index:
                self.DELETE_GLOBAL(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_NAME.index:
                self.DELETE_NAME(oparg, next_instr)
            elif opcode == opcodedesc.DELETE_SUBSCR.index:
                self.DELETE_SUBSCR(oparg, next_instr)
            elif opcode == opcodedesc.DUP_TOP.index:
                self.DUP_TOP(oparg, next_instr)
            elif opcode == opcodedesc.DUP_TOP_TWO.index:
                self.DUP_TOP_TWO(oparg, next_instr)
            elif opcode == opcodedesc.GET_ITER.index:
                self.GET_ITER(oparg, next_instr)
            elif opcode == opcodedesc.IMPORT_FROM.index:
                self.IMPORT_FROM(oparg, next_instr)
            elif opcode == opcodedesc.IMPORT_NAME.index:
                self.IMPORT_NAME(oparg, next_instr)
            elif opcode == opcodedesc.IMPORT_STAR.index:
                self.IMPORT_STAR(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_ADD.index:
                self.INPLACE_ADD(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_AND.index:
                self.INPLACE_AND(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_FLOOR_DIVIDE.index:
                self.INPLACE_FLOOR_DIVIDE(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_LSHIFT.index:
                self.INPLACE_LSHIFT(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_MATRIX_MULTIPLY.index:
                self.INPLACE_MATRIX_MULTIPLY(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_MODULO.index:
                self.INPLACE_MODULO(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_MULTIPLY.index:
                self.INPLACE_MULTIPLY(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_OR.index:
                self.INPLACE_OR(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_POWER.index:
                self.INPLACE_POWER(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_RSHIFT.index:
                self.INPLACE_RSHIFT(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_SUBTRACT.index:
                self.INPLACE_SUBTRACT(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_TRUE_DIVIDE.index:
                self.INPLACE_TRUE_DIVIDE(oparg, next_instr)
            elif opcode == opcodedesc.INPLACE_XOR.index:
                self.INPLACE_XOR(oparg, next_instr)
            elif opcode == opcodedesc.LIST_APPEND.index:
                self.LIST_APPEND(oparg, next_instr)
            elif opcode == opcodedesc.LIST_EXTEND.index:
                self.LIST_EXTEND(oparg, next_instr)
            elif opcode == opcodedesc.LIST_TO_TUPLE.index:
                self.LIST_TO_TUPLE(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_ATTR.index:
                self.LOAD_ATTR(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_BUILD_CLASS.index:
                self.LOAD_BUILD_CLASS(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_CLOSURE.index:
                self.LOAD_CLOSURE(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_CONST.index:
                self.LOAD_CONST(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_DEREF.index:
                self.LOAD_DEREF(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_CLASSDEREF.index:
                self.LOAD_CLASSDEREF(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_FAST.index:
                self.LOAD_FAST(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_GLOBAL.index:
                self.LOAD_GLOBAL(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_NAME.index:
                self.LOAD_NAME(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_METHOD.index:
                self.LOAD_METHOD(oparg, next_instr)
            elif opcode == opcodedesc.MAKE_FUNCTION.index:
                self.MAKE_FUNCTION(oparg, next_instr)
            elif opcode == opcodedesc.MAP_ADD.index:
                self.MAP_ADD(oparg, next_instr)
            elif opcode == opcodedesc.DICT_MERGE.index:
                self.DICT_MERGE(oparg, next_instr)
            elif opcode == opcodedesc.DICT_UPDATE.index:
                self.DICT_UPDATE(oparg, next_instr)
            elif opcode == opcodedesc.NOP.index:
                self.NOP(oparg, next_instr)
            elif opcode == opcodedesc.POP_BLOCK.index:
                self.POP_BLOCK(oparg, next_instr)
            elif opcode == opcodedesc.POP_EXCEPT.index:
                self.POP_EXCEPT(oparg, next_instr)
            elif opcode == opcodedesc.POP_TOP.index:
                self.POP_TOP(oparg, next_instr)
            elif opcode == opcodedesc.PREP_RERAISE_STAR.index:
                self.PREP_RERAISE_STAR(oparg, next_instr)
            elif opcode == opcodedesc.PRINT_EXPR.index:
                self.PRINT_EXPR(oparg, next_instr)
            elif opcode == opcodedesc.RAISE_VARARGS.index:
                self.RAISE_VARARGS(oparg, next_instr)
            elif opcode == opcodedesc.ROT_FOUR.index:
                self.ROT_FOUR(oparg, next_instr)
            elif opcode == opcodedesc.ROT_THREE.index:
                self.ROT_THREE(oparg, next_instr)
            elif opcode == opcodedesc.ROT_TWO.index:
                self.ROT_TWO(oparg, next_instr)
            elif opcode == opcodedesc.SETUP_EXCEPT.index:
                self.SETUP_EXCEPT(oparg, next_instr)
            elif opcode == opcodedesc.SETUP_FINALLY.index:
                self.SETUP_FINALLY(oparg, next_instr)
            elif opcode == opcodedesc.SETUP_WITH.index:
                self.SETUP_WITH(oparg, next_instr)
            elif opcode == opcodedesc.SET_ADD.index:
                self.SET_ADD(oparg, next_instr)
            elif opcode == opcodedesc.SET_UPDATE.index:
                self.SET_UPDATE(oparg, next_instr)
            elif opcode == opcodedesc.STORE_ATTR.index:
                self.STORE_ATTR(oparg, next_instr)
            elif opcode == opcodedesc.STORE_DEREF.index:
                self.STORE_DEREF(oparg, next_instr)
            elif opcode == opcodedesc.STORE_FAST.index:
                self.STORE_FAST(oparg, next_instr)
            elif opcode == opcodedesc.STORE_GLOBAL.index:
                self.STORE_GLOBAL(oparg, next_instr)
            elif opcode == opcodedesc.STORE_NAME.index:
                self.STORE_NAME(oparg, next_instr)
            elif opcode == opcodedesc.STORE_SUBSCR.index:
                self.STORE_SUBSCR(oparg, next_instr)
            elif opcode == opcodedesc.UNARY_INVERT.index:
                self.UNARY_INVERT(oparg, next_instr)
            elif opcode == opcodedesc.UNARY_NEGATIVE.index:
                self.UNARY_NEGATIVE(oparg, next_instr)
            elif opcode == opcodedesc.UNARY_NOT.index:
                self.UNARY_NOT(oparg, next_instr)
            elif opcode == opcodedesc.UNARY_POSITIVE.index:
                self.UNARY_POSITIVE(oparg, next_instr)
            elif opcode == opcodedesc.UNPACK_EX.index:
                self.UNPACK_EX(oparg, next_instr)
            elif opcode == opcodedesc.UNPACK_SEQUENCE.index:
                self.UNPACK_SEQUENCE(oparg, next_instr)
            elif opcode == opcodedesc.WITH_EXCEPT_START.index:
                self.WITH_EXCEPT_START(oparg, next_instr)
            elif opcode == opcodedesc.YIELD_VALUE.index:
                self.YIELD_VALUE(oparg, next_instr)
            elif opcode == opcodedesc.YIELD_FROM.index:
                self.YIELD_FROM(oparg, next_instr)
            elif opcode == opcodedesc.GET_YIELD_FROM_ITER.index:
                self.GET_YIELD_FROM_ITER(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_ASSERTION_ERROR.index:
                self.LOAD_ASSERTION_ERROR(oparg, next_instr)
            elif opcode == opcodedesc.GET_AWAITABLE.index:
                self.GET_AWAITABLE(oparg, next_instr)
            elif opcode == opcodedesc.SETUP_ASYNC_WITH.index:
                self.SETUP_ASYNC_WITH(oparg, next_instr)
            elif opcode == opcodedesc.BEFORE_ASYNC_WITH.index:
                self.BEFORE_ASYNC_WITH(oparg, next_instr)
            elif opcode == opcodedesc.GET_AITER.index:
                self.GET_AITER(oparg, next_instr)
            elif opcode == opcodedesc.GET_ANEXT.index:
                self.GET_ANEXT(oparg, next_instr)
            elif opcode == opcodedesc.END_ASYNC_FOR.index:
                next_instr = self.END_ASYNC_FOR(oparg, next_instr)
            elif opcode == opcodedesc.FORMAT_VALUE.index:
                self.FORMAT_VALUE(oparg, next_instr)
            elif opcode == opcodedesc.BUILD_STRING.index:
                self.BUILD_STRING(oparg, next_instr)
            elif opcode == opcodedesc.LOAD_REVDB_VAR.index:
                self.LOAD_REVDB_VAR(oparg, next_instr)
            elif opcode == opcodedesc.GET_LEN.index:
                self.GET_LEN(oparg, next_instr)
            elif opcode == opcodedesc.MATCH_MAPPING.index:
                self.MATCH_MAPPING(oparg, next_instr)
            elif opcode == opcodedesc.MATCH_SEQUENCE.index:
                self.MATCH_SEQUENCE(oparg, next_instr)
            elif opcode == opcodedesc.MATCH_KEYS.index:
                self.MATCH_KEYS(oparg, next_instr)
            elif opcode == opcodedesc.MATCH_CLASS.index:
                self.MATCH_CLASS(oparg, next_instr)
            elif opcode == opcodedesc.COPY_DICT_WITHOUT_KEYS.index:
                self.COPY_DICT_WITHOUT_KEYS(oparg, next_instr)
            elif opcode == opcodedesc.ROT_N.index:
                self.ROT_N(oparg, next_instr)
            else:
                self.MISSING_OPCODE(oparg, next_instr)

            if jit.we_are_jitted():
                return next_instr

    @jit.unroll_safe
    def unrollstack(self):
        while self.blockstack_non_empty():
            block = self.pop_block()
            if not isinstance(block, SysExcInfoRestorer):
                return block
            block.cleanupstack(self)
        self.frame_finished_execution = True  # for generators
        return None


    ### accessor functions ###

    def getlocalvarname(self, index):
        return self.getcode().co_varnames[index]

    def getconstant_w(self, index):
        return self.getcode().co_consts_w[index]

    def getname_u(self, index):
        return self.space.text_w(self.getname_w(index))

    def getname_w(self, index):
        return self.getcode().co_names_w[index]


    ################################################################
    ##  Implementation of the "operational" opcodes
    ##  See also nestedscope.py for the rest.
    ##

    def NOP(self, oparg, next_instr):
        # annotation-time check: if it fails, it means that the decoding
        # of oparg failed to produce an integer which is annotated as non-neg
        check_nonneg(oparg)

    @always_inline
    def LOAD_FAST(self, varindex, next_instr):
        # access a local variable directly
        w_value = self.locals_cells_stack_w[varindex]
        if w_value is None:
            self._load_fast_failed(varindex)
        self.pushvalue(w_value)

    @dont_inline
    def _load_fast_failed(self, varindex):
        varname = self.getlocalvarname(varindex)
        raise oefmt(self.space.w_UnboundLocalError,
                    "local variable '%s' referenced before assignment",
                    varname)

    def LOAD_CONST(self, constindex, next_instr):
        w_const = self.getconstant_w(constindex)
        self.pushvalue(w_const)

    def STORE_FAST(self, varindex, next_instr):
        w_newvalue = self.popvalue()
        assert w_newvalue is not None
        self.locals_cells_stack_w[varindex] = w_newvalue

    def getfreevarname(self, index):
        pycode = self.pycode
        if self.iscellvar(index):
            return pycode.co_cellvars[index]
        return pycode.co_freevars[index - len(pycode.co_cellvars)]

    def iscellvar(self, index):
        # is the variable given by index a cell or a free var?
        return index < len(self.pycode.co_cellvars)

    def LOAD_DEREF(self, varindex, next_instr):
        # nested scopes: access a variable through its cell object
        cell = self._getcell(varindex)
        try:
            w_value = cell.get()
        except ValueError:
            self.raise_exc_unbound(varindex)
        else:
            self.pushvalue(w_value)

    def LOAD_CLASSDEREF(self, varindex, next_instr):
        # like LOAD_DEREF but used in class bodies
        space = self.space
        i = varindex - len(self.pycode.co_cellvars)
        assert i >= 0
        name = self.pycode.co_freevars[i]
        w_value = space.finditem(self.debugdata.w_locals, space.newtext(name))
        if w_value is None:
            self.LOAD_DEREF(varindex, next_instr)
        else:
            self.pushvalue(w_value)

    def STORE_DEREF(self, varindex, next_instr):
        # nested scopes: access a variable through its cell object
        w_newvalue = self.popvalue()
        cell = self._getcell(varindex)
        cell.set(w_newvalue)

    def DELETE_DEREF(self, varindex, next_instr):
        cell = self._getcell(varindex)
        try:
            cell.get()
        except ValueError:
            self.raise_exc_unbound(varindex)
        else:
            cell.set(None)

    def raise_exc_unbound(self, varindex):
        varname = self.getfreevarname(varindex)
        if self.iscellvar(varindex):
            raise oefmt(self.space.w_UnboundLocalError,
                        "local variable '%s' referenced before assignment",
                        varname)
        else:
            raise oefmt(self.space.w_NameError,
                        "free variable '%s' referenced before assignment"
                        " in enclosing scope", varname)

    def LOAD_CLOSURE(self, varindex, next_instr):
        # nested scopes: access the cell object
        w_value = self._getcell(varindex)
        self.pushvalue(w_value)

    def POP_TOP(self, oparg, next_instr):
        self.popvalue()

    def ROT_TWO(self, oparg, next_instr):
        w_1 = self.popvalue()
        w_2 = self.popvalue()
        self.pushvalue(w_1)
        self.pushvalue(w_2)

    def ROT_THREE(self, oparg, next_instr):
        w_1 = self.popvalue()
        w_2 = self.popvalue()
        w_3 = self.popvalue()
        self.pushvalue(w_1)
        self.pushvalue(w_3)
        self.pushvalue(w_2)

    def ROT_FOUR(self, oparg, next_instr):
        w_1 = self.popvalue()
        w_2 = self.popvalue()
        w_3 = self.popvalue()
        w_4 = self.popvalue()
        self.pushvalue(w_1)
        self.pushvalue(w_4)
        self.pushvalue(w_3)
        self.pushvalue(w_2)

    def DUP_TOP(self, oparg, next_instr):
        w_1 = self.peekvalue()
        self.pushvalue(w_1)

    def DUP_TOP_TWO(self, oparg, next_instr):
        self.dupvalues(2)

    def DUP_TOPX(self, itemcount, next_instr):
        assert 1 <= itemcount <= 5, "limitation of the current interpreter"
        self.dupvalues(itemcount)

    UNARY_POSITIVE = unaryoperation("pos")
    UNARY_NEGATIVE = unaryoperation("neg")
    UNARY_NOT      = unaryoperation("not_")
    UNARY_CONVERT  = unaryoperation("repr")
    UNARY_INVERT   = unaryoperation("invert")

    def BINARY_POWER(self, oparg, next_instr):
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        w_result = self.space.pow(w_1, w_2, self.space.w_None)
        self.pushvalue(w_result)

    BINARY_MULTIPLY = binaryoperation("mul")
    BINARY_TRUE_DIVIDE  = binaryoperation("truediv")
    BINARY_FLOOR_DIVIDE = binaryoperation("floordiv")
    BINARY_MODULO       = binaryoperation("mod")
    BINARY_MATRIX_MULTIPLY = binaryoperation("matmul")
    BINARY_ADD      = binaryoperation("add")
    BINARY_SUBTRACT = binaryoperation("sub")
    BINARY_SUBSCR   = binaryoperation("getitem")
    BINARY_LSHIFT   = binaryoperation("lshift")
    BINARY_RSHIFT   = binaryoperation("rshift")
    BINARY_AND = binaryoperation("and_")
    BINARY_XOR = binaryoperation("xor")
    BINARY_OR  = binaryoperation("or_")

    def INPLACE_POWER(self, oparg, next_instr):
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        w_result = self.space.inplace_pow(w_1, w_2)
        self.pushvalue(w_result)

    INPLACE_MULTIPLY = binaryoperation("inplace_mul")
    INPLACE_TRUE_DIVIDE  = binaryoperation("inplace_truediv")
    INPLACE_FLOOR_DIVIDE = binaryoperation("inplace_floordiv")
    INPLACE_DIVIDE       = binaryoperation("inplace_div")
    # XXX INPLACE_DIVIDE must fall back to INPLACE_TRUE_DIVIDE with -Qnew
    INPLACE_MODULO       = binaryoperation("inplace_mod")
    INPLACE_MATRIX_MULTIPLY = binaryoperation("inplace_matmul")
    INPLACE_ADD      = binaryoperation("inplace_add")
    INPLACE_SUBTRACT = binaryoperation("inplace_sub")
    INPLACE_LSHIFT   = binaryoperation("inplace_lshift")
    INPLACE_RSHIFT   = binaryoperation("inplace_rshift")
    INPLACE_AND = binaryoperation("inplace_and")
    INPLACE_XOR = binaryoperation("inplace_xor")
    INPLACE_OR  = binaryoperation("inplace_or")

    def STORE_SUBSCR(self, oparg, next_instr):
        "obj[subscr] = newvalue"
        w_subscr = self.popvalue()
        w_obj = self.popvalue()
        w_newvalue = self.popvalue()
        self.space.setitem(w_obj, w_subscr, w_newvalue)

    def DELETE_SUBSCR(self, oparg, next_instr):
        "del obj[subscr]"
        w_subscr = self.popvalue()
        w_obj = self.popvalue()
        self.space.delitem(w_obj, w_subscr)

    def PRINT_EXPR(self, oparg, next_instr):
        w_expr = self.popvalue()
        print_expr(self.space, w_expr)

    def RAISE_VARARGS(self, nbargs, next_instr):
        space = self.space
        if nbargs > 2:
            raise BytecodeCorruption("bad RAISE_VARARGS oparg")
        if nbargs == 0:
            if not self.hide():
                last_operr = self.space.getexecutioncontext().sys_exc_info()
            else:
                last_operr = self.getorcreatedebug().hidden_operationerr
            if last_operr is None:
                raise oefmt(space.w_RuntimeError,
                            "No active exception to reraise")
            # re-raise, no new traceback obj will be attached
            raise RaiseWithExplicitTraceback(last_operr)
        if nbargs == 2:
            w_cause = self.popvalue()
            if space.exception_is_valid_obj_as_class_w(w_cause):
                w_cause = space.call_function(w_cause)
        else:
            w_cause = None
        w_value = self.popvalue()
        if space.exception_is_valid_obj_as_class_w(w_value):
            w_type = w_value
            w_value = space.call_function(w_type)
        else:
            w_type = space.type(w_value)
        operror = OperationError(w_type, w_value)
        operror.normalize_exception(space)
        operror.set_cause(space, w_cause)
        tb = space.getattr(w_value, space.newtext('__traceback__'))
        if not space.is_w(tb, space.w_None):
            operror.set_traceback(tb)
        raise operror

    def LOAD_LOCALS(self, oparg, next_instr):
        self.pushvalue(self.getorcreatedebug().w_locals)

    def exec_(self, w_prog, w_globals, w_locals, w_closure=None):
        """The builtins.exec function."""
        from pypy.interpreter.function import Function
        space = self.space
        ec = space.getexecutioncontext()
        flags = ec.compiler.getcodeflags(self.pycode)
        outer_func = None

        if space.isinstance_w(w_prog, space.gettypeobject(PyCode.typedef)):
            space.audit("exec", [w_prog])
            code = space.interp_w(PyCode, w_prog)
            if code.co_freevars:
                needed = len(code.co_freevars)
                if (space.is_none(w_closure) or
                        not space.isinstance_w(w_closure, space.w_tuple) or
                        not space.len_w(w_closure) == needed):
                    raise oefmt(
                        space.w_TypeError,
                        "code object requires a closure of exactly length %d",
                        needed)
                closure_w = space.fixedview(w_closure, needed)
                outer_func = Function(space, code, closure=closure_w)
            else:
                if not space.is_none(w_closure):
                    raise oefmt(
                        space.w_TypeError,
                        "cannot use a closure with this code object")
        else:
            from pypy.interpreter.astcompiler import consts
            if not space.is_none(w_closure):
                raise oefmt(
                    space.w_TypeError,
                    "closure can only be used when source is a code object")
            flags |= consts.PyCF_SOURCE_IS_UTF8
            source, flags = source_as_str(space, w_prog, 'exec',
                                          "string, bytes or code", flags)
            code = ec.compiler.compile(source, "<string>", 'exec', flags)

        w_globals, w_locals = ensure_ns(space, w_globals, w_locals, 'exec',
                                        self)
        space.call_method(w_globals, 'setdefault', space.newtext('__builtins__'),
                          self.get_builtin())

        code.exec_code(space, w_globals, w_locals, outer_func)

    def POP_EXCEPT(self, oparg, next_instr):
        block = self.pop_block()
        assert isinstance(block, SysExcInfoRestorer)
        block.cleanupstack(self)   # restores ec.sys_exc_operror

    def POP_BLOCK(self, oparg, next_instr):
        self.pop_block()

    def save_and_change_sys_exc_info(self, operationerr):
        ec = self.space.getexecutioncontext()
        last_exception = ec.current_exception()
        block = SysExcInfoRestorer(last_exception, self.lastblock, self.last_instr)
        self.lastblock = block
        if operationerr is not None:   # otherwise, don't change sys_exc_info
            if not self.hide():
                ec.set_sys_exc_info(operationerr)
            else:
                # for hidden frames, a more limited solution should be
                # enough: store away the exception on the frame
                self.getorcreatedebug().hidden_operationerr = operationerr

    @jit.unroll_safe
    def _any_except_or_finally_handler(self):
        block = self.lastblock
        while block is not None:
            if isinstance(block, SysExcInfoRestorer):
                return True
            block = block.previous
        return False

    def LOAD_BUILD_CLASS(self, oparg, next_instr):
        w_build_class = self.get_builtin().getdictvalue(
            self.space, '__build_class__')
        if w_build_class is None:
            raise oefmt(self.space.w_ImportError, "__build_class__ not found")
        self.pushvalue(w_build_class)

    def STORE_NAME(self, varindex, next_instr):
        varname = self.getname_u(varindex)
        w_newvalue = self.popvalue()
        self.space.setitem_str(self.getorcreatedebug().w_locals, varname,
                               w_newvalue)

    def DELETE_NAME(self, varindex, next_instr):
        w_varname = self.getname_w(varindex)
        try:
            self.space.delitem(self.getorcreatedebug().w_locals, w_varname)
        except OperationError as e:
            # catch KeyErrors and turn them into NameErrors
            if not e.match(self.space, self.space.w_KeyError):
                raise
            raise oefmt(self.space.w_NameError,
                        "name %R is not defined", w_varname)

    def UNPACK_SEQUENCE(self, itemcount, next_instr):
        w_iterable = self.popvalue()
        try:
            items = self.space.fixedview_unroll(w_iterable, itemcount)
        except OperationError as e:
            if not e.match(self.space, self.space.w_TypeError):
                raise
            raise oefmt(self.space.w_TypeError,
                    "cannot unpack non-iterable %T object",
                    w_iterable)
        self.pushrevvalues(itemcount, items)

    @jit.unroll_safe
    def UNPACK_EX(self, oparg, next_instr):
        "a, *b, c = range(10)"
        left = oparg & 0xFF
        right = (oparg & 0xFF00) >> 8
        w_iterable = self.popvalue()
        try:
            items = self.space.fixedview(w_iterable)
        except OperationError as e:
            if not e.match(self.space, self.space.w_TypeError):
                raise
            raise oefmt(self.space.w_TypeError,
                    "cannot unpack non-iterable %T object",
                    w_iterable)
        itemcount = len(items)
        count = left + right
        if count > itemcount:
            raise oefmt(self.space.w_ValueError,
                        "not enough values to unpack (expected at least %d, got %d)",
                        count, itemcount)
        right = itemcount - right
        assert right >= 0
        # push values in reverse order
        i = itemcount - 1
        while i >= right:
            self.pushvalue(items[i])
            i -= 1
        self.pushvalue(self.space.newlist(items[left:right]))
        i = left - 1
        while i >= 0:
            self.pushvalue(items[i])
            i -= 1

    def STORE_ATTR(self, nameindex, next_instr):
        "obj.attributename = newvalue"
        w_obj = self.popvalue()
        w_newvalue = self.popvalue()
        if not jit.we_are_jitted():
            from pypy.objspace.std.mapdict import STORE_ATTR_caching
            STORE_ATTR_caching(self.getcode(), w_obj, nameindex, w_newvalue)
        else:
            w_attributename = self.getname_w(nameindex)
            self.space.setattr(w_obj, w_attributename, w_newvalue)

    def DELETE_ATTR(self, nameindex, next_instr):
        "del obj.attributename"
        w_attributename = self.getname_w(nameindex)
        w_obj = self.popvalue()
        self.space.delattr(w_obj, w_attributename)

    def STORE_GLOBAL(self, nameindex, next_instr):
        #varname = self.getname_u(nameindex)
        #w_newvalue = self.popvalue()
        #self.space.setitem_str(self.get_w_globals(), varname, w_newvalue)
        from pypy.objspace.std.celldict import STORE_GLOBAL_cached
        STORE_GLOBAL_cached(self, nameindex, next_instr)

    def DELETE_GLOBAL(self, nameindex, next_instr):
        w_varname = self.getname_w(nameindex)
        self.space.delitem(self.get_w_globals(), w_varname)

    def LOAD_NAME(self, nameindex, next_instr):
        from pypy.objspace.std.celldict import LOAD_GLOBAL_cached
        if self.getorcreatedebug().w_locals is not self.get_w_globals():
            w_varname = self.getname_w(nameindex)
            varname = self.space.text_w(w_varname)
            w_value = self.space.finditem_str(self.getorcreatedebug().w_locals,
                                              varname)
            if w_value is not None:
                self.pushvalue(w_value)
                return
        LOAD_GLOBAL_cached(self, nameindex, next_instr)

    @always_inline
    def _load_global(self, varname):
        w_value = self.space.finditem_str(self.get_w_globals(), varname)
        if w_value is None:
            # not in the globals, now look in the built-ins
            w_value = self.get_builtin().getdictvalue(self.space, varname)
            if w_value is None:
                # XXX re-wrapping
                w_varname = self.space.newtext(varname)
                self._load_global_failed(w_varname)
        return w_value

    @dont_inline
    def _load_global_failed(self, w_varname):
        # CPython Issue #17032: The "global" in the "NameError: global
        # name 'x' is not defined" error message has been removed.
        raise oefmt_name_error(self.space, w_varname,
                    "name %R is not defined")

    @always_inline
    def LOAD_GLOBAL(self, nameindex, next_instr):
        from pypy.objspace.std.celldict import LOAD_GLOBAL_cached
        LOAD_GLOBAL_cached(self, nameindex, next_instr)

    def DELETE_FAST(self, varindex, next_instr):
        if self.locals_cells_stack_w[varindex] is None:
            varname = self.getlocalvarname(varindex)
            raise oefmt(self.space.w_UnboundLocalError,
                        "local variable '%s' referenced before assignment",
                        varname)
        self.locals_cells_stack_w[varindex] = None

    def SETUP_ANNOTATIONS(self, oparg, next_instr):
        w_locals = self.getorcreatedebug().w_locals
        if not self.space.finditem_str(w_locals, '__annotations__'):
            w_annotations = self.space.newdict()
            self.space.setitem_str(w_locals, '__annotations__', w_annotations)

    def BUILD_TUPLE(self, itemcount, next_instr):
        items = self.popvalues(itemcount)
        w_tuple = self.space.newtuple(items)
        self.pushvalue(w_tuple)

    def BUILD_LIST(self, itemcount, next_instr):
        items = self.popvalues_mutable(itemcount)
        w_list = self.space.newlist(items)
        self.pushvalue(w_list)

    def BUILD_LIST_FROM_ARG(self, _, next_instr):
        space = self.space
        # this is a little dance, because list has to be before the
        # value
        last_val = self.popvalue()
        length_hint = 0
        try:
            length_hint = space.length_hint(last_val, length_hint)
        except OperationError as e:
            if e.async(space):
                raise
        self.pushvalue(space.newlist([], sizehint=length_hint))
        self.pushvalue(last_val)

    @always_inline
    def LOAD_ATTR(self, nameindex, next_instr):
        "obj.attributename"
        w_obj = self.popvalue()
        if not jit.we_are_jitted():
            from pypy.objspace.std.mapdict import LOAD_ATTR_caching
            w_value = LOAD_ATTR_caching(self.getcode(), w_obj, nameindex)
        else:
            w_attributename = self.getname_w(nameindex)
            w_value = self.space.getattr(w_obj, w_attributename)
        self.pushvalue(w_value)

    @jit.unroll_safe
    def cmp_exc_match(self, w_1, w_2):
        space = self.space
        if space.isinstance_w(w_2, space.w_tuple):
            for w_type in space.fixedview(w_2):
                if not space.exception_is_valid_class_w(w_type):
                    raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG)
        elif not space.exception_is_valid_class_w(w_2):
            raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG)
        return space.exception_match(space.type(w_1), w_2)

    def COMPARE_OP(self, testnum, next_instr):
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        if testnum == 0:
            w_result = self.space.lt(w_1, w_2)
        elif testnum == 1:
            w_result = self.space.le(w_1, w_2)
        elif testnum == 2:
            w_result = self.space.eq(w_1, w_2)
        elif testnum == 3:
            w_result = self.space.ne(w_1, w_2)
        elif testnum == 4:
            w_result = self.space.gt(w_1, w_2)
        elif testnum == 5:
            w_result = self.space.ge(w_1, w_2)
        else:
            raise BytecodeCorruption
        self.pushvalue(w_result)

    def IS_OP(self, oparg, next_instr):
        space = self.space
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        res = self.space.is_w(w_1, w_2)
        if oparg:
            if res:
                w_res = space.w_False
            else:
                w_res = space.w_True
        else:
            if res:
                w_res = space.w_True
            else:
                w_res = space.w_False
        self.pushvalue(w_res)

    def CONTAINS_OP(self, oparg, next_instr):
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        res = self.space.contains_w(w_2, w_1) ^ oparg
        self.pushvalue(self.space.newbool(bool(res)))

    def JUMP_IF_NOT_EXC_MATCH(self, target, next_instr):
        w_2 = self.popvalue()
        w_1 = self.popvalue()
        res = self.cmp_exc_match(w_1, w_2)
        if res:
            return next_instr
        else:
            return target * 2

    def IMPORT_NAME(self, nameindex, next_instr):
        space = self.space
        w_modulename = self.getname_w(nameindex)
        w_fromlist = self.popvalue()
        w_flag = self.popvalue()

        w_import = self.get_builtin().getdictvalue(space, '__import__')
        if w_import is None:
            raise oefmt(space.w_ImportError, "__import__ not found")
        d = self.getdebug()
        if d is None:
            w_locals = None
        else:
            w_locals = d.w_locals
        if w_locals is None:            # CPython does this
            w_locals = space.w_None
        w_globals = self.get_w_globals()

        w_obj = space.call_function(w_import, w_modulename, w_globals,
                w_locals, w_fromlist, w_flag)

        self.pushvalue(w_obj)

    def IMPORT_STAR(self, oparg, next_instr):
        w_module = self.popvalue()
        w_locals = self.getdictscope()
        import_all_from(self.space, w_module, w_locals)
        self.setdictscope(w_locals)

    def IMPORT_FROM(self, nameindex, next_instr):
        w_name = self.getname_w(nameindex)
        w_module = self.peekvalue()
        self.pushvalue(self.import_from(w_module, w_name))

    def import_from(self, w_module, w_name):
        space = self.space
        try:
            return space.getattr(w_module, w_name)
        except OperationError as e:
            if not e.match(space, space.w_AttributeError):
                raise

        w_pkgname = space.newtext("<unknown module name>")
        try:
            w_pkgname = space.getattr(
                w_module, space.newtext('__name__'))
            w_fullname = space.newtext(b'%s.%s' %
                (space.utf8_w(w_pkgname), space.utf8_w(w_name)))
            return space.getitem(space.sys.get('modules'), w_fullname)
        except OperationError:
            from pypy.module.imp.importing import (is_spec_initializing,
                get_path, get_spec, is_spec_uninitialized_submodule)

            w_pkgpath = get_path(space, w_module)
            args = (space.utf8_w(w_name), space.utf8_w(w_pkgname), space.utf8_w(w_pkgpath))
            w_spec = get_spec(space, w_module)
            if is_spec_initializing(space, w_spec):
                msg = (
                    "cannot import name '%s' from partially initialized module '%s' "
                    "(most likely due to a circular import) (%s)" % args
                )
            elif is_spec_uninitialized_submodule(space, w_spec, w_pkgname):
                msg = (
                    "cannot access submodule '%s' of module '%s' "
                    "(most likely due to a circular import)" % (args[1], args[0])
                )
            else:
                msg = "cannot import name '%s' from '%s' (%s)" % args

            raise_import_error(
                space,
                space.newtext(msg),
                w_pkgname,
                w_pkgpath,
            )

    def YIELD_VALUE(self, oparg, next_instr):
        if self.getcode().co_flags & pycode.CO_ASYNC_GENERATOR:
            from pypy.interpreter.generator import AsyncGenValueWrapper
            w_value = self.popvalue()
            w_value = AsyncGenValueWrapper(w_value)
            self.pushvalue(w_value)
        raise Yield

    def next_yield_from(self, w_yf, w_inputvalue_or_err):
        """Fetch the next item of the current 'yield from', push it on
        the frame stack, and raises Yield.  If there isn't one, push
        w_stopiteration_value and returns.  May also just raise.
        """
        from pypy.interpreter.generator import (
            GeneratorOrCoroutine, AsyncGenASend)
        space = self.space
        try:
            if isinstance(w_yf, GeneratorOrCoroutine):
                w_retval = w_yf.send_ex(w_inputvalue_or_err)
            elif isinstance(w_yf, AsyncGenASend):   # performance only
                w_retval = w_yf.do_send(w_inputvalue_or_err)
            elif space.is_w(w_inputvalue_or_err, space.w_None):
                w_retval = space.next(w_yf)
            else:
                w_retval = delegate_to_nongen(space, w_yf, w_inputvalue_or_err)
        except OperationError as e:
            if not e.match(space, space.w_StopIteration):
                raise
            self._report_stopiteration_sometimes(w_yf, e)
            try:
                w_stop_value = space.getattr(e.get_w_value(space),
                                             space.newtext("value"))
            except OperationError as e:
                if not e.match(space, space.w_AttributeError):
                    raise
                w_stop_value = space.w_None
            self.pushvalue(w_stop_value)
            return
        else:
            self.pushvalue(w_retval)
            self.w_yielding_from = w_yf
            raise Yield

    def YIELD_FROM(self, oparg, next_instr):
        # Unlike CPython, we handle this not by repeating the same
        # bytecode over and over until the inner iterator is exhausted.
        # Instead, we set w_yielding_from.
        # This asks resume_execute_frame() to exhaust that
        # sub-iterable first before continuing on the next bytecode.
        w_inputvalue = self.popvalue()    # that's always w_None, actually
        w_gen = self.popvalue()
        #
        self.next_yield_from(w_gen, w_inputvalue)
        # Common case: the call above raises Yield.
        # If instead the iterable is empty, next_yield_from() pushed the
        # final result and returns.  In that case, we can just continue
        # with the next bytecode.

    def _revdb_jump_backward(self, jumpto):
        # moved in its own function for the import statement
        from pypy.interpreter.reverse_debugging import jump_backward
        jump_backward(self, jumpto)

    def jump_absolute(self, jumpto, next_instr, ec):
        # this function is overridden by pypy.module.pypyjit.interp_jit
        check_nonneg(jumpto)
        if self.space.reverse_debugging:
            self._revdb_jump_backward(jumpto * 2)
        return jumpto * 2

    def JUMP_FORWARD(self, jumpby, next_instr):
        next_instr += jumpby * 2
        return next_instr

    def POP_JUMP_IF_FALSE(self, target, next_instr, ec):
        w_value = self.popvalue()
        if not self.space.is_true(w_value):
            return self.jump_absolute(target, next_instr, ec)
        return next_instr

    def POP_JUMP_IF_TRUE(self, target, next_instr, ec):
        w_value = self.popvalue()
        if self.space.is_true(w_value):
            return self.jump_absolute(target, next_instr, ec)
        return next_instr

    def JUMP_IF_FALSE_OR_POP(self, target, next_instr, ec):
        w_value = self.peekvalue()
        if not self.space.is_true(w_value):
            return self.jump_absolute(target, next_instr, ec)
        self.popvalue()
        return next_instr

    def JUMP_IF_TRUE_OR_POP(self, target, next_instr, ec):
        w_value = self.peekvalue()
        if self.space.is_true(w_value):
            return self.jump_absolute(target, next_instr, ec)
        self.popvalue()
        return next_instr

    def GET_ITER(self, oparg, next_instr):
        w_iterable = self.popvalue()
        w_iterator = self.space.iter(w_iterable)
        self.pushvalue(w_iterator)

    def FOR_ITER(self, jumpby, next_instr):
        w_iterator = self.peekvalue()
        try:
            w_nextitem = self.space.next(w_iterator)
        except OperationError as e:
            if not e.match(self.space, self.space.w_StopIteration):
                raise
            # iterator exhausted
            self._report_stopiteration_sometimes(w_iterator, e)
            self.popvalue()
            next_instr += jumpby * 2
        else:
            self.pushvalue(w_nextitem)
        return next_instr

    def _report_stopiteration_sometimes(self, w_iterator, operr):
        # CPython 3.5 calls the exception trace in an ill-defined subset
        # of cases: only if tp_iternext returned NULL and set a
        # StopIteration exception, but not if tp_iternext returned NULL
        # *without* setting an exception.  We can't easily emulate that
        # behavior at this point.  For example, the generator's
        # tp_iternext uses one or other case depending on whether the
        # generator is already exhausted or just exhausted now.  We'll
        # classify that as a CPython incompatibility and use an
        # approximative rule: if w_iterator is a generator-iterator,
        # we always report it; if operr has already a stack trace
        # attached (likely from a custom __iter__() method), we also
        # report it; in other cases, we don't.
        from pypy.interpreter.generator import GeneratorOrCoroutine, AsyncGenASend
        if (isinstance(w_iterator, GeneratorOrCoroutine) or
                isinstance(w_iterator, AsyncGenASend) or
                operr.has_any_traceback()):
            self.space.getexecutioncontext().exception_trace(self, operr)

    def SETUP_EXCEPT(self, offsettoend, next_instr):
        block = ExceptBlock(self.valuestackdepth,
                            next_instr + offsettoend * 2, self.lastblock)
        self.lastblock = block

    def SETUP_FINALLY(self, offsettoend, next_instr):
        block = FinallyBlock(self.valuestackdepth,
                             next_instr + offsettoend * 2, self.lastblock)
        self.lastblock = block

    def SETUP_WITH(self, offsettoend, next_instr):
        w_manager = self.peekvalue()
        w_enter = self.space.lookup(w_manager, "__enter__")
        w_descr = self.space.lookup(w_manager, "__exit__")
        if w_enter is None or w_descr is None:
            raise oefmt(self.space.w_TypeError,
                        "'%T' object does not support the context manager "
                        "protocol (no __enter__/__exit__ method)", w_manager)
        w_exit = self.space.get(w_descr, w_manager)
        self.settopvalue(w_exit)
        w_result = self.space.get_and_call_function(w_enter, w_manager)
        block = FinallyBlock(self.valuestackdepth,
                             next_instr + offsettoend * 2, self.lastblock)
        self.lastblock = block
        self.pushvalue(w_result)

    def WITH_EXCEPT_START(self, oparg, next_instr):
        w_unroller = self.popvalue()
        w_exitfunc = self.popvalue()
        self.pushvalue(w_unroller)
        if isinstance(w_unroller, SApplicationException):
            operr = w_unroller.operr
            w_traceback = operr.get_w_traceback(self.space)
            w_res = self.call_contextmanager_exit_function(
                w_exitfunc,
                operr.w_type,
                operr.get_w_value(self.space),
                w_traceback)
        else:
            assert 0
        self.pushvalue(w_res)

    def RERAISE(self, reset_last_instr, next_instr):
        unroller = self.popvalue()
        if not isinstance(unroller, SApplicationException):
            assert 0
        if reset_last_instr:
            block = self.lastblock
            assert isinstance(block, SysExcInfoRestorer)
            self.last_instr = block.last_instr
        block = self.unrollstack()
        if block is None:
            w_result = unroller.reraise()
            assert 0, "unreachable"
        else:
            next_instr = block.handle(self, unroller)
        return next_instr

    def CALL_FUNCTION(self, oparg, next_instr):
        # Only positional arguments
        nargs = oparg & 0xff
        w_function = self.peekvalue(nargs)
        w_result = self.space.call_valuestack(w_function, nargs, self, dropvalues=nargs+1)
        self.pushvalue(w_result)

    @jit.unroll_safe
    def CALL_FUNCTION_KW(self, n_arguments, next_instr):
        from pypy.objspace.std.tupleobject import W_AbstractTupleObject
        space = self.space
        # like in BUILD_CONST_KEY_MAP we can't use space.fixedview because then
        # the immutability of the tuple is lost
        w_tup_varnames = space.interp_w(W_AbstractTupleObject, self.popvalue())
        n_keywords = space.len_w(w_tup_varnames)
        keyword_names_w = [None] * n_keywords
        keywords_w = [None] * n_keywords
        for i in range(n_keywords):
            keyword_names_w[i] = w_tup_varnames.getitem(space, i)
            w_value = self.peekvalue(n_keywords - 1 - i)
            keywords_w[i] = w_value
        self.dropvalues(n_keywords)
        n_arguments -= n_keywords
        arguments = self.popvalues(n_arguments)
        w_function  = self.popvalue()
        args = self.argument_factory(arguments, keyword_names_w, keywords_w, None, None,
                                     w_function=w_function)
        if self.get_is_being_profiled() and function.is_builtin_code(w_function):
            w_result = self.space.call_args_and_c_profile(self, w_function,
                                                          args)
        else:
            w_result = self.space.call_args(w_function, args)
        self.pushvalue(w_result)

    def CALL_FUNCTION_EX(self, has_kwarg, next_instr):
        w_kwargs = None
        if has_kwarg:
            w_kwargs = self.popvalue()
        w_args = self.popvalue()
        w_function = self.popvalue()
        args = self.argument_factory(
            [], None, None, w_star=w_args, w_starstar=w_kwargs, w_function=w_function)
        if self.get_is_being_profiled() and function.is_builtin_code(w_function):
            w_result = self.space.call_args_and_c_profile(self, w_function,
                                                          args)
        else:
            w_result = self.space.call_args(w_function, args)
        self.pushvalue(w_result)

    @jit.unroll_safe
    def MAKE_FUNCTION(self, oparg, next_instr):
        space = self.space
        w_codeobj = self.popvalue()
        codeobj = self.space.interp_w(PyCode, w_codeobj)
        assert 0 <= oparg <= 0x0F
        if oparg & 0x08:
            w_freevarstuple = self.popvalue()
            # XXX this list copy is expensive, it's purely for the annotator
            freevars = [self.space.interp_w(Cell, cell)
                        for cell in self.space.fixedview(w_freevarstuple)]
        else:
            freevars = None
        if oparg & 0x04:
            w_ann = self.popvalue()
        else:
            w_ann = None
        if oparg & 0x02:
            w_kw_defs = self.popvalue()
            # XXX
            kw_defs_w = [space.unpackiterable(w_tup)
                            for w_tup in space.fixedview(
                                space.call_method(w_kw_defs, 'items'))]
        else:
            kw_defs_w = None
        if oparg & 0x01:
            defaultarguments = space.fixedview(self.popvalue())
        else:
            defaultarguments = []

        fn = function.Function(space, codeobj, self.get_w_globals(),
                               defaultarguments,
                               kw_defs_w, freevars, w_ann,
                               qualname=codeobj.co_qualname)
        self.pushvalue(fn)

    def BUILD_SLICE(self, numargs, next_instr):
        if numargs == 3:
            w_step = self.popvalue()
        elif numargs == 2:
            w_step = self.space.w_None
        else:
            raise BytecodeCorruption
        w_end = self.popvalue()
        w_start = self.popvalue()
        w_slice = self.space.newslice(w_start, w_end, w_step)
        self.pushvalue(w_slice)

    def LIST_APPEND(self, oparg, next_instr):
        w = self.popvalue()
        v = self.peekvalue(oparg - 1)
        self.space.call_method(v, 'append', w)

    def LIST_EXTEND(self, oparg, next_instr):
        w = self.popvalue()
        v = self.peekvalue(oparg - 1)
        space = self.space
        try:
            space.call_method(v, 'extend', w)
        except OperationError as e:
            if space.is_iterable(w):
                raise
            raise oefmt(space.w_TypeError,
                        "Value after * must be an iterable, not %T",
                        w)

    def LIST_TO_TUPLE(self, oparg, next_instr):
        w_l = self.popvalue()
        w_res = self.space.call_function(self.space.w_tuple, w_l)
        self.pushvalue(w_res)

    def SET_ADD(self, oparg, next_instr):
        w_value = self.popvalue()
        w_set = self.peekvalue(oparg - 1)
        self.space.call_method(w_set, 'add', w_value)

    def SET_UPDATE(self, oparg, next_instr):
        w_update = self.popvalue()
        w_set = self.peekvalue(oparg - 1)
        self.space.call_method(w_set, 'update', w_update)

    def MAP_ADD(self, oparg, next_instr):
        w_value = self.popvalue()
        w_key = self.popvalue()
        w_dict = self.peekvalue(oparg - 1)
        self.space.setitem(w_dict, w_key, w_value)

    def DICT_MERGE(self, oparg, next_instr):
        from pypy.objspace.std.dictmultiobject import W_DictMultiObject
        space = self.space
        w_dict = self.peekvalue(1)
        w_item = self.popvalue()
        # dict merge happens always in places where the function is on the
        # stack too
        w_function = self.peekvalue(oparg+2)
        _dict_merge(space, w_dict, w_item, w_function)

    def DICT_UPDATE(self, oparg, next_instr):
        w_item = self.popvalue()
        space = self.space
        if not space.ismapping_w(w_item):
            raise oefmt(space.w_TypeError,
                        "'%T' object is not a mapping",
                        w_item)
        w_dict = self.peekvalue()
        space.call_method(w_dict, "update", w_item)

    # overridden by faster version in the standard object space.
    LOAD_METHOD = LOAD_ATTR
    CALL_METHOD = CALL_FUNCTION
    CALL_METHOD_KW = CALL_FUNCTION_KW

    def MISSING_OPCODE(self, oparg, next_instr):
        ofs = self.last_instr
        c = self.pycode.co_code[ofs]
        name = self.pycode.co_name
        raise BytecodeCorruption("unknown opcode, ofs=%d, code=%d, name=%s" %
                                 (ofs, ord(c), name) )

    @jit.unroll_safe
    def BUILD_MAP(self, itemcount, next_instr):
        w_dict = self.space.newdict()
        for i in range(itemcount-1, -1, -1):
            w_value = self.peekvalue(2 * i)
            w_key = self.peekvalue(2 * i + 1)
            self.space.setitem(w_dict, w_key, w_value)
        self.dropvalues(2 * itemcount)
        self.pushvalue(w_dict)

    @jit.unroll_safe
    def BUILD_CONST_KEY_MAP(self, itemcount, next_instr):
        from pypy.objspace.std.tupleobject import W_AbstractTupleObject
        # the reason why we don't use space.fixedview here is that then the
        # immutability of the tuple would not propagate into the loop below in
        # the JIT
        w_keys = self.space.interp_w(W_AbstractTupleObject, self.popvalue())
        w_dict = self.space.newdict()
        for i in range(itemcount):
            w_value = self.peekvalue(itemcount - 1 - i)
            w_key = w_keys.getitem(self.space, i)
            self.space.setitem(w_dict, w_key, w_value)
        self.dropvalues(itemcount)
        self.pushvalue(w_dict)

    @jit.unroll_safe
    def BUILD_SET(self, itemcount, next_instr):
        w_set = self.space.newset()
        for i in range(itemcount-1, -1, -1):
            w_item = self.peekvalue(i)
            self.space.call_method(w_set, 'add', w_item)
        self.dropvalues(itemcount)
        self.pushvalue(w_set)

    def GET_YIELD_FROM_ITER(self, oparg, next_instr):
        from pypy.interpreter.astcompiler import consts
        from pypy.interpreter.generator import GeneratorIterator, Coroutine
        w_iterable = self.peekvalue()
        if isinstance(w_iterable, Coroutine):
            if not self.pycode.co_flags & (consts.CO_COROUTINE |
                                       consts.CO_ITERABLE_COROUTINE):
                #'iterable' coroutine is used in a 'yield from' expression
                #of a regular generator
                raise oefmt(self.space.w_TypeError,
                            "cannot 'yield from' a coroutine object "
                            "in a non-coroutine generator")
        elif not isinstance(w_iterable, GeneratorIterator):
            w_iterator = self.space.iter(w_iterable)
            self.settopvalue(w_iterator)

    def LOAD_ASSERTION_ERROR(self, oparg, next_instr):
        self.pushvalue(self.space.w_AssertionError)

    def GET_AWAITABLE(self, oparg, next_instr):
        from pypy.interpreter.generator import get_awaitable_iter
        from pypy.interpreter.generator import Coroutine
        w_iterable = self.popvalue()
        w_iter = get_awaitable_iter(self.space, w_iterable)
        if isinstance(w_iter, Coroutine):
            if w_iter.get_delegate() is not None:
                # 'w_iter' is a coroutine object that is being awaited,
                # '.w_yielded_from' is the current awaitable being awaited on.
                raise oefmt(self.space.w_RuntimeError,
                            "coroutine is being awaited already")
        self.pushvalue(w_iter)

    def SETUP_ASYNC_WITH(self, offsettoend, next_instr):
        res = self.popvalue()
        block = FinallyBlock(self.valuestackdepth,
                             next_instr + offsettoend * 2, self.lastblock)
        self.lastblock = block
        self.pushvalue(res)

    def BEFORE_ASYNC_WITH(self, oparg, next_instr):
        space = self.space
        w_manager = self.peekvalue()
        w_enter = space.lookup(w_manager, "__aenter__")
        w_descr = space.lookup(w_manager, "__aexit__")
        if w_enter is None or w_descr is None:
            raise oefmt(space.w_TypeError,
                        "'%T' object does not support the asynchronous context "
                        "manager protocol (no __aenter__/__aexit__ method)",
                        w_manager)
        w_exit = space.get(w_descr, w_manager)
        self.settopvalue(w_exit)
        w_result = space.get_and_call_function(w_enter, w_manager)
        self.pushvalue(w_result)

    def GET_AITER(self, oparg, next_instr):
        from pypy.interpreter.generator import AIterWrapper, get_awaitable_iter

        space = self.space
        w_obj = self.popvalue()
        w_func = space.lookup(w_obj, "__aiter__")
        if w_func is None:
            raise oefmt(space.w_TypeError,
                        "'async for' requires an object with "
                        "__aiter__ method, got %T",
                        w_obj)
        w_iter = space.get_and_call_function(w_func, w_obj)

        # If __aiter__() returns an object with a __anext__() method,
        # wrap it in a awaitable that resolves to 'w_iter'.
        if space.lookup(w_iter, "__anext__") is not None:
            self.pushvalue(w_iter)
        else:
            new_error = oefmt(space.w_TypeError,
                        "'async for' received a object from __aiter__ that"
                        " does not implement __anext__: %T", w_iter)
            raise new_error

    def GET_ANEXT(self, oparg, next_instr):
        from pypy.interpreter.generator import get_awaitable_iter

        # XXX add performance shortcut if w_aiter is an AsyncGenerator
        space = self.space
        w_aiter = self.peekvalue()
        w_func = space.lookup(w_aiter, "__anext__")
        if w_func is None:
            raise oefmt(space.w_TypeError,
                        "'async for' requires an iterator with "
                        "__anext__ method, got %T",
                        w_aiter)
        w_next_iter = space.get_and_call_function(w_func, w_aiter)
        try:
            w_awaitable = get_awaitable_iter(space, w_next_iter)
        except OperationError as e:
            if e.async(space):
                raise
            new_error = oefmt(space.w_TypeError,
                        "'async for' received an invalid object "
                        "from __anext__: %T", w_next_iter)
            w_cause = e.normalize_exception(space)
            new_error.normalize_exception(space)
            new_error.set_cause(space, w_cause)
            raise new_error
        self.pushvalue(w_awaitable)

    def END_ASYNC_FOR(self, oparg, next_instr):
        block = self.pop_block()
        assert isinstance(block, SysExcInfoRestorer)
        block.cleanupstack(self)   # restores ec.sys_exc_operror

        w_exc = self.popvalue()
        if self.space.exception_match(self.space.type(w_exc), self.space.w_StopAsyncIteration):
            self.popvalue() # unroller
            self.popvalue() # aiter
            return next_instr
        else:
            unroller = self.peekvalue(0)
            if not isinstance(unroller, SApplicationException):
                raise oefmt(self.space.w_RuntimeError,
                        "END_ASYNC_FOR found no exception")
            block = self.unrollstack()
            if block is None:
                w_result = unroller.reraise()
                assert 0, "unreachable"
            else:
                next_instr = block.handle(self, unroller)
        return next_instr

    def FORMAT_VALUE(self, oparg, next_instr):
        from pypy.interpreter.astcompiler import consts
        space = self.space
        #
        if (oparg & consts.FVS_MASK) == consts.FVS_HAVE_SPEC:
            w_spec = self.popvalue()
        else:
            w_spec = space.newtext('')
        w_value = self.popvalue()
        #
        conversion = oparg & consts.FVC_MASK
        if conversion == consts.FVC_STR:
            w_value = space.str(w_value)
        elif conversion == consts.FVC_REPR:
            w_value = space.repr(w_value)
        elif conversion == consts.FVC_ASCII:
            from pypy.objspace.std.unicodeobject import ascii_from_object
            w_value = ascii_from_object(space, w_value)
        #
        w_res = space.format(w_value, w_spec)
        self.pushvalue(w_res)

    @jit.unroll_safe
    def BUILD_STRING(self, itemcount, next_instr):
        from rpython.rlib import rutf8
        space = self.space
        builder = rutf8.Utf8StringBuilder()
        for i in range(itemcount-1, -1, -1):
            w_item = self.peekvalue(i)
            utf8, length = space.utf8_len_w(w_item)
            builder.append_utf8(utf8, length)
        self.dropvalues(itemcount)
        w_res = space.newutf8(builder.build(), builder.getlength())
        self.pushvalue(w_res)

    def _revdb_load_var(self, oparg):
        # moved in its own function for the import statement
        from pypy.interpreter.reverse_debugging import load_metavar
        w_var = load_metavar(oparg)
        self.pushvalue(w_var)

    def LOAD_REVDB_VAR(self, oparg, next_instr):
        if self.space.reverse_debugging:
            self._revdb_load_var(oparg)
        else:
            self.MISSING_OPCODE(oparg, next_instr)

    def MATCH_SEQUENCE(self, oparg, next_instr):
        w_sequence = self.peekvalue()
        space = self.space
        flag_patma_collection = space.type(w_sequence).flag_patma_collection
        if flag_patma_collection == "M": # a mapping!
            is_sequence = False
        else:
            is_sequence = flag_patma_collection == "S"
            if not is_sequence:
                is_sequence = space.issequence_w(w_sequence)

            is_sequence = (is_sequence and not
                    space.isinstance_w(w_sequence, space.w_unicode) and not
                    space.isinstance_w(w_sequence, space.w_bytes) and not
                    space.isinstance_w(w_sequence, space.w_bytearray))
        self.pushvalue(space.newbool(is_sequence))

    def MATCH_MAPPING(self, oparg, next_instr):
        w_mapping = self.peekvalue()
        flag_patma_collection = self.space.type(w_mapping).flag_patma_collection
        if flag_patma_collection == "S": # sequence
            is_mapping = False
        else:
            is_mapping = flag_patma_collection == "M"
            if not is_mapping:
                is_mapping = self.space.ismapping_w(w_mapping)
        self.pushvalue(self.space.newbool(is_mapping))

    def MATCH_CLASS(self, oparg, next_instr):
        space = self.space
        nargs = oparg
        w_names = self.popvalue()
        w_type = self.popvalue()
        w_subject = self.popvalue()

        w_attribute_tuple = _match_class(self.space, nargs, w_names, w_type, w_subject)
        if w_attribute_tuple is None:
            self.pushvalue(space.w_None)
            self.pushvalue(space.w_False)
        else:
            self.pushvalue(w_attribute_tuple)
            self.pushvalue(space.w_True)


    def GET_LEN(self, oparg, next_instr):
        w_sequence = self.peekvalue()
        w_len = self.space.len(w_sequence)
        self.pushvalue(w_len)

    @jit.unroll_safe
    def MATCH_KEYS(self, oparg, next_instr):
        w_keys = self.peekvalue()
        w_dict = self.peekvalue(1)
        length = self.space.len_w(w_keys)
        values_w = [None] * length
        w_seen = self.space.newset()
        w_iter = self.space.iter(w_keys)
        w_sentinel = self.space.call_method(self.space.builtin, "object")
        try:
            i = 0
            while True:
                w_key = self.space.next(w_iter)
                if self.space.contains_w(w_seen, w_key):
                    raise oefmt(self.space.w_ValueError, "mapping pattern checks duplicate key (%R)", w_key)
                self.space.call_method(w_seen, "add", w_key)
                w_value = self.space.call_method(w_dict, 'get', w_key, w_sentinel)
                if not self.space.is_w(w_value, w_sentinel):
                    values_w[i] = w_value
                else:
                    self.pushvalue(self.space.w_None)
                    self.pushvalue(self.space.w_False)
                    return
                i += 1
        except OperationError as e:
            if not e.match(self.space, self.space.w_StopIteration):
                raise

        self.pushvalue(self.space.newtuple(values_w))
        self.pushvalue(self.space.w_True)

    def COPY_DICT_WITHOUT_KEYS(self, oparg, next_instr):
        w_keys = self.popvalue()
        w_subject = self.peekvalue()
        w_dict = _copy_dict_without_keys(self.space, w_keys, w_subject)
        self.pushvalue(w_dict)

    @jit.unroll_safe
    def ROT_N(self, oparg, next_instr):
        w_top = self.peekvalue()
        for i in range(oparg - 1):
            self.settopvalue(self.peekvalue(i + 1), i)
        self.settopvalue(w_top, oparg - 1)

    def CHECK_EG_MATCH(self, oparg, next_instr):
        space = self.space
        w_typ = self.popvalue()
        check_except_star_type_valid(space, w_typ)
        w_eg = self.peekvalue()
        w_match, w_rest = exception_group_match(space, w_eg, w_typ)
        if space.is_w(w_match, space.w_None):
            self.pushvalue(w_match)
        else:
            self.settopvalue(w_rest)
            self.pushvalue(w_match)
            ec = space.getexecutioncontext()
            ec.set_sys_exc_info3(w_match)

    def PREP_RERAISE_STAR(self, oparg, next_instr):
        space = self.space
        w_res = self.popvalue()
        w_orig = self.popvalue()
        w_mod = space.call_method(space.builtin, '__import__', space.newtext('__exceptions__'))
        w_eg_or_None = space.call_method(w_mod, "_prep_reraise_star", w_orig, w_res)
        if space.is_w(w_eg_or_None, space.w_None):
            w_push = space.w_None
        else:
            w_push = SApplicationException(OperationError(space.type(w_eg_or_None), w_eg_or_None))
        self.pushvalue(w_push)


def delegate_to_nongen(space, w_yf, w_inputvalue_or_err):
    # invoke a "send" or "throw" by method name to a non-generator w_yf
    if isinstance(w_inputvalue_or_err, SApplicationException):
        operr = w_inputvalue_or_err.operr
        try:
            w_meth = space.getattr(w_yf, space.newtext("throw"))
        except OperationError as e:
            if not e.match(space, space.w_AttributeError):
                raise
            raise operr
        # bah, CPython calls here with the exact same arguments as
        # originally passed to throw().  In our case it is far removed.
        # Let's hope nobody will complain...
        w_val = operr.normalize_exception(space)
        w_exc = operr.w_type
        w_tb = operr.get_w_traceback(space)
        return space.call_function(w_meth, w_exc, w_val, w_tb)
    else:
        return space.call_method(w_yf, "send", w_inputvalue_or_err)


### ____________________________________________________________ ###

class ExitFrame(Exception):
    pass


class Return(ExitFrame):
    """Raised when exiting a frame via a 'return' statement."""


class Yield(ExitFrame):
    """Raised when exiting a frame via a 'yield' statement."""


class RaiseWithExplicitTraceback(Exception):
    """Raised at interp-level by a 0-argument 'raise' statement."""
    def __init__(self, operr):
        self.operr = operr


### Frame Blocks ###

class SApplicationException(W_Root):
    """Signals an application-level exception
    (i.e. an OperationException)."""
    _immutable_ = True
    def __init__(self, operr):
        self.operr = operr
    def reraise(self):
        raise RaiseWithExplicitTraceback(self.operr)


class FrameBlock(object):
    """Abstract base class for frame blocks from the blockstack,
    used by the SETUP_XXX and POP_BLOCK opcodes."""

    _immutable_ = True

    def __init__(self, valuestackdepth, handlerposition, previous):
        self.handlerposition = handlerposition
        self.valuestackdepth = valuestackdepth
        self.previous = previous   # this makes a linked list of blocks

    def cleanupstack(self, frame):
        frame.dropvaluesuntil(self.valuestackdepth)

    # internal pickling interface, not using the standard protocol
    def _get_state_(self, space):
        return space.newtuple([space.newtext(self._opname), space.newint(self.handlerposition),
                               space.newint(self.valuestackdepth)])

    def handle(self, frame, unroller):
        """ Purely abstract method
        """
        raise NotImplementedError


class SysExcInfoRestorer(FrameBlock):
    """
    This is a special, implicit block type which is created when entering a
    finally or except handler. It does not belong to any opcode
    """

    _immutable_ = True
    _opname = 'SYS_EXC_INFO_RESTORER' # it's not associated to any opcode

    def __init__(self, operr, previous, last_instr):
        self.operr = operr
        self.previous = previous
        self.last_instr = last_instr

    def handle(self, frame, unroller):
        assert False # never called

    def cleanupstack(self, frame):
        ec = frame.space.getexecutioncontext()
        ec.set_sys_exc_info(self.operr)


class ExceptBlock(FrameBlock):
    """An try:except: block.  Stores the position of the exception handler."""

    _immutable_ = True
    _opname = 'SETUP_EXCEPT'

    def handle(self, frame, unroller):
        # push the exception to the value stack for inspection by the
        # exception handler (the code after the except:)
        self.cleanupstack(frame)
        # the stack setup is slightly different than in CPython:
        # instead of the traceback, we store the unroller object,
        # wrapped.
        assert isinstance(unroller, SApplicationException)
        operationerr = unroller.operr
        w_value = operationerr.normalize_exception(frame.space)
        frame.pushvalue(unroller)
        frame.pushvalue(w_value)
        # set the current value of sys_exc_info to operationerr,
        # saving the old value in a custom type of FrameBlock
        frame.save_and_change_sys_exc_info(operationerr)
        return r_uint(self.handlerposition)   # jump to the handler


class FinallyBlock(FrameBlock):
    """A try:finally: block.  Stores the position of the exception handler."""

    _immutable_ = True
    _opname = 'SETUP_FINALLY'

    def handle(self, frame, unroller):
        # any abnormal reason for unrolling a finally: triggers the end of
        # the block unrolling and the entering the finally: handler.
        # see comments in cleanup().
        self.cleanupstack(frame)
        operationerr = None
        if isinstance(unroller, SApplicationException):
            operationerr = unroller.operr
            operationerr.normalize_exception(frame.space)
        frame.pushvalue(unroller)
        # set the current value of sys_exc_info to operationerr,
        # saving the old value in a custom type of FrameBlock
        frame.save_and_change_sys_exc_info(operationerr)
        return r_uint(self.handlerposition)   # jump to the handler

    def pop_block(self, frame):
        pass


def source_as_str(space, w_source, funcname, what, flags):
    """Return source code as str0 with adjusted compiler flags

    w_source must be a str or support the buffer interface
    """
    from pypy.interpreter.astcompiler import consts

    if space.isinstance_w(w_source, space.w_unicode):
        from pypy.interpreter.unicodehelper import encode
        w_source = encode(space, w_source)
        source = space.bytes_w(w_source)
        flags |= consts.PyCF_IGNORE_COOKIE
    elif space.isinstance_w(w_source, space.w_bytes):
        source = space.bytes_w(w_source)
    else:
        try:
            buf = space.buffer_w(w_source, space.BUF_SIMPLE)
        except OperationError as e:
            if not e.match(space, space.w_TypeError):
                raise
            raise oefmt(space.w_TypeError,
                        "%s() arg 1 must be a %s object", funcname, what)
        source = buf.as_str()

    if not (flags & consts.PyCF_ACCEPT_NULL_BYTES):
        if '\x00' in source:
            raise oefmt(space.w_ValueError,
                        "source code string cannot contain null bytes")
        source = rstring.assert_str0(source)
    return source, flags


def ensure_ns(space, w_globals, w_locals, funcname, caller=None):
    """Ensure globals/locals exist and are of the correct type"""
    if (not space.is_none(w_globals) and
        not space.isinstance_w(w_globals, space.w_dict)):
        raise oefmt(space.w_TypeError,
                    '%s() arg 2 must be a dict, not %T', funcname, w_globals)
    if (not space.is_none(w_locals) and
        space.lookup(w_locals, '__getitem__') is None):
        raise oefmt(space.w_TypeError,
                    '%s() arg 3 must be a mapping or None, not %T',
                    funcname, w_locals)

    if space.is_none(w_globals):
        if caller is None:
            caller = space.getexecutioncontext().gettopframe_nohidden()
        if caller is None:
            w_globals = space.newdict(module=True)
            if space.is_none(w_locals):
                w_locals = w_globals
        else:
            w_globals = caller.get_w_globals()
            if space.is_none(w_locals):
                w_locals = caller.getdictscope()
    elif space.is_none(w_locals):
        w_locals = w_globals

    return w_globals, w_locals

def _dict_merge(space, w_dict, w_item, w_function):
    from pypy.objspace.std.dictmultiobject import W_DictMultiObject, update1
    l1 = space.len_w(w_dict)
    unroll_safe = jit.isvirtual(w_dict) and l1 < 10
    if not space.isinstance_w(w_dict, space.w_dict):
        raise oefmt(space.w_RuntimeError,
                    "expected a dict, got %T", w_dict)
    assert isinstance(w_dict, W_DictMultiObject)
    if not space.isinstance_w(w_item, space.w_dict):
        unroll_safe = False
        if not space.ismapping_w(w_item):
            raise oefmt(space.w_TypeError,
                        "%s argument after ** must be a mapping, not %T",
                        space.guess_function_name_parens(w_function), w_item)
    else:
        l2 = space.len_w(w_item)
        if l1 == 0:
            update1(space, w_dict, w_item)
            return
        l2 = space.len_w(w_item)
        if l2 == 0:
            return
        unroll_safe = unroll_safe and jit.isvirtual(w_item) and l2 < 10
    _dict_merge_loop(space, w_dict, w_item, unroll_safe, w_function)

@jit.look_inside_iff(lambda space, w_dict, w_item, unroll_safe, w_function: unroll_safe)
def _dict_merge_loop(space, w_dict, w_item, unroll_safe, w_function):
    try:
        w_iterator = space.iter(space.call_method(w_item, "keys"))
    except OperationError as e:
        if not e.match(space, space.w_AttributeError):
            raise
        raise oefmt(space.w_TypeError,
                    "%s argument after ** must be a mapping, not %T",
                    space.guess_function_name_parens(w_function), w_item)
    while True:
        try:
            w_key = space.next(w_iterator)
        except OperationError as e:
            if not e.match(space, space.w_StopIteration):
                raise
            break
        w_value = space.getitem(w_item, w_key)
        if space.contains_w(w_dict, w_key):
            raise oefmt(space.w_TypeError,
                "%s got multiple values for keyword argument %R",
                space.guess_function_name_parens(w_function), w_key)
        space.setitem(w_dict, w_key, w_value)

def _copy_dict_without_keys(space, w_keys, w_subject):
    w_dict = space.newdict()
    space.call_method(w_dict, 'update', w_subject)
    for i in range(space.len_w(w_keys)):
        w_key = space.getitem(w_keys, space.newint(i))
        space.delitem(w_dict, w_key)
    return w_dict

def match_class_attr(space, w_subject, w_name, w_type, seen):
    name = space.text_w(w_name)
    if name in seen:
        raise oefmt(space.w_TypeError,
                "%N() got multiple sub-patterns for attribute %R", w_type, w_name)
    seen[name] = None
    return space.findattr(w_subject, w_name)

@jit.unroll_safe
def _match_class(space, nargs, w_names, w_type, w_subject):
    w_res = space.call_method(space.builtin, "isinstance", w_subject, w_type)
    if not space.is_true(w_res):
        return None

    seen = {}
    attrs_w = []
    if nargs:
        try:
            w_match_args = space.getattr(w_type, space.newtext('__match_args__'))
            match_self = False
        except OperationError as e:
            if not e.match(space, space.w_AttributeError):
                raise e

            w_match_args = space.newtuple([])
            match_self = \
                space.isinstance_w(w_subject, space.w_float) or \
                space.isinstance_w(w_subject, space.w_tuple) or \
                space.isinstance_w(w_subject, space.w_dict) or \
                space.isinstance_w(w_subject, space.w_long) or \
                space.isinstance_w(w_subject, space.w_bytes) or \
                space.isinstance_w(w_subject, space.w_list) or \
                space.isinstance_w(w_subject, space.w_bytearray) or \
                space.isinstance_w(w_subject, space.w_unicode) or \
                space.isinstance_w(w_subject, space.w_set) or \
                space.isinstance_w(w_subject, space.w_frozenset)
        if not space.isinstance_w(w_match_args, space.w_tuple):
            raise oefmt(space.w_TypeError,
                "%N.__match_args__ must be a tuple (got '%T')",
                w_type, w_match_args)

        allowed = 1 if match_self else space.len_w(w_match_args)
        if allowed < nargs:
            plural = "" if allowed == 1 else "s";
            raise oefmt(space.w_TypeError,
                    "%N() accepts %d positional sub-pattern%s (%d given)", w_type, allowed, plural, nargs)

        if match_self:
            attrs_w.append(w_subject)
        else:
            for i in range(nargs):
                w_name = space.getitem(w_match_args, space.newint(i))
                if not space.isinstance_w(w_name, space.w_unicode):
                    raise oefmt(space.w_TypeError,
                            "__match_args__ elements must be strings (got '%T')", w_name)
                w_attr = match_class_attr(space, w_subject, w_name, w_type, seen)
                if w_attr is None:
                    return None
                attrs_w.append(w_attr)

    w_iter = space.iter(w_names)
    try:
        while True:
            w_name = space.next(w_iter)
            w_attr = match_class_attr(space, w_subject, w_name, w_type, seen)
            if w_attr is None:
                return None
            attrs_w.append(w_attr)
    except OperationError as e:
        if not e.match(space, space.w_StopIteration):
            raise

    return space.newtuple(attrs_w[:])

# exception group helpers

@jit.unroll_safe
def check_except_star_type_valid(space, w_typ):
    def check(space, w_typ, w_BaseExceptionGroup):
        if not space.exception_is_valid_class_w(w_typ):
            raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG)
        if space.issubtype_w(w_typ, w_BaseExceptionGroup):
            raise oefmt(space.w_TypeError, "catching ExceptionGroup with except* is not allowed. Use except instead.")
    w_BaseExceptionGroup = space.getattr(space.builtin, space.newtext('BaseExceptionGroup'))
    if space.isinstance_w(w_typ, space.w_tuple):
        for w_sub in space.fixedview(w_typ):
            check(space, w_sub, w_BaseExceptionGroup)
    else:
        check(space, w_typ, w_BaseExceptionGroup)

def exception_group_match(space, w_eg, w_typ):
    if space.is_w(w_eg, space.w_None):
        return space.w_None, space.w_None
    assert space.isinstance_w(w_eg, space.w_Exception)
    w_BaseExceptionGroup = space.getattr(space.builtin, space.newtext('BaseExceptionGroup'))
    w_ExceptionGroup = space.getattr(space.builtin, space.newtext('ExceptionGroup'))
    if space.exception_match(space.type(w_eg), w_typ):
        if space.isinstance_w(w_eg, w_BaseExceptionGroup):
            return w_eg, space.w_None
        w_list = space.newlist([w_eg])
        w_wrapped = space.call_function(w_ExceptionGroup, space.newtext(''), w_list)
        return w_wrapped, space.w_None
    elif space.isinstance_w(w_eg, w_BaseExceptionGroup):
        w_tup = space.call_method(w_eg, 'split', w_typ)
        w_match, w_rest = space.unpackiterable(w_tup, 2)
        return w_match, w_rest
    else:
        return space.w_None, space.w_None

### helpers written at the application-level ###
# Some of these functions are expected to be generally useful if other
# parts of the code need to do the same thing as a non-trivial opcode,
# like finding out which metaclass a new class should have.
# This is why they are not methods of PyFrame.
# There are also a couple of helpers that are methods, defined in the
# class above.

app = gateway.applevel(r'''
    """ applevel implementation of certain system properties, imports
    and other helpers"""
    import sys

    def print_expr(obj):
        try:
            displayhook = sys.displayhook
        except AttributeError:
            raise RuntimeError("lost sys.displayhook")
        displayhook(obj)
''', filename=__file__)

print_expr      = app.interphook('print_expr')

app = gateway.applevel(r'''
    def import_all_from(module, into_locals):
        try:
            all = module.__all__
        except AttributeError:
            try:
                dict = module.__dict__
            except AttributeError:
                raise ImportError("from-import-* object has no __dict__ "
                                  "and no __all__")
            all = dict.keys()
            skip_leading_underscores = True
        else:
            skip_leading_underscores = False

        module_name = module.__name__
        if not isinstance(module_name, str):
            raise TypeError("module __name__ must be a string, not %s", type(module_name).__name__)

        for name in all:
            if not isinstance(name, str):
                if skip_leading_underscores:
                    container = "__dict__"
                    accessor = "Key"
                else:
                    container = "__all__"
                    accessor = "Item"

                raise TypeError(
                    "%s in %s.%s must be str, not %s" % (
                        accessor,
                        module_name,
                        container,
                        type(name).__name__
                    )
                )
            if skip_leading_underscores and name and name[0] == '_':
                continue
            into_locals[name] = getattr(module, name)
''', filename=__file__)

import_all_from = app.interphook('import_all_from')