diff options
author | Georg Brandl <georg@python.org> | 2010-12-04 09:14:36 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-04 09:14:36 (GMT) |
commit | d9e833c70a22558236fc863250870837d4ad8b16 (patch) | |
tree | b9bb6ff8d06dcb05975f5281d48a55babbb8582a /Modules | |
parent | da72231c9feeef6983f05dc963a69cf6f1c8de29 (diff) | |
download | cpython-d9e833c70a22558236fc863250870837d4ad8b16.zip cpython-d9e833c70a22558236fc863250870837d4ad8b16.tar.gz cpython-d9e833c70a22558236fc863250870837d4ad8b16.tar.bz2 |
#6045: provide at least get() and setdefault() for all dbm modules.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_gdbmmodule.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index f15fefd..474561b 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -135,6 +135,28 @@ dbm_subscript(dbmobject *dp, register PyObject *key) return v; } +PyDoc_STRVAR(dbm_get__doc__, +"get(key[, default]) -> value\n\ +Get the value for key, or default if not present; if not given,\n\ +default is None."); + +static PyObject * +dbm_get(dbmobject *dp, PyObject *args) +{ + PyObject *v, *res; + PyObject *def = Py_None; + + if (!PyArg_UnpackTuple(args, "get", 1, 2, &v, &def)) + return NULL; + res = dbm_subscript(dp, v); + if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { + PyErr_Clear(); + Py_INCREF(def); + return def; + } + return res; +} + static int dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) { @@ -176,6 +198,29 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) return 0; } +PyDoc_STRVAR(dbm_setdefault__doc__, +"setdefault(key[, default]) -> value\n\ +Get value for key, or set it to default and return default if not present;\n\ +if not given, default is None."); + +static PyObject * +dbm_setdefault(dbmobject *dp, PyObject *args) +{ + PyObject *v, *res; + PyObject *def = Py_None; + + if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &v, &def)) + return NULL; + res = dbm_subscript(dp, v); + if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { + PyErr_Clear(); + if (dbm_ass_sub(dp, v, def) < 0) + return NULL; + return dbm_subscript(dp, v); + } + return res; +} + static PyMappingMethods dbm_as_mapping = { (lenfunc)dbm_length, /*mp_length*/ (binaryfunc)dbm_subscript, /*mp_subscript*/ @@ -378,6 +423,8 @@ static PyMethodDef dbm_methods[] = { {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__}, {"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__}, {"sync", (PyCFunction)dbm_sync, METH_NOARGS, dbm_sync__doc__}, + {"get", (PyCFunction)dbm_get, METH_VARARGS, dbm_get__doc__}, + {"setdefault",(PyCFunction)dbm_setdefault,METH_VARARGS, dbm_setdefault__doc__}, {NULL, NULL} /* sentinel */ }; |