Skip to content

Commit b273837

Browse files
authored
gh-107122: Add clear method to dbm.gdbm.module (gh-107127)
1 parent e59da0c commit b273837

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

Doc/library/dbm.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ supported.
245245

246246
Close the ``gdbm`` database.
247247

248+
.. method:: gdbm.clear()
249+
250+
Remove all items from the ``gdbm`` database.
251+
252+
.. versionadded:: 3.13
253+
254+
248255
:mod:`dbm.ndbm` --- Interface based on ndbm
249256
-------------------------------------------
250257

Lib/test/test_dbm_gnu.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ def test_open_with_bytes_path(self):
192192
def test_open_with_pathlib_bytes_path(self):
193193
gdbm.open(FakePath(os.fsencode(filename)), "c").close()
194194

195+
def test_clear(self):
196+
kvs = [('foo', 'bar'), ('1234', '5678')]
197+
with gdbm.open(filename, 'c') as db:
198+
for k, v in kvs:
199+
db[k] = v
200+
self.assertIn(k, db)
201+
self.assertEqual(len(db), len(kvs))
202+
203+
db.clear()
204+
for k, v in kvs:
205+
self.assertNotIn(k, db)
206+
self.assertEqual(len(db), 0)
207+
208+
195209

196210
if __name__ == '__main__':
197211
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :meth:`dbm.gdbm.clear` to :mod:`dbm.gdbm`. Patch By Dong-hee Na.

Modules/_gdbmmodule.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,37 @@ _gdbm_gdbm_sync_impl(gdbmobject *self, PyTypeObject *cls)
561561
Py_RETURN_NONE;
562562
}
563563

564+
/*[clinic input]
565+
_gdbm.gdbm.clear
566+
cls: defining_class
567+
/
568+
Remove all items from the database.
569+
570+
[clinic start generated code]*/
571+
572+
static PyObject *
573+
_gdbm_gdbm_clear_impl(gdbmobject *self, PyTypeObject *cls)
574+
/*[clinic end generated code: output=673577c573318661 input=34136d52fcdd4210]*/
575+
{
576+
_gdbm_state *state = PyType_GetModuleState(cls);
577+
assert(state != NULL);
578+
check_gdbmobject_open(self, state->gdbm_error);
579+
datum key;
580+
// Invalidate cache
581+
self->di_size = -1;
582+
while (1) {
583+
key = gdbm_firstkey(self->di_dbm);
584+
if (key.dptr == NULL) {
585+
break;
586+
}
587+
if (gdbm_delete(self->di_dbm, key) < 0) {
588+
PyErr_SetString(state->gdbm_error, "cannot delete item from database");
589+
return NULL;
590+
}
591+
}
592+
Py_RETURN_NONE;
593+
}
594+
564595
static PyObject *
565596
gdbm__enter__(PyObject *self, PyObject *args)
566597
{
@@ -582,6 +613,7 @@ static PyMethodDef gdbm_methods[] = {
582613
_GDBM_GDBM_SYNC_METHODDEF
583614
_GDBM_GDBM_GET_METHODDEF
584615
_GDBM_GDBM_SETDEFAULT_METHODDEF
616+
_GDBM_GDBM_CLEAR_METHODDEF
585617
{"__enter__", gdbm__enter__, METH_NOARGS, NULL},
586618
{"__exit__", gdbm__exit__, METH_VARARGS, NULL},
587619
{NULL, NULL} /* sentinel */

Modules/clinic/_gdbmmodule.c.h

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy