summaryrefslogtreecommitdiffstats
path: root/Lib/dbm
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-04-26 20:56:52 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-04-26 20:56:52 (GMT)
commite3083d3ad541432e2420869c7ed56fb9aff7a453 (patch)
treec1bffd6775a90a4e341e13c5fd8a2a6229461e5a /Lib/dbm
parentd4992dccfb52ef4c0f4f9417b3e18fd7bd56476b (diff)
downloadcpython-e3083d3ad541432e2420869c7ed56fb9aff7a453.zip
cpython-e3083d3ad541432e2420869c7ed56fb9aff7a453.tar.gz
cpython-e3083d3ad541432e2420869c7ed56fb9aff7a453.tar.bz2
make operations on closed dumb databases raise a consistent exception (closes #19385)
Patch by Claudiu Popa.
Diffstat (limited to 'Lib/dbm')
-rw-r--r--Lib/dbm/dumb.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index ba6a20d..15dad9c 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -118,9 +118,14 @@ class _Database(collections.MutableMapping):
sync = _commit
+ def _verify_open(self):
+ if self._index is None:
+ raise error('DBM object has already been closed')
+
def __getitem__(self, key):
if isinstance(key, str):
key = key.encode('utf-8')
+ self._verify_open()
pos, siz = self._index[key] # may raise KeyError
f = _io.open(self._datfile, 'rb')
f.seek(pos)
@@ -173,6 +178,7 @@ class _Database(collections.MutableMapping):
val = val.encode('utf-8')
elif not isinstance(val, (bytes, bytearray)):
raise TypeError("values must be bytes or strings")
+ self._verify_open()
if key not in self._index:
self._addkey(key, self._addval(val))
else:
@@ -200,6 +206,7 @@ class _Database(collections.MutableMapping):
def __delitem__(self, key):
if isinstance(key, str):
key = key.encode('utf-8')
+ self._verify_open()
# The blocks used by the associated value are lost.
del self._index[key]
# XXX It's unclear why we do a _commit() here (the code always
@@ -209,21 +216,26 @@ class _Database(collections.MutableMapping):
self._commit()
def keys(self):
+ self._verify_open()
return list(self._index.keys())
def items(self):
+ self._verify_open()
return [(key, self[key]) for key in self._index.keys()]
def __contains__(self, key):
if isinstance(key, str):
key = key.encode('utf-8')
+ self._verify_open()
return key in self._index
def iterkeys(self):
+ self._verify_open()
return iter(self._index.keys())
__iter__ = iterkeys
def __len__(self):
+ self._verify_open()
return len(self._index)
def close(self):