Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-107122: Add clear method to dbm.ndbm module #107126

Merged
merged 7 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/dbm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@
modified by the prevailing umask).


The object returned by :func:`.open` supports the same basic functionality as

Check warning on line 70 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: keys

Check warning on line 70 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: get

Check warning on line 70 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: setdefault
dictionaries; keys and their corresponding values can be stored, retrieved, and
deleted, and the :keyword:`in` operator and the :meth:`keys` method are
available, as well as :meth:`get` and :meth:`setdefault`.

.. versionchanged:: 3.2
:meth:`get` and :meth:`setdefault` are now available in all database modules.

Check warning on line 76 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: get

Check warning on line 76 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: setdefault

.. versionchanged:: 3.8
Deleting a key from a read-only database raises database module specific error
Expand Down Expand Up @@ -145,7 +145,7 @@
``gdbm`` instead to provide some additional functionality. Please note that the
file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible.

The :mod:`dbm.gnu` module provides an interface to the GNU DBM library.

Check warning on line 148 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: items

Check warning on line 148 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: values
``dbm.gnu.gdbm`` objects behave like mappings (dictionaries), except that keys and
values are always converted to bytes before storing. Printing a ``gdbm``
object doesn't print the
Expand All @@ -160,7 +160,7 @@

.. function:: open(filename[, flag[, mode]])

Open a ``gdbm`` database and return a :class:`gdbm` object. The *filename*

Check warning on line 163 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: gdbm
argument is the name of the database file.

The optional *flag* argument can be:
Expand Down Expand Up @@ -197,7 +197,7 @@
| ``'u'`` | Do not lock database. |
+---------+--------------------------------------------+

Not all flags are valid for all versions of ``gdbm``. The module constant

Check warning on line 200 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: open_flags
:const:`open_flags` is a string of supported flag characters. The exception
:exc:`error` is raised if an invalid flag is specified.

Expand Down Expand Up @@ -263,7 +263,7 @@

--------------

The :mod:`dbm.ndbm` module provides an interface to the Unix "(n)dbm" library.

Check warning on line 266 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: items
Dbm objects behave like mappings (dictionaries), except that keys and values are
always stored as bytes. Printing a ``dbm`` object doesn't print the keys and
values, and the :meth:`items` and :meth:`values` methods are not supported.
Expand Down Expand Up @@ -320,6 +320,12 @@

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')]
corona10 marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 13 additions & 0 deletions Lib/test/test_dbm_ndbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ def test_bool_on_closed_db_raises(self):
db['a'] = 'b'
self.assertRaises(dbm.ndbm.error, bool, db)

def test_clear(self):
kvs = [('foo', 'bar'), ('1234', '5678')]
with dbm.ndbm.open(self.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.ndbm.clear` to :mod:`dbm.ndbm`. Patch By Dong-hee Na.
33 changes: 33 additions & 0 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
corona10 marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
}
Py_RETURN_NONE;
}

static PyObject *
dbm__enter__(PyObject *self, PyObject *args)
{
Expand All @@ -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 */
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.

Loading