Skip to content

Commit 76aa2c0

Browse files
bpo-33216: Clarify the documentation for CALL_FUNCTION_* (#8338)
Clarify the documentation for the CALL_FUNCTION_* bytecodes. They changed in 3.5 in subtle ways and the documentation has never been correct, much less clear.
1 parent 1b141b9 commit 76aa2c0

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

Doc/library/dis.rst

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -960,34 +960,38 @@ the more significant byte last.
960960

961961
.. opcode:: RAISE_VARARGS (argc)
962962

963-
Raises an exception. *argc* indicates the number of parameters to the raise
963+
Raises an exception. *argc* indicates the number of arguments to the raise
964964
statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
965965
the parameter as TOS1, and the exception as TOS.
966966

967967

968968
.. opcode:: CALL_FUNCTION (argc)
969969

970-
Calls a function. The low byte of *argc* indicates the number of positional
971-
parameters, the high byte the number of keyword parameters. On the stack, the
972-
opcode finds the keyword parameters first. For each keyword argument, the
973-
value is on top of the key. Below the keyword parameters, the positional
974-
parameters are on the stack, with the right-most parameter on top. Below the
975-
parameters, the function object to call is on the stack. Pops all function
976-
arguments, and the function itself off the stack, and pushes the return
977-
value.
970+
Calls a callable object. The low byte of *argc* indicates the number of
971+
positional arguments, the high byte the number of keyword arguments.
972+
The stack contains keyword arguments on top (if any), then the positional
973+
arguments below that (if any), then the callable object to call below that.
974+
Each keyword argument is represented with two values on the stack:
975+
the argument's name, and its value, with the argument's value above the
976+
name on the stack.
977+
The positional arguments are pushed in the order that they are passed in
978+
to the callable object, with the right-most positional argument on top.
979+
``CALL_FUNCTION`` pops all arguments and the callable object off the stack,
980+
calls the callable object with those arguments, and pushes the return value
981+
returned by the callable object.
978982

979983

980984
.. opcode:: MAKE_FUNCTION (argc)
981985

982986
Pushes a new function object on the stack. From bottom to top, the consumed
983987
stack must consist of
984988

985-
* ``argc & 0xFF`` default argument objects in positional order
989+
* ``argc & 0xFF`` default argument objects in positional order, for positional parameters
986990
* ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
987991
just below the object on the stack, for keyword-only parameters
988992
* ``(argc >> 16) & 0x7FFF`` parameter annotation objects
989993
* a tuple listing the parameter names for the annotations (only if there are
990-
ony annotation objects)
994+
any annotation objects)
991995
* the code associated with the function (at TOS1)
992996
* the :term:`qualified name` of the function (at TOS)
993997

@@ -1020,24 +1024,68 @@ the more significant byte last.
10201024

10211025
.. opcode:: CALL_FUNCTION_VAR (argc)
10221026

1023-
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
1024-
top element on the stack contains the variable argument list, followed by
1025-
keyword and positional arguments.
1027+
Calls a callable object, similarly to :opcode:`CALL_FUNCTION`.
1028+
*argc* represents the number of keyword and positional
1029+
arguments, identically to :opcode:`CALL_FUNCTION`.
1030+
The top of the stack contains keyword arguments (if any), stored
1031+
identically to :opcode:`CALL_FUNCTION`.
1032+
Below that
1033+
is an iterable object containing additional positional arguments.
1034+
Below that are positional arguments (if any)
1035+
and a callable object, identically to :opcode:`CALL_FUNCTION`.
1036+
Before the callable object is called, the iterable object
1037+
is "unpacked" and its contents are appended to the positional
1038+
arguments passed in.
1039+
The iterable object is ignored when computing
1040+
the value of ``argc``.
1041+
1042+
.. versionchanged:: 3.5
1043+
In versions 3.0 to 3.4, the iterable object was above
1044+
the keyword arguments; in 3.5 the iterable object was moved
1045+
below the keyword arguments.
1046+
10261047

10271048

10281049
.. opcode:: CALL_FUNCTION_KW (argc)
10291050

1030-
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
1031-
top element on the stack contains the keyword arguments dictionary, followed
1032-
by explicit keyword and positional arguments.
1051+
Calls a callable object, similarly to :opcode:`CALL_FUNCTION`.
1052+
*argc* represents the number of keyword and positional
1053+
arguments, identically to :opcode:`CALL_FUNCTION`.
1054+
The top of the stack contains a mapping object containing additional keyword
1055+
arguments.
1056+
Below this are keyword arguments (if any), positional arguments (if any),
1057+
and a callable object, identically to :opcode:`CALL_FUNCTION`.
1058+
Before the callable is called, the mapping object at the top of the stack is
1059+
"unpacked" and its contents are appended to the keyword arguments passed in.
1060+
The mapping object at the top of the stack is ignored when computing
1061+
the value of ``argc``.
10331062

10341063

10351064
.. opcode:: CALL_FUNCTION_VAR_KW (argc)
10361065

1037-
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
1038-
top element on the stack contains the keyword arguments dictionary, followed
1039-
by the variable-arguments tuple, followed by explicit keyword and positional
1040-
arguments.
1066+
Calls a callable object, similarly to :opcode:`CALL_FUNCTION_VAR` and
1067+
:opcode:`CALL_FUNCTION_KW`.
1068+
*argc* represents the number of keyword and positional
1069+
arguments, identically to :opcode:`CALL_FUNCTION`.
1070+
The top of the stack contains a mapping object, as per
1071+
:opcode:`CALL_FUNCTION_KW`.
1072+
Below that are keyword arguments (if any), stored
1073+
identically to :opcode:`CALL_FUNCTION`.
1074+
Below that
1075+
is an iterable object containing additional positional arguments.
1076+
Below that are positional arguments (if any)
1077+
and a callable object, identically to :opcode:`CALL_FUNCTION`.
1078+
Before the callable is called, the mapping object and iterable object
1079+
are each "unpacked" and their contents passed in as keyword and
1080+
positional arguments respectively,
1081+
identically to :opcode:`CALL_FUNCTION_VAR` and :opcode:`CALL_FUNCTION_KW`.
1082+
The mapping object and iterable object are both ignored when computing
1083+
the value of ``argc``.
1084+
1085+
.. versionchanged:: 3.5
1086+
In versions 3.0 to 3.4, the iterable object was above
1087+
the keyword arguments; in 3.5 the iterable object was moved
1088+
below the keyword arguments.
10411089

10421090

10431091
.. opcode:: HAVE_ARGUMENT
@@ -1071,7 +1119,7 @@ instructions:
10711119

10721120
.. data:: hasconst
10731121

1074-
Sequence of bytecodes that have a constant parameter.
1122+
Sequence of bytecodes that access a constant.
10751123

10761124

10771125
.. data:: hasfree
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clarify the documentation for CALL_FUNCTION_VAR, CALL_FUNCTION_KW, and
2+
CALL_FUNCTION_VAR_KW.

0 commit comments

Comments
 (0)