Skip to content

Commit aaa0ba3

Browse files
docs: fix the description of the stack impact of multiple opcodes
1 parent b8bd57e commit aaa0ba3

File tree

1 file changed

+105
-29
lines changed

1 file changed

+105
-29
lines changed

Doc/library/dis.rst

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ The Python compiler currently generates the following bytecode instructions.
404404

405405
In the following, We will refer to the interpreter stack as STACK and describe
406406
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.
408408

409409
.. opcode:: NOP
410410

@@ -516,37 +516,63 @@ not have to be) the original ``STACK[-2]``.
516516
.. opcode:: BINARY_OP (op)
517517

518518
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)
521524

522525
.. versionadded:: 3.11
523526

524527

525528
.. opcode:: BINARY_SUBSCR
526529

527-
Implements ``STACK[-1] = STACK[-2][STACK[-1]]``.
530+
Implements::
531+
532+
key = STACK.pop()
533+
container = STACK.pop()
534+
STACK.append(container[index])
528535

529536

530537
.. opcode:: STORE_SUBSCR
531538

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
533545

534546

535547
.. opcode:: DELETE_SUBSCR
536548

537-
Implements ``del STACK[-2][STACK[-1]]``.
549+
Implements::
538550

551+
key = STACK.pop()
552+
container = STACK.pop()
553+
del container[key]
539554

540555
.. opcode:: BINARY_SLICE
541556

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])
543563

544564
.. versionadded:: 3.12
545565

546566

547567
.. opcode:: STORE_SLICE
548568

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
550576

551577
.. versionadded:: 3.12
552578

@@ -644,18 +670,32 @@ not have to be) the original ``STACK[-2]``.
644670

645671
.. opcode:: SET_ADD (i)
646672

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.
648678

649679

650680
.. opcode:: LIST_APPEND (i)
651681

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.
653688

654689

655690
.. opcode:: MAP_ADD (i)
656691

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.
659699

660700
.. versionadded:: 3.1
661701
.. versionchanged:: 3.8
@@ -837,7 +877,7 @@ iterations of the loop.
837877

838878
.. opcode:: STORE_NAME (namei)
839879

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
841881
:attr:`co_names` of the code object. The compiler tries to use
842882
:opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
843883

@@ -877,13 +917,22 @@ iterations of the loop.
877917

878918
.. opcode:: STORE_ATTR (namei)
879919

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
882925

926+
where *namei* is the index of name in :attr:`co_names`.
883927

884928
.. opcode:: DELETE_ATTR (namei)
885929

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`.
887936

888937

889938
.. opcode:: STORE_GLOBAL (namei)
@@ -961,21 +1010,36 @@ iterations of the loop.
9611010

9621011
.. opcode:: LIST_EXTEND (i)
9631012

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.
9651019

9661020
.. versionadded:: 3.9
9671021

9681022

9691023
.. opcode:: SET_UPDATE (i)
9701024

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.
9721031

9731032
.. versionadded:: 3.9
9741033

9751034

9761035
.. opcode:: DICT_UPDATE (i)
9771036

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.
9791043

9801044
.. versionadded:: 3.9
9811045

@@ -1138,10 +1202,10 @@ iterations of the loop.
11381202

11391203
.. opcode:: FOR_ITER (delta)
11401204

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*.
11451209

11461210
.. versionchanged:: 3.12
11471211
Up until 3.11 the iterator was popped when it was exhausted.
@@ -1343,9 +1407,20 @@ iterations of the loop.
13431407

13441408
.. index:: builtin: slice
13451409

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.
13491424

13501425

13511426
.. opcode:: EXTENDED_ARG (ext)
@@ -1386,7 +1461,8 @@ iterations of the loop.
13861461

13871462
Pop ``STACK[-1]``, ``STACK[-2]``, and ``STACK[-3]``. If ``STACK[-3]`` is an
13881463
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``.
13901466

13911467
.. versionadded:: 3.10
13921468

@@ -1421,8 +1497,8 @@ iterations of the loop.
14211497

14221498
.. opcode:: SEND (delta)
14231499

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.
14261502

14271503
If the call raises :exc:`StopIteration`, pop both items, push the
14281504
exception's ``value`` attribute, and increment the bytecode counter by

0 commit comments

Comments
 (0)