summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2023-07-23 13:51:12 (GMT)
committerGitHub <noreply@github.com>2023-07-23 13:51:12 (GMT)
commitb273837fea129df8477da557ad3cb23a0ed12c20 (patch)
treeb633c744e9f4711edefef7ab56a12aa6ce57483f /Modules
parente59da0c4f283b966ccb175fb94460f58211a9704 (diff)
downloadcpython-b273837fea129df8477da557ad3cb23a0ed12c20.zip
cpython-b273837fea129df8477da557ad3cb23a0ed12c20.tar.gz
cpython-b273837fea129df8477da557ad3cb23a0ed12c20.tar.bz2
gh-107122: Add clear method to dbm.gdbm.module (gh-107127)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_gdbmmodule.c32
-rw-r--r--Modules/clinic/_gdbmmodule.c.h24
2 files changed, 55 insertions, 1 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index bedbdc0..47f2da8 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -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 */
diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h
index 5c6aeee..76f6db3 100644
--- a/Modules/clinic/_gdbmmodule.c.h
+++ b/Modules/clinic/_gdbmmodule.c.h
@@ -247,6 +247,28 @@ _gdbm_gdbm_sync(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_s
return _gdbm_gdbm_sync_impl(self, cls);
}
+PyDoc_STRVAR(_gdbm_gdbm_clear__doc__,
+"clear($self, /)\n"
+"--\n"
+"\n"
+"Remove all items from the database.");
+
+#define _GDBM_GDBM_CLEAR_METHODDEF \
+ {"clear", _PyCFunction_CAST(_gdbm_gdbm_clear), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _gdbm_gdbm_clear__doc__},
+
+static PyObject *
+_gdbm_gdbm_clear_impl(gdbmobject *self, PyTypeObject *cls);
+
+static PyObject *
+_gdbm_gdbm_clear(gdbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ if (nargs) {
+ PyErr_SetString(PyExc_TypeError, "clear() takes no arguments");
+ return NULL;
+ }
+ return _gdbm_gdbm_clear_impl(self, cls);
+}
+
PyDoc_STRVAR(dbmopen__doc__,
"open($module, filename, flags=\'r\', mode=0o666, /)\n"
"--\n"
@@ -322,4 +344,4 @@ skip_optional:
exit:
return return_value;
}
-/*[clinic end generated code: output=c6e721d82335adb3 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8c613cbd88e57480 input=a9049054013a1b77]*/