Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0da7e03

Browse files
committed
And the gdbm module's test now passes again.
1 parent ef67111 commit 0da7e03

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

Modules/gdbmmodule.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,41 @@ dbm_keys(register dbmobject *dp, PyObject *unused)
241241
return v;
242242
}
243243

244-
PyDoc_STRVAR(dbm_contains__doc__,
245-
"__contains__(key) -> bool\n\
246-
Find out whether or not the database contains a given key.");
247-
248-
static PyObject *
249-
dbm_contains(register dbmobject *dp, PyObject *args)
244+
static int
245+
dbm_contains(PyObject *self, PyObject *arg)
250246
{
247+
dbmobject *dp = (dbmobject *)self;
251248
datum key;
252249

253-
if (!PyArg_ParseTuple(args, "s#:contains", &key.dptr, &key.dsize))
254-
return NULL;
255-
check_dbmobject_open(dp);
256-
return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key));
250+
if ((dp)->di_dbm == NULL) {
251+
PyErr_SetString(DbmError,
252+
"GDBM object has already been closed");
253+
return -1;
254+
}
255+
if (!PyString_Check(arg)) {
256+
PyErr_Format(PyExc_TypeError,
257+
"gdbm key must be string, not %.100s",
258+
arg->ob_type->tp_name);
259+
return -1;
260+
}
261+
key.dptr = PyString_AS_STRING(arg);
262+
key.dsize = PyString_GET_SIZE(arg);
263+
return gdbm_exists(dp->di_dbm, key);
257264
}
258265

266+
static PySequenceMethods dbm_as_sequence = {
267+
0, /* sq_length */
268+
0, /* sq_concat */
269+
0, /* sq_repeat */
270+
0, /* sq_item */
271+
0, /* sq_slice */
272+
0, /* sq_ass_item */
273+
0, /* sq_ass_slice */
274+
dbm_contains, /* sq_contains */
275+
0, /* sq_inplace_concat */
276+
0, /* sq_inplace_repeat */
277+
};
278+
259279
PyDoc_STRVAR(dbm_firstkey__doc__,
260280
"firstkey() -> key\n\
261281
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)
355375
static PyMethodDef dbm_methods[] = {
356376
{"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__},
357377
{"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__},
358-
{"__contains__",(PyCFunction)dbm_contains,METH_VARARGS, dbm_contains__doc__},
359378
{"firstkey", (PyCFunction)dbm_firstkey,METH_NOARGS, dbm_firstkey__doc__},
360379
{"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__},
361380
{"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__},
@@ -382,7 +401,7 @@ static PyTypeObject Dbmtype = {
382401
0, /*tp_compare*/
383402
0, /*tp_repr*/
384403
0, /*tp_as_number*/
385-
0, /*tp_as_sequence*/
404+
&dbm_as_sequence, /*tp_as_sequence*/
386405
&dbm_as_mapping, /*tp_as_mapping*/
387406
0, /*tp_hash*/
388407
0, /*tp_call*/
@@ -498,7 +517,8 @@ PyMODINIT_FUNC
498517
initgdbm(void) {
499518
PyObject *m, *d, *s;
500519

501-
Dbmtype.ob_type = &PyType_Type;
520+
if (PyType_Ready(&Dbmtype) < 0)
521+
return;
502522
m = Py_InitModule4("gdbm", dbmmodule_methods,
503523
gdbmmodule__doc__, (PyObject *)NULL,
504524
PYTHON_API_VERSION);

0 commit comments

Comments
 (0)