summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-08-19 23:18:48 +0000
committerGuido van Rossum <guido@python.org>2006-08-19 23:18:48 +0000
commit0da7e03e12abd760085bd7e76df617d69fd94822 (patch)
treeb5bb2c5c60ca72fc2254ff13bcdc3e633fdf27ca /Modules
parentef67111634dc8a977e4f788b95d470c2aabfb36f (diff)
downloadcpython-git-0da7e03e12abd760085bd7e76df617d69fd94822.tar.gz
And the gdbm module's test now passes again.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/gdbmmodule.c46
1 files changed, 33 insertions, 13 deletions
diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c
index 0da93ec36d..0ffb4b4f16 100644
--- a/Modules/gdbmmodule.c
+++ b/Modules/gdbmmodule.c
@@ -241,21 +241,41 @@ dbm_keys(register dbmobject *dp, PyObject *unused)
return v;
}
-PyDoc_STRVAR(dbm_contains__doc__,
-"__contains__(key) -> bool\n\
-Find out whether or not the database contains a given key.");
-
-static PyObject *
-dbm_contains(register dbmobject *dp, PyObject *args)
+static int
+dbm_contains(PyObject *self, PyObject *arg)
{
+ dbmobject *dp = (dbmobject *)self;
datum key;
- if (!PyArg_ParseTuple(args, "s#:contains", &key.dptr, &key.dsize))
- return NULL;
- check_dbmobject_open(dp);
- return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key));
+ if ((dp)->di_dbm == NULL) {
+ PyErr_SetString(DbmError,
+ "GDBM object has already been closed");
+ return -1;
+ }
+ if (!PyString_Check(arg)) {
+ PyErr_Format(PyExc_TypeError,
+ "gdbm key must be string, not %.100s",
+ arg->ob_type->tp_name);
+ return -1;
+ }
+ key.dptr = PyString_AS_STRING(arg);
+ key.dsize = PyString_GET_SIZE(arg);
+ return gdbm_exists(dp->di_dbm, key);
}
+static PySequenceMethods dbm_as_sequence = {
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ dbm_contains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
+};
+
PyDoc_STRVAR(dbm_firstkey__doc__,
"firstkey() -> key\n\
It's possible to loop over every key in the database using this method\n\
@@ -355,7 +375,6 @@ dbm_sync(register dbmobject *dp, PyObject *unused)
static PyMethodDef dbm_methods[] = {
{"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__},
{"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__},
- {"__contains__",(PyCFunction)dbm_contains,METH_VARARGS, dbm_contains__doc__},
{"firstkey", (PyCFunction)dbm_firstkey,METH_NOARGS, dbm_firstkey__doc__},
{"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__},
{"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__},
@@ -382,7 +401,7 @@ static PyTypeObject Dbmtype = {
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
+ &dbm_as_sequence, /*tp_as_sequence*/
&dbm_as_mapping, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
@@ -498,7 +517,8 @@ PyMODINIT_FUNC
initgdbm(void) {
PyObject *m, *d, *s;
- Dbmtype.ob_type = &PyType_Type;
+ if (PyType_Ready(&Dbmtype) < 0)
+ return;
m = Py_InitModule4("gdbm", dbmmodule_methods,
gdbmmodule__doc__, (PyObject *)NULL,
PYTHON_API_VERSION);