@@ -404,7 +404,7 @@ The Python compiler currently generates the following bytecode instructions.
404
404
405
405
In the following, We will refer to the interpreter stack as STACK and describe
406
406
operations on it as if it was a Python list. The top of the stack corresponds to
407
- STACK[-1] in this language.
407
+ `` STACK[-1] `` in this language.
408
408
409
409
.. opcode :: NOP
410
410
@@ -516,37 +516,63 @@ not have to be) the original ``STACK[-2]``.
516
516
.. opcode :: BINARY_OP (op)
517
517
518
518
Implements the binary and in-place operators (depending on the value of
519
- *op *).
520
- ``STACK[-1] = op STACK[-1] ``.
519
+ *op *).::
520
+
521
+ rhs = STACK.pop()
522
+ lhs = STACK.pop()
523
+ STACK.append(lhs op rhs)
521
524
522
525
.. versionadded :: 3.11
523
526
524
527
525
528
.. opcode :: BINARY_SUBSCR
526
529
527
- Implements ``STACK[-1] = STACK[-2][STACK[-1]] ``.
530
+ Implements::
531
+
532
+ key = STACK.pop()
533
+ container = STACK.pop()
534
+ STACK.append(container[index])
528
535
529
536
530
537
.. opcode :: STORE_SUBSCR
531
538
532
- Implements ``STACK[-2][STACK[-1]] = STACK[-3] ``.
539
+ Implements::
540
+
541
+ key = STACK.pop()
542
+ container = STACK.pop()
543
+ value = STACK.pop()
544
+ container[key] = value
533
545
534
546
535
547
.. opcode :: DELETE_SUBSCR
536
548
537
- Implements `` del STACK[-2][STACK[-1]] ``.
549
+ Implements::
538
550
551
+ key = STACK.pop()
552
+ container = STACK.pop()
553
+ del container[key]
539
554
540
555
.. opcode :: BINARY_SLICE
541
556
542
- Implements ``STACK[-1] = STACK[-3][STACK[-2]:STACK[-1]] ``.
557
+ Implements::
558
+
559
+ end = STACK.pop()
560
+ start = STACK.pop()
561
+ container = STACK.pop()
562
+ STACK.append(container[start:end])
543
563
544
564
.. versionadded :: 3.12
545
565
546
566
547
567
.. opcode :: STORE_SLICE
548
568
549
- Implements ``STACK[-3][STACK[-2]:STACK[-1]] = STACK[-4] ``.
569
+ Implements::
570
+
571
+ end = STACK.pop()
572
+ start = STACK.pop()
573
+ container = STACK.pop()
574
+ values = STACK.pop()
575
+ container[start:end] = value
550
576
551
577
.. versionadded :: 3.12
552
578
@@ -644,18 +670,32 @@ not have to be) the original ``STACK[-2]``.
644
670
645
671
.. opcode :: SET_ADD (i)
646
672
647
- Calls ``set.add(STACK[-i], STACK[-1]) ``. Used to implement set comprehensions.
673
+ Implements::
674
+ item = STACK.pop()
675
+ set.add(STACK[-i], item)
676
+
677
+ Used to implement set comprehensions.
648
678
649
679
650
680
.. opcode :: LIST_APPEND (i)
651
681
652
- Calls ``list.append(STACK[-i], STACK[-1]) ``. Used to implement list comprehensions.
682
+ Implements::
683
+
684
+ item = STACK.pop()
685
+ list.append(STACK[-i], item)
686
+
687
+ Used to implement list comprehensions.
653
688
654
689
655
690
.. opcode :: MAP_ADD (i)
656
691
657
- Calls ``dict.__setitem__(STACK[-i], STACK[-2], STACK[-1]) ``. Used to implement dict
658
- comprehensions.
692
+ Implements::
693
+
694
+ value = STACK.pop()
695
+ key = STACK.pop()
696
+ dict.__setitem__(STACK[-i], key, value)
697
+
698
+ Used to implement dict comprehensions.
659
699
660
700
.. versionadded :: 3.1
661
701
.. versionchanged :: 3.8
@@ -837,7 +877,7 @@ iterations of the loop.
837
877
838
878
.. opcode :: STORE_NAME (namei)
839
879
840
- Implements ``name = STACK[-1] ``. *namei * is the index of *name * in the attribute
880
+ Implements ``name = STACK.pop() ``. *namei * is the index of *name * in the attribute
841
881
:attr: `co_names ` of the code object. The compiler tries to use
842
882
:opcode: `STORE_FAST ` or :opcode: `STORE_GLOBAL ` if possible.
843
883
@@ -877,13 +917,22 @@ iterations of the loop.
877
917
878
918
.. opcode :: STORE_ATTR (namei)
879
919
880
- Implements ``STACK[-1].name = STACK[-2] ``, where *namei * is the index of name in
881
- :attr: `co_names `. Both values are popped from the stack.
920
+ Implements::
921
+
922
+ obj = STACK.pop()
923
+ value = STACK.pop()
924
+ obj.name = value
882
925
926
+ where *namei * is the index of name in :attr: `co_names `.
883
927
884
928
.. opcode :: DELETE_ATTR (namei)
885
929
886
- Implements ``del STACK[-1].name ``, using *namei * as index into :attr: `co_names `.
930
+ Implements::
931
+
932
+ obj = STACK.pop()
933
+ del obj.name
934
+
935
+ where *namei * is the index of name into :attr: `co_names `.
887
936
888
937
889
938
.. opcode :: STORE_GLOBAL (namei)
@@ -961,21 +1010,36 @@ iterations of the loop.
961
1010
962
1011
.. opcode :: LIST_EXTEND (i)
963
1012
964
- Calls ``list.extend(STACK[-i], STACK[-1]) ``. Used to build lists.
1013
+ Implements::
1014
+
1015
+ seq = STACK.pop()
1016
+ list.extend(STACK[-i], seq)
1017
+
1018
+ Used to build lists.
965
1019
966
1020
.. versionadded :: 3.9
967
1021
968
1022
969
1023
.. opcode :: SET_UPDATE (i)
970
1024
971
- Calls ``set.update(STACK[-i], STACK[-1]) ``. Used to build sets.
1025
+ Implements::
1026
+
1027
+ seq = STACK.pop()
1028
+ set.update(STACK[-i], seq)
1029
+
1030
+ Used to build sets.
972
1031
973
1032
.. versionadded :: 3.9
974
1033
975
1034
976
1035
.. opcode :: DICT_UPDATE (i)
977
1036
978
- Calls ``dict.update(STACK[-i], STACK[-1]) ``. Used to build dicts.
1037
+ Implements::
1038
+
1039
+ map = STACK.pop()
1040
+ dict.update(STACK[-i], map)
1041
+
1042
+ Used to build dicts.
979
1043
980
1044
.. versionadded :: 3.9
981
1045
@@ -1138,10 +1202,10 @@ iterations of the loop.
1138
1202
1139
1203
.. opcode :: FOR_ITER (delta)
1140
1204
1141
- ``STACK[-1] `` is an :term: `iterator `. Call its :meth: `~iterator.__next__ ` method. If
1142
- this yields a new value, push it on the stack (leaving the iterator below
1143
- it). If the iterator indicates it is exhausted then the byte
1144
- code counter is incremented by *delta *.
1205
+ ``STACK[-1] `` is an :term: `iterator `. Call its :meth: `~iterator.__next__ ` method.
1206
+ If this yields a new value, push it on the stack (leaving the iterator below
1207
+ it). If the iterator indicates it is exhausted then the byte code counter is
1208
+ incremented by *delta *.
1145
1209
1146
1210
.. versionchanged :: 3.12
1147
1211
Up until 3.11 the iterator was popped when it was exhausted.
@@ -1343,9 +1407,20 @@ iterations of the loop.
1343
1407
1344
1408
.. index :: builtin: slice
1345
1409
1346
- Pushes a slice object on the stack. *argc * must be 2 or 3. If it is 2,
1347
- ``slice(STACK[-2], STACK[-1]) `` is pushed; if it is 3, ``slice(STACK[-3], STACK[-2], STACK[-1]) `` is
1348
- pushed. See the :func: `slice ` built-in function for more information.
1410
+ Pushes a slice object on the stack. *argc * must be 2 or 3. If it is 2, implements::
1411
+
1412
+ end = STACK.pop()
1413
+ start = STACK.pop()
1414
+ STACK.append(slice(start, stop))
1415
+
1416
+ if it is 3, implements::
1417
+
1418
+ step = STACK.pop()
1419
+ end = STACK.pop()
1420
+ start = STACK.pop()
1421
+ STACK.append(slice(start, end, step))
1422
+
1423
+ See the :func: `slice ` built-in function for more information.
1349
1424
1350
1425
1351
1426
.. opcode :: EXTENDED_ARG (ext)
@@ -1386,7 +1461,8 @@ iterations of the loop.
1386
1461
1387
1462
Pop ``STACK[-1] ``, ``STACK[-2] ``, and ``STACK[-3] ``. If ``STACK[-3] `` is an
1388
1463
instance of ``STACK[-2] `` and has the positional and keyword attributes
1389
- required by *count * and ``STACK[-1] ``, push a tuple of extracted attributes. Otherwise, push ``None ``.
1464
+ required by *count * and ``STACK[-1] ``, push a tuple of extracted attributes.
1465
+ Otherwise, push ``None ``.
1390
1466
1391
1467
.. versionadded :: 3.10
1392
1468
@@ -1421,8 +1497,8 @@ iterations of the loop.
1421
1497
1422
1498
.. opcode :: SEND (delta)
1423
1499
1424
- Equivalent to ``STACK[-1] = STACK[-2].send(STACK[-1]) ``. Used in ``yield from `` and `` await ``
1425
- statements.
1500
+ Equivalent to ``STACK[-1] = STACK[-2].send(STACK[-1]) ``. Used in ``yield from ``
1501
+ and `` await `` statements.
1426
1502
1427
1503
If the call raises :exc: `StopIteration `, pop both items, push the
1428
1504
exception's ``value `` attribute, and increment the bytecode counter by
0 commit comments