summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-08-08 16:12:54 (GMT)
committerGuido van Rossum <guido@python.org>2000-08-08 16:12:54 (GMT)
commit164452cec416fbe032c1173d58ad89adab38820b (patch)
treedc5b8769a82ffc778d27a8e0679bda4c5125100e /Objects/dictobject.c
parent2b042ded19bc7efa43551da297c29dc142b7d73c (diff)
downloadcpython-164452cec416fbe032c1173d58ad89adab38820b.zip
cpython-164452cec416fbe032c1173d58ad89adab38820b.tar.gz
cpython-164452cec416fbe032c1173d58ad89adab38820b.tar.bz2
Barry's patch to implement the new setdefault() method.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 4fafb8a..ddd8eb8 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -950,6 +950,41 @@ dict_get(register dictobject *mp, PyObject *args)
static PyObject *
+dict_setdefault(register dictobject *mp, PyObject *args)
+{
+ PyObject *key;
+ PyObject *failobj = Py_None;
+ PyObject *val = NULL;
+ long hash;
+
+ if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
+ return NULL;
+ if (mp->ma_table == NULL)
+ goto finally;
+
+#ifdef CACHE_HASH
+ if (!PyString_Check(key) ||
+ (hash = ((PyStringObject *) key)->ob_shash) == -1)
+#endif
+ {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ val = lookdict(mp, key, hash)->me_value;
+
+ finally:
+ if (val == NULL) {
+ val = failobj;
+ if (PyDict_SetItem((PyObject*)mp, key, failobj))
+ val = NULL;
+ }
+ Py_XINCREF(val);
+ return val;
+}
+
+
+static PyObject *
dict_clear(register dictobject *mp, PyObject *args)
{
if (!PyArg_NoArgs(args))
@@ -993,6 +1028,7 @@ static PyMethodDef mapp_methods[] = {
{"clear", (PyCFunction)dict_clear},
{"copy", (PyCFunction)dict_copy},
{"get", (PyCFunction)dict_get, METH_VARARGS},
+ {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS},
{NULL, NULL} /* sentinel */
};