diff options
author | Dong-hee Na <donghee.na@python.org> | 2023-07-23 14:26:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-23 14:26:23 (GMT) |
commit | 0ae4870d09de82ed5063b6998c172cc63628437e (patch) | |
tree | 467aa07b0291a44a37ea9de2654c60ff9464ffcb /Modules/_dbmmodule.c | |
parent | b3c34e55c053846beb35f5e4253ef237b3494bd0 (diff) | |
download | cpython-0ae4870d09de82ed5063b6998c172cc63628437e.zip cpython-0ae4870d09de82ed5063b6998c172cc63628437e.tar.gz cpython-0ae4870d09de82ed5063b6998c172cc63628437e.tar.bz2 |
gh-107122: Add clear method to dbm.ndbm module (gh-107126)
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r-- | Modules/_dbmmodule.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 5be444d..bd80769 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -414,6 +414,38 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key, return default_value; } +/*[clinic input] +_dbm.dbm.clear + cls: defining_class + / +Remove all items from the database. + +[clinic start generated code]*/ + +static PyObject * +_dbm_dbm_clear_impl(dbmobject *self, PyTypeObject *cls) +/*[clinic end generated code: output=8d126b9e1d01a434 input=43aa6ca1acb7f5f5]*/ +{ + _dbm_state *state = PyType_GetModuleState(cls); + assert(state != NULL); + check_dbmobject_open(self, state->dbm_error); + datum key; + // Invalidate cache + self->di_size = -1; + while (1) { + key = dbm_firstkey(self->di_dbm); + if (key.dptr == NULL) { + break; + } + if (dbm_delete(self->di_dbm, key) < 0) { + dbm_clearerr(self->di_dbm); + PyErr_SetString(state->dbm_error, "cannot delete item from database"); + return NULL; + } + } + Py_RETURN_NONE; +} + static PyObject * dbm__enter__(PyObject *self, PyObject *args) { @@ -431,6 +463,7 @@ static PyMethodDef dbm_methods[] = { _DBM_DBM_KEYS_METHODDEF _DBM_DBM_GET_METHODDEF _DBM_DBM_SETDEFAULT_METHODDEF + _DBM_DBM_CLEAR_METHODDEF {"__enter__", dbm__enter__, METH_NOARGS, NULL}, {"__exit__", dbm__exit__, METH_VARARGS, NULL}, {NULL, NULL} /* sentinel */ |