Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 810d5d8

Browse files
authored
gh-107089: Improve Shelf.clear method performance (gh-107090)
1 parent 11c055f commit 810d5d8

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/shelve.py

+7
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ def __init__(self, filename, flag='c', protocol=None, writeback=False):
226226
import dbm
227227
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
228228

229+
def clear(self):
230+
"""Remove all items from the shelf."""
231+
# Call through to the clear method on dbm-backed shelves.
232+
# see https://github.com/python/cpython/issues/107089
233+
self.cache.clear()
234+
self.dict.clear()
235+
229236

230237
def open(filename, flag='c', protocol=None, writeback=False):
231238
"""Open a persistent dictionary for reading and writing.

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ Edward Catmur
289289
Lorenzo M. Catucci
290290
Bruno Cauet
291291
Donn Cave
292+
James Cave
292293
Charles Cazabon
293294
Jesús Cea Avión
294295
Per Cederqvist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Shelves opened with :func:`shelve.open` have a much faster :meth:`clear`
2+
method. Patch by James Cave.

0 commit comments

Comments
 (0)