diff options
author | Raymond Hettinger <python@rcn.com> | 2004-03-04 08:25:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-03-04 08:25:44 (GMT) |
commit | 31017aed36a5c5b0e4b16ca58bea09c9ce360134 (patch) | |
tree | 766d70bb4fbb6878a71c81fc3874515cfcbc8aa8 /Objects | |
parent | 6c79a518e70ea8e45e3287573d99c648ae3cb21b (diff) | |
download | cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.zip cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.tar.gz cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.tar.bz2 |
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
(Championed by Bob Ippolito.)
The update() method for mappings now accepts all the same argument forms
as the dict() constructor. This includes item lists and/or keyword
arguments.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index eb0222c..b5cbd66 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1029,10 +1029,30 @@ Fail: return NULL; } +static int +dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) +{ + PyObject *arg = NULL; + int result = 0; + + if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) + result = -1; + + else if (arg != NULL) { + if (PyObject_HasAttrString(arg, "keys")) + result = PyDict_Merge(self, arg, 1); + else + result = PyDict_MergeFromSeq2(self, arg, 1); + } + if (result == 0 && kwds != NULL) + result = PyDict_Merge(self, kwds, 1); + return result; +} + static PyObject * -dict_update(PyObject *mp, PyObject *other) +dict_update(PyObject *self, PyObject *args, PyObject *kwds) { - if (PyDict_Update(mp, other) < 0) + if (dict_update_common(self, args, kwds, "update") == -1) return NULL; Py_INCREF(Py_None); return Py_None; @@ -1806,7 +1826,7 @@ static PyMethodDef mapp_methods[] = { items__doc__}, {"values", (PyCFunction)dict_values, METH_NOARGS, values__doc__}, - {"update", (PyCFunction)dict_update, METH_O, + {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, update__doc__}, {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, fromkeys__doc__}, @@ -1875,21 +1895,7 @@ dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static int dict_init(PyObject *self, PyObject *args, PyObject *kwds) { - PyObject *arg = NULL; - int result = 0; - - if (!PyArg_UnpackTuple(args, "dict", 0, 1, &arg)) - result = -1; - - else if (arg != NULL) { - if (PyObject_HasAttrString(arg, "keys")) - result = PyDict_Merge(self, arg, 1); - else - result = PyDict_MergeFromSeq2(self, arg, 1); - } - if (result == 0 && kwds != NULL) - result = PyDict_Merge(self, kwds, 1); - return result; + return dict_update_common(self, args, kwds, "dict"); } static long |