gh-107122: Add clear method to dbm.gdbm.module (gh-107127)

This commit is contained in:
Dong-hee Na 2023-07-23 22:51:12 +09:00 committed by GitHub
parent e59da0c4f2
commit b273837fea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 1 deletions

View file

@ -561,6 +561,37 @@ _gdbm_gdbm_sync_impl(gdbmobject *self, PyTypeObject *cls)
Py_RETURN_NONE;
}
/*[clinic input]
_gdbm.gdbm.clear
cls: defining_class
/
Remove all items from the database.
[clinic start generated code]*/
static PyObject *
_gdbm_gdbm_clear_impl(gdbmobject *self, PyTypeObject *cls)
/*[clinic end generated code: output=673577c573318661 input=34136d52fcdd4210]*/
{
_gdbm_state *state = PyType_GetModuleState(cls);
assert(state != NULL);
check_gdbmobject_open(self, state->gdbm_error);
datum key;
// Invalidate cache
self->di_size = -1;
while (1) {
key = gdbm_firstkey(self->di_dbm);
if (key.dptr == NULL) {
break;
}
if (gdbm_delete(self->di_dbm, key) < 0) {
PyErr_SetString(state->gdbm_error, "cannot delete item from database");
return NULL;
}
}
Py_RETURN_NONE;
}
static PyObject *
gdbm__enter__(PyObject *self, PyObject *args)
{
@ -582,6 +613,7 @@ static PyMethodDef gdbm_methods[] = {
_GDBM_GDBM_SYNC_METHODDEF
_GDBM_GDBM_GET_METHODDEF
_GDBM_GDBM_SETDEFAULT_METHODDEF
_GDBM_GDBM_CLEAR_METHODDEF
{"__enter__", gdbm__enter__, METH_NOARGS, NULL},
{"__exit__", gdbm__exit__, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */