diff options
author | Raymond Hettinger <python@rcn.com> | 2003-03-06 23:54:28 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-03-06 23:54:28 (GMT) |
commit | a3e1e4cd79abe7069c8bc20db65161dd8c68b305 (patch) | |
tree | 18ff4fb0f06cc1a684da8ad62ce21a5b14c1a33c /Objects/dictobject.c | |
parent | 2b4821347f00b01746562d58390a213924f2b102 (diff) | |
download | cpython-a3e1e4cd79abe7069c8bc20db65161dd8c68b305.zip cpython-a3e1e4cd79abe7069c8bc20db65161dd8c68b305.tar.gz cpython-a3e1e4cd79abe7069c8bc20db65161dd8c68b305.tar.bz2 |
SF patch #693753: fix for bug 639806: default for dict.pop
(contributed by Michael Stone.)
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index f7f2d6b..633f2ce 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1544,13 +1544,20 @@ dict_clear(register dictobject *mp) } static PyObject * -dict_pop(dictobject *mp, PyObject *key) +dict_pop(dictobject *mp, PyObject *args) { long hash; dictentry *ep; PyObject *old_value, *old_key; + PyObject *key, *deflt = NULL; + if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) + return NULL; if (mp->ma_used == 0) { + if (deflt) { + Py_INCREF(deflt); + return deflt; + } PyErr_SetString(PyExc_KeyError, "pop(): dictionary is empty"); return NULL; @@ -1563,6 +1570,10 @@ dict_pop(dictobject *mp, PyObject *key) } ep = (mp->ma_lookup)(mp, key, hash); if (ep->me_value == NULL) { + if (deflt) { + Py_INCREF(deflt); + return deflt; + } PyErr_SetObject(PyExc_KeyError, key); return NULL; } @@ -1719,7 +1730,8 @@ PyDoc_STRVAR(setdefault_doc__, "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"); PyDoc_STRVAR(pop__doc__, -"D.pop(k) -> v, remove specified key and return the corresponding value"); +"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\ +If key is not found, d is returned if given, otherwise KeyError is raised"); PyDoc_STRVAR(popitem__doc__, "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ @@ -1763,7 +1775,7 @@ static PyMethodDef mapp_methods[] = { get__doc__}, {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, setdefault_doc__}, - {"pop", (PyCFunction)dict_pop, METH_O, + {"pop", (PyCFunction)dict_pop, METH_VARARGS, pop__doc__}, {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, popitem__doc__}, |