summaryrefslogtreecommitdiffstats
path: root/Lib/dbm
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-12-07 08:56:39 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-12-07 08:56:39 (GMT)
commit028ace1ccbabd40f6491b0337054a341be47a39d (patch)
tree8a404a9628b4ad565eaa49d2669f57b1b5c635e1 /Lib/dbm
parente5031260741e18968faedd19c2ac617ddc8f5c32 (diff)
downloadcpython-028ace1ccbabd40f6491b0337054a341be47a39d.zip
cpython-028ace1ccbabd40f6491b0337054a341be47a39d.tar.gz
cpython-028ace1ccbabd40f6491b0337054a341be47a39d.tar.bz2
Issue #28847: dbm.dumb now supports reading read-only files and no longer
writes the index file when it is not changed.
Diffstat (limited to 'Lib/dbm')
-rw-r--r--Lib/dbm/dumb.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index 7777a7c..e6d6505 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -47,6 +47,7 @@ class _Database(collections.MutableMapping):
def __init__(self, filebasename, mode, flag='c'):
self._mode = mode
+ self._readonly = (flag == 'r')
# The directory file is a text file. Each line looks like
# "%r, (%d, %d)\n" % (key, pos, siz)
@@ -91,8 +92,9 @@ class _Database(collections.MutableMapping):
try:
f = _io.open(self._dirfile, 'r', encoding="Latin-1")
except OSError:
- pass
+ self._modified = not self._readonly
else:
+ self._modified = False
with f:
for line in f:
line = line.rstrip()
@@ -107,7 +109,7 @@ class _Database(collections.MutableMapping):
# CAUTION: It's vital that _commit() succeed, and _commit() can
# be called from __del__(). Therefore we must never reference a
# global in this routine.
- if self._index is None:
+ if self._index is None or not self._modified:
return # nothing to do
try:
@@ -187,6 +189,7 @@ class _Database(collections.MutableMapping):
elif not isinstance(val, (bytes, bytearray)):
raise TypeError("values must be bytes or strings")
self._verify_open()
+ self._modified = True
if key not in self._index:
self._addkey(key, self._addval(val))
else:
@@ -215,6 +218,7 @@ class _Database(collections.MutableMapping):
if isinstance(key, str):
key = key.encode('utf-8')
self._verify_open()
+ self._modified = True
# 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