From 8a4cd700a7426341c2074a2b580306d2d60ec839 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 27 Jan 2020 09:57:45 +0000 Subject: 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. --- Python/ceval.c | 55 +++++++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 36 deletions(-) (limited to 'Python/ceval.c') diff --git a/Python/ceval.c b/Python/ceval.c index 528ad7fdd1..673b401e6a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -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) - goto error; + case TARGET(DICT_MERGE): { + PyObject *update = POP(); + PyObject *dict = PEEK(oparg); - 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; - } + if (_PyDict_MergeEx(dict, update, 2) < 0) { + format_kwargs_error(tstate, PEEK(2 + oparg), update); + Py_DECREF(update); + goto error; } - - while (oparg--) - Py_DECREF(POP()); - PUSH(sum); + Py_DECREF(update); PREDICT(CALL_FUNCTION_EX); DISPATCH(); } -- cgit v1.2.1