[Python-Dev] CALL_FUNCTION_EX arg and stack_effect
Matthieu Dartiailh
m.dartiailh at gmail.com
Mon Feb 20 13:44:36 EST 2017
Hi,
I have a question about the use of CALL_FUNCTION_EX in
https://github.com/python/cpython/blob/master/Python/compile.c#L3624.
Looking at the code it appears that the argument will be either 1 or 0
depending on whether or not the function is taking keywords arguments
(which means that CALL_FUNCTION_EX cannot be used on function taking no
argument).
Executing that opcode will remove from the stack the function code, the
positional arguments (packed in a tuple) and the potential keyword
arguments packed in a dict and push the return value. So the stack
effect will be either -1 or -2 (could be 0 if the possibility to pass 0
arguments existed).
Looking at the stack effect computation
(https://github.com/python/cpython/blob/master/Python/compile.c#L1047),
it appears that the stack effect will be 0 if the argument is 0, -1 for
either 1 or 2, and -2 for 3. Which means that the code generated at
https://github.com/python/cpython/blob/master/Python/compile.c#L3624 can
never allow to compute the right stack effect using the stack_effect
function (as it will return either 0 or -1 instead of -1 and -2)
I would say that this is a bug and that the oparg should be 1 + 2 if
keywords arguments are present at line 3624.
I am not sure what consequence this can have on CPython but it means the
bytecode becomes weird as a the stack looks like it can grow during a
list comprehension (calling a function f using * syntax) :
BUILD_LIST 0 1
LOAD_FAST .0 1
FOR_ITER 22 1 ---> after each jump it looks like the stack is
higher by one
STORE_FAST i -1
LOAD_GLOBAL f 1
LOAD_DEREF a 1
LOAD_FAST i 1
BINARY_SUBSCR None -1
CALL_FUNCTION_EX 0 0
LIST_APPEND 2 -1
JUMP_ABSOLUTE 4 0
RETURN_VALUE None -1
What do you think ? Should I open an issue on https://bugs.python.org/ ?
Best regards
Matthieu
More information about the Python-Dev
mailing list