summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Cave <jcave137@gmail.com>2023-07-29 00:08:11 (GMT)
committerGitHub <noreply@github.com>2023-07-29 00:08:11 (GMT)
commit810d5d87d9fe8d86aad99e48cef4f78a72e16ccf (patch)
tree3d5155864548a0ad86aeadb1ea009c9ffa99078e
parent11c055f5ff1a353de6d2e77f2af24aaa782878ba (diff)
downloadcpython-810d5d87d9fe8d86aad99e48cef4f78a72e16ccf.zip
cpython-810d5d87d9fe8d86aad99e48cef4f78a72e16ccf.tar.gz
cpython-810d5d87d9fe8d86aad99e48cef4f78a72e16ccf.tar.bz2
gh-107089: Improve Shelf.clear method performance (gh-107090)
-rw-r--r--Lib/shelve.py7
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2023-07-22-21-57-34.gh-issue-107089.Dnget2.rst2
3 files changed, 10 insertions, 0 deletions
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