]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107089: Improve Shelf.clear method performance (gh-107090)
authorJames Cave <jcave137@gmail.com>
Sat, 29 Jul 2023 00:08:11 +0000 (20:08 -0400)
committerGitHub <noreply@github.com>
Sat, 29 Jul 2023 00:08:11 +0000 (09:08 +0900)
Lib/shelve.py
Misc/ACKS
Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst [new file with mode: 0644]

index e053c397345a07e69dfa8f72a3d5ebbede86a883..50584716e9ea6417f6cb6bf4886f2dc78dd142cf 100644 (file)
@@ -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.
index fadf488888aa8ba1a97e09d68a1dfeae3fc725c5..8b8c5ad8434bd723b993b5df5b2627d6fa22170c 100644 (file)
--- 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 (file)
index 0000000..9d5ba1a
--- /dev/null
@@ -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