GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-107122: Add clear method to dbm.ndbm module by corona10 · Pull Request #107126 · python/cpython

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

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.

Sign up for GitHub

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

Merged
corona10 merged 7 commits into python:main from corona10:gh-107122
Jul 23, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
7a930a7
gh-107122: Add clear method to dbm.ndbm module
corona10 Jul 23, 2023
d0f930c
Address code review
corona10 Jul 23, 2023
7a7e826
Be more safer
corona10 Jul 23, 2023
3affdb7
Address code review
corona10 Jul 23, 2023
2be5402
Update test_dbm_ndbm
corona10 Jul 23, 2023
1d14440
Update docs
corona10 Jul 23, 2023
5eb2b13
Simply invalidate cache
corona10 Jul 23, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

6 changes: 6 additions & 0 deletions 6 Doc/library/dbm.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ 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 15 Lib/test/test_dbm.py
Show comments
View file
Open in desktop
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')]
Comment thread
corona10 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

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 13 Lib/test/test_dbm_ndbm.py
Show comments
View file
Open in desktop
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()
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Core and Builtins/2023-07-23-21-16-54.gh-issue-107122.VNuNcq.rst
Show comments
View file
Open in desktop
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 33 Modules/_dbmmodule.c
Show comments
View file
Open in desktop
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");
Comment thread
corona10 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

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 24 Modules/clinic/_dbmmodule.c.h
Show comments
View file
Open in desktop

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

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.