Skip to content

Commit

Permalink
pythongh-107122: Add clear method to dbm.gdbm.module (pythongh-107127)
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 authored and mementum committed Jul 23, 2023
1 parent 2d648af commit c3fb34e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Doc/library/dbm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ supported.

Close the ``gdbm`` database.

.. method:: gdbm.clear()

Remove all items from the ``gdbm`` database.

.. versionadded:: 3.13


:mod:`dbm.ndbm` --- Interface based on ndbm
-------------------------------------------

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_dbm_gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ def test_open_with_bytes_path(self):
def test_open_with_pathlib_bytes_path(self):
gdbm.open(FakePath(os.fsencode(filename)), "c").close()

def test_clear(self):
kvs = [('foo', 'bar'), ('1234', '5678')]
with gdbm.open(filename, 'c') as db:
for k, v in kvs:
db[k] = v
self.assertIn(k, db)
self.assertEqual(len(db), len(kvs))

db.clear()
for k, v in kvs:
self.assertNotIn(k, db)
self.assertEqual(len(db), 0)



if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`dbm.gdbm.clear` to :mod:`dbm.gdbm`. Patch By Dong-hee Na.
32 changes: 32 additions & 0 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 */
Expand Down
24 changes: 23 additions & 1 deletion Modules/clinic/_gdbmmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c3fb34e

Please sign in to comment.