Skip to content

Commit

Permalink
gh-107089: Improve Shelf.clear method performance (gh-107090)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcave committed Jul 29, 2023
1 parent 11c055f commit 810d5d8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Lib/shelve.py
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Expand Up @@ -289,6 +289,7 @@ Edward Catmur
Lorenzo M. Catucci
Bruno Cauet
Donn Cave
James Cave
Charles Cazabon
Jesús Cea Avión
Per Cederqvist
Expand Down
@@ -0,0 +1,2 @@
Shelves opened with :func:`shelve.open` have a much faster :meth:`clear`
method. Patch by James Cave.

0 comments on commit 810d5d8

Please sign in to comment.