Update of /cvsroot/python/python/dist/src/Modules In directory slayer.i.sourceforge.net:/tmp/cvs-serv16854 Modified Files: dbmmodule.c Log Message: Add a constant "library" to the module which names the library used, based on the available headers. Update comments on the filename extensions used to reflect library differences. Added get() and setdefault() methods to the dbm object. Added docstrings, convert all methods to PyArg_ParseTuple() so that error messages will have the method names. Index: dbmmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Modules/dbmmodule.c,v retrieving revision 2.24 retrieving revision 2.25 diff -C2 -r2.24 -r2.25 *** dbmmodule.c 2000/09/15 03:38:12 2.24 --- dbmmodule.c 2000/09/15 21:35:14 2.25 *************** *** 14,21 **** --- 14,24 ---- #if defined(HAVE_NDBM_H) #include <ndbm.h> + static char *which_dbm = "ndbm"; #elif defined(HAVE_DB1_NDBM_H) #include <db1/ndbm.h> + static char *which_dbm = "BSD db"; #elif defined(HAVE_GDBM_NDBM_H) #include <gdbm/ndbm.h> + static char *which_dbm = "GNU gdbm"; #else #error "No ndbm.h available!" *************** *** 159,165 **** dbm__close(register dbmobject *dp, PyObject *args) { ! if ( !PyArg_NoArgs(args) ) return NULL; ! if ( dp->di_dbm ) dbm_close(dp->di_dbm); dp->di_dbm = NULL; --- 162,168 ---- dbm__close(register dbmobject *dp, PyObject *args) { ! if (!PyArg_ParseTuple(args, ":close")) return NULL; ! if (dp->di_dbm) dbm_close(dp->di_dbm); dp->di_dbm = NULL; *************** *** 175,179 **** int err; ! if (!PyArg_NoArgs(args)) return NULL; check_dbmobject_open(dp); --- 178,182 ---- int err; ! if (!PyArg_ParseTuple(args, ":keys")) return NULL; check_dbmobject_open(dp); *************** *** 203,207 **** datum key, val; ! if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize)) return NULL; check_dbmobject_open(dp); --- 206,210 ---- datum key, val; ! if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize)) return NULL; check_dbmobject_open(dp); *************** *** 210,217 **** } static PyMethodDef dbm_methods[] = { ! {"close", (PyCFunction)dbm__close}, ! {"keys", (PyCFunction)dbm_keys}, ! {"has_key", (PyCFunction)dbm_has_key}, {NULL, NULL} /* sentinel */ }; --- 213,279 ---- } + static PyObject * + dbm_get(register dbmobject *dp, PyObject *args) + { + datum key, val; + PyObject *defvalue = Py_None; + + if (!PyArg_ParseTuple(args, "s#|O:get", + &key.dptr, &key.dsize, &defvalue)) + return NULL; + check_dbmobject_open(dp); + val = dbm_fetch(dp->di_dbm, key); + if (val.dptr != NULL) + return PyString_FromStringAndSize(val.dptr, val.dsize); + else { + Py_INCREF(defvalue); + return defvalue; + } + } + + static PyObject * + dbm_setdefault(register dbmobject *dp, PyObject *args) + { + datum key, val; + PyObject *defvalue = NULL; + + if (!PyArg_ParseTuple(args, "s#|S:setdefault", + &key.dptr, &key.dsize, &defvalue)) + return NULL; + check_dbmobject_open(dp); + val = dbm_fetch(dp->di_dbm, key); + if (val.dptr != NULL) + return PyString_FromStringAndSize(val.dptr, val.dsize); + if (defvalue == NULL) { + defvalue = PyString_FromStringAndSize(NULL, 0); + if (defvalue == NULL) + return NULL; + } + else + Py_INCREF(defvalue); + val.dptr = PyString_AS_STRING(defvalue); + val.dsize = PyString_GET_SIZE(defvalue); + if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { + dbm_clearerr(dp->di_dbm); + PyErr_SetString(DbmError, "Cannot add item to database"); + return NULL; + } + return defvalue; + } + static PyMethodDef dbm_methods[] = { ! {"close", (PyCFunction)dbm__close, METH_VARARGS, ! "close()\nClose the database."}, ! {"keys", (PyCFunction)dbm_keys, METH_VARARGS, ! "keys() -> list\nReturn a list of all keys in the database."}, ! {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, ! "has_key(key} -> boolean\nReturn true iff key is in the database."}, ! {"get", (PyCFunction)dbm_get, METH_VARARGS, ! "get(key[, default]) -> value\n" ! "Return the value for key if present, otherwise default."}, ! {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS, ! "setdefault(key[, default]) -> value\n" ! "Return the value for key if present, otherwise default. If key\n" ! "is not in the database, it is inserted with default as the value."}, {NULL, NULL} /* sentinel */ }; *************** *** 271,275 **** static PyMethodDef dbmmodule_methods[] = { ! { "open", (PyCFunction)dbmopen, 1 }, { 0, 0 }, }; --- 333,339 ---- static PyMethodDef dbmmodule_methods[] = { ! { "open", (PyCFunction)dbmopen, METH_VARARGS, ! "open(path[, flag[, mode]]) -> mapping\n" ! "Return a database object."}, { 0, 0 }, }; *************** *** 277,285 **** DL_EXPORT(void) initdbm(void) { ! PyObject *m, *d; m = Py_InitModule("dbm", dbmmodule_methods); d = PyModule_GetDict(m); ! DbmError = PyErr_NewException("dbm.error", NULL, NULL); if (DbmError != NULL) PyDict_SetItemString(d, "error", DbmError); --- 341,355 ---- DL_EXPORT(void) initdbm(void) { ! PyObject *m, *d, *s; m = Py_InitModule("dbm", dbmmodule_methods); d = PyModule_GetDict(m); ! if (DbmError == NULL) ! DbmError = PyErr_NewException("dbm.error", NULL, NULL); ! s = PyString_FromString(which_dbm); ! if (s != NULL) { ! PyDict_SetItemString(d, "library", s); ! Py_DECREF(s); ! } if (DbmError != NULL) PyDict_SetItemString(d, "error", DbmError);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4