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

* Add DICT_UPDATE and DICT_MERGE bytecodes. Use them for ** unpacking.

* Remove BUILD_MAP_UNPACK and BUILD_MAP_UNPACK_WITH_CALL, as they are now unused.

* Update magic number for ** unpacking opcodes.

* Update dis.rst to incorporate new bytecodes.

* Add blurb entry.
This commit is contained in:
Mark Shannon 2020-01-27 09:57:45 +00:00 committed by GitHub
parent 72b1004657
commit 8a4cd700a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 3680 additions and 3672 deletions

View file

@ -2801,49 +2801,32 @@ main_loop:
DISPATCH();
}
case TARGET(BUILD_MAP_UNPACK): {
Py_ssize_t i;
PyObject *sum = PyDict_New();
if (sum == NULL)
goto error;
for (i = oparg; i > 0; i--) {
PyObject *arg = PEEK(i);
if (PyDict_Update(sum, arg) < 0) {
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping",
arg->ob_type->tp_name);
}
Py_DECREF(sum);
goto error;
case TARGET(DICT_UPDATE): {
PyObject *update = POP();
PyObject *dict = PEEK(oparg);
if (PyDict_Update(dict, update) < 0) {
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping",
update->ob_type->tp_name);
}
Py_DECREF(update);
goto error;
}
while (oparg--)
Py_DECREF(POP());
PUSH(sum);
Py_DECREF(update);
DISPATCH();
}
case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Py_ssize_t i;
PyObject *sum = PyDict_New();
if (sum == NULL)
case TARGET(DICT_MERGE): {
PyObject *update = POP();
PyObject *dict = PEEK(oparg);
if (_PyDict_MergeEx(dict, update, 2) < 0) {
format_kwargs_error(tstate, PEEK(2 + oparg), update);
Py_DECREF(update);
goto error;
for (i = oparg; i > 0; i--) {
PyObject *arg = PEEK(i);
if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Py_DECREF(sum);
format_kwargs_error(tstate, PEEK(2 + oparg), arg);
goto error;
}
}
while (oparg--)
Py_DECREF(POP());
PUSH(sum);
Py_DECREF(update);
PREDICT(CALL_FUNCTION_EX);
DISPATCH();
}