From 810d5d87d9fe8d86aad99e48cef4f78a72e16ccf Mon Sep 17 00:00:00 2001 From: James Cave Date: Fri, 28 Jul 2023 20:08:11 -0400 Subject: gh-107089: Improve Shelf.clear method performance (gh-107090) --- Lib/shelve.py | 7 +++++++ Misc/ACKS | 1 + .../next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst | 2 ++ 3 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst diff --git a/Lib/shelve.py b/Lib/shelve.py index e053c39..5058471 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -226,6 +226,13 @@ class DbfilenameShelf(Shelf): 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. diff --git a/Misc/ACKS b/Misc/ACKS index fadf488..8b8c5ad 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -289,6 +289,7 @@ Edward Catmur Lorenzo M. Catucci Bruno Cauet Donn Cave +James Cave Charles Cazabon Jesús Cea Avión Per Cederqvist diff --git a/Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst b/Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst new file mode 100644 index 0000000..9d5ba1a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst @@ -0,0 +1,2 @@ +Shelves opened with :func:`shelve.open` have a much faster :meth:`clear` +method. Patch by James Cave. \ No newline at end of file -- cgit v0.12