gh-107089: Improve Shelf.clear method performance (gh-107090)

This commit is contained in:
James Cave 2023-07-28 20:08:11 -04:00 committed by GitHub
parent 11c055f5ff
commit 810d5d87d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 0 deletions

View file

@ -226,6 +226,13 @@ def __init__(self, filename, flag='c', protocol=None, writeback=False):
import dbm
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
def clear(self):
"""Remove all items from the shelf."""
# Call through to the clear method on dbm-backed shelves.
# see https://github.com/python/cpython/issues/107089
self.cache.clear()
self.dict.clear()
def open(filename, flag='c', protocol=None, writeback=False):
"""Open a persistent dictionary for reading and writing.

View file

@ -289,6 +289,7 @@ Edward Catmur
Lorenzo M. Catucci
Bruno Cauet
Donn Cave
James Cave
Charles Cazabon
Jesús Cea Avión
Per Cederqvist

View file

@ -0,0 +1,2 @@
Shelves opened with :func:`shelve.open` have a much faster :meth:`clear`
method. Patch by James Cave.