Skip to content

Commit

Permalink
pythongh-107122: Add clear method to dbm.ndbm module
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Jul 23, 2023
1 parent e5252c6 commit 9b7a5af
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Doc/library/dbm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ to locate the appropriate header file to simplify building this module.

Close the ``ndbm`` database.

.. method:: ndbm.clear()
Remove all items from the ``ndbm`` database.

.. versionadded:: 3.13


:mod:`dbm.dumb` --- Portable DBM implementation
-----------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_dbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ def test_keys(self):
self.assertNotIn(b'xxx', d)
self.assertRaises(KeyError, lambda: d[b'xxx'])

def test_clear(self):
with dbm.open(_fname, 'c') as d:
self.assertEqual(d.keys(), [])
a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')]
for k, v in a:
d[k] = v
for k, _ in a:
self.assertIn(k, d)
self.assertEqual(len(d), len(a))

d.clear()
self.assertEqual(len(d), 0)
for k, _ in a:
self.assertNotIn(k, d)

def setUp(self):
self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod)
dbm._defaultmod = self.module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`clear` to :mod:`dbm.ndbm`. Patch By Dong-hee Na.
29 changes: 29 additions & 0 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,34 @@ _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);
for (datum key = dbm_firstkey(self->di_dbm); key.dptr; key = dbm_nextkey(self->di_dbm)) {
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;
}
}
if (self->di_size > 0) {
self->di_size = 0;
}
Py_RETURN_NONE;
}

static PyObject *
dbm__enter__(PyObject *self, PyObject *args)
{
Expand All @@ -431,6 +459,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 */
Expand Down
24 changes: 23 additions & 1 deletion Modules/clinic/_dbmmodule.c.h

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

0 comments on commit 9b7a5af

Please sign in to comment.