Skip to content
Permalink
Browse files

bpo-39320: Handle unpacking of *values in compiler (GH-17984)

* Add three new bytecodes: LIST_TO_TUPLE, LIST_EXTEND, SET_UPDATE. Use them to implement star unpacking expressions.

* Remove four bytecodes BUILD_LIST_UNPACK, BUILD_TUPLE_UNPACK, BUILD_SET_UNPACK and  BUILD_TUPLE_UNPACK_WITH_CALL opcodes as they are now unused.

* Update magic number and dis.rst for new bytecodes.
  • Loading branch information
markshannon committed Jan 23, 2020
1 parent f9e07e1 commit 13bc13960cc83dbd1cb5701d9a59ac9b9144b205
@@ -859,40 +859,25 @@ All of the following opcodes use their arguments.
.. versionadded:: 3.6


.. opcode:: BUILD_TUPLE_UNPACK (count)
.. opcode:: LIST_TO_TUPLE

Pops *count* iterables from the stack, joins them in a single tuple,
and pushes the result. Implements iterable unpacking in tuple
displays ``(*x, *y, *z)``.
Pops a list from the stack and pushes a tuple containing the same values.

.. versionadded:: 3.5


.. opcode:: BUILD_TUPLE_UNPACK_WITH_CALL (count)

This is similar to :opcode:`BUILD_TUPLE_UNPACK`,
but is used for ``f(*x, *y, *z)`` call syntax. The stack item at position
``count + 1`` should be the corresponding callable ``f``.

.. versionadded:: 3.6
.. versionadded:: 3.9


.. opcode:: BUILD_LIST_UNPACK (count)
.. opcode:: LIST_EXTEND (i)

This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a list
instead of tuple. Implements iterable unpacking in list
displays ``[*x, *y, *z]``.
Calls ``list.extend(TOS1[-i], TOS)``. Used to build lists.

.. versionadded:: 3.5
.. versionadded:: 3.9


.. opcode:: BUILD_SET_UNPACK (count)
.. opcode:: SET_UPDATE

This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a set
instead of tuple. Implements iterable unpacking in set
displays ``{*x, *y, *z}``.
Calls ``set.update(TOS1[-i], TOS)``. Used to build sets.

.. versionadded:: 3.5
.. versionadded:: 3.9


.. opcode:: BUILD_MAP_UNPACK (count)
@@ -1124,10 +1109,6 @@ All of the following opcodes use their arguments.
Calls a callable object with variable set of positional and keyword
arguments. If the lowest bit of *flags* is set, the top of the stack
contains a mapping object containing additional keyword arguments.
Below that is an iterable object containing positional arguments and
a callable object to call. :opcode:`BUILD_MAP_UNPACK_WITH_CALL` and
:opcode:`BUILD_TUPLE_UNPACK_WITH_CALL` can be used for merging multiple
mapping objects and iterables containing arguments.
Before the callable is called, the mapping object and iterable object
are each "unpacked" and their contents passed in as keyword and
positional arguments respectively.

Some generated files are not rendered by default. Learn more.

@@ -275,6 +275,8 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.9a0 3421 (simplified bytecode for with blocks #32949)
# Python 3.9a0 3422 (remove BEGIN_FINALLY, END_FINALLY, CALL_FINALLY, POP_FINALLY bytecodes #33387)
# Python 3.9a2 3423 (add IS_OP, CONTAINS_OP and JUMP_IF_NOT_EXC_MATCH bytecodes #39156)
# Python 3.9a2 3424 (simplify bytecodes for *value unpacking)

#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
@@ -283,7 +285,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3423).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3424).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
@@ -117,6 +117,7 @@ def jabs_op(name, op):
def_op('INPLACE_XOR', 78)
def_op('INPLACE_OR', 79)

def_op('LIST_TO_TUPLE', 82)
def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)
def_op('SETUP_ANNOTATIONS', 85)
@@ -199,20 +200,19 @@ def jabs_op(name, op):
def_op('EXTENDED_ARG', 144)
EXTENDED_ARG = 144

def_op('BUILD_LIST_UNPACK', 149)
def_op('BUILD_MAP_UNPACK', 150)
def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
def_op('BUILD_TUPLE_UNPACK', 152)
def_op('BUILD_SET_UNPACK', 153)

jrel_op('SETUP_ASYNC_WITH', 154)

def_op('FORMAT_VALUE', 155)
def_op('BUILD_CONST_KEY_MAP', 156)
def_op('BUILD_STRING', 157)
def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 158)

name_op('LOAD_METHOD', 160)
def_op('CALL_METHOD', 161)

def_op('LIST_EXTEND', 162)
def_op('SET_UPDATE', 163)

del def_op, name_op, jrel_op, jabs_op
@@ -252,12 +252,12 @@
>>> h(1, *h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after * must be an iterable, not function
TypeError: Value after * must be an iterable, not function
>>> h(*[1], *h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after * must be an iterable, not function
TypeError: Value after * must be an iterable, not function
>>> dir(*h)
Traceback (most recent call last):
@@ -0,0 +1,15 @@
Replace four complex bytecodes for building sequences with three simpler ones.


The following four bytecodes have been removed:

* BUILD_LIST_UNPACK
* BUILD_TUPLE_UNPACK
* BUILD_SET_UNPACK
* BUILD_TUPLE_UNPACK_WITH_CALL

The following three bytecodes have been added:

* LIST_TO_TUPLE
* LIST_EXTEND
* SET_UPDATE
@@ -2621,46 +2621,46 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
DISPATCH();
}

case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
case TARGET(BUILD_TUPLE_UNPACK):
case TARGET(BUILD_LIST_UNPACK): {
int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Py_ssize_t i;
PyObject *sum = PyList_New(0);
PyObject *return_value;

if (sum == NULL)
case TARGET(LIST_TO_TUPLE): {
PyObject *list = POP();
PyObject *tuple = PyList_AsTuple(list);
Py_DECREF(list);
if (tuple == NULL) {
goto error;
}
PUSH(tuple);
DISPATCH();
}

for (i = oparg; i > 0; i--) {
PyObject *none_val;

none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
if (none_val == NULL) {
if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
_PyErr_ExceptionMatches(tstate, PyExc_TypeError))
{
check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
}
Py_DECREF(sum);
goto error;
case TARGET(LIST_EXTEND): {
PyObject *iterable = POP();
PyObject *list = PEEK(oparg);
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
if (none_val == NULL) {
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
(iterable->ob_type->tp_iter == NULL && !PySequence_Check(iterable)))
{
PyErr_Clear();
_PyErr_Format(tstate, PyExc_TypeError,
"Value after * must be an iterable, not %.200s",
Py_TYPE(iterable)->tp_name);
}
Py_DECREF(none_val);
Py_DECREF(iterable);
goto error;
}
Py_DECREF(none_val);
Py_DECREF(iterable);
DISPATCH();
}

if (convert_to_tuple) {
return_value = PyList_AsTuple(sum);
Py_DECREF(sum);
if (return_value == NULL)
goto error;
}
else {
return_value = sum;
case TARGET(SET_UPDATE): {
PyObject *iterable = POP();
PyObject *set = PEEK(oparg);
int err = _PySet_Update(set, iterable);
Py_DECREF(iterable);
if (err < 0) {
goto error;
}

while (oparg--)
Py_DECREF(POP());
PUSH(return_value);
DISPATCH();
}

@@ -2685,25 +2685,6 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
DISPATCH();
}

case TARGET(BUILD_SET_UNPACK): {
Py_ssize_t i;
PyObject *sum = PySet_New(NULL);
if (sum == NULL)
goto error;

for (i = oparg; i > 0; i--) {
if (_PySet_Update(sum, PEEK(i)) < 0) {
Py_DECREF(sum);
goto error;
}
}

while (oparg--)
Py_DECREF(POP());
PUSH(sum);
DISPATCH();
}

case TARGET(BUILD_MAP): {
Py_ssize_t i;
PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);

0 comments on commit 13bc139

Please sign in to comment.
You can’t perform that action at this time.