summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-06-25 17:35:31 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-06-25 17:35:31 (GMT)
commit65c623de74cba3eff6d8f5f4c37cfec89c0fde43 (patch)
tree7f57e0d51b7613063b6f16c7d888b377b30993ab
parent0c4baa40ec73044b79d330e04b413ca04f3baedf (diff)
downloadcpython-65c623de74cba3eff6d8f5f4c37cfec89c0fde43.zip
cpython-65c623de74cba3eff6d8f5f4c37cfec89c0fde43.tar.gz
cpython-65c623de74cba3eff6d8f5f4c37cfec89c0fde43.tar.bz2
Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
files closing. Patch by Claudiu Popa.
-rw-r--r--Lib/dbm/dumb.py69
-rw-r--r--Misc/NEWS3
2 files changed, 36 insertions, 36 deletions
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index 17854ee..8f48aad 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -68,9 +68,10 @@ class _Database(collections.MutableMapping):
try:
f = _io.open(self._datfile, 'r', encoding="Latin-1")
except OSError:
- f = _io.open(self._datfile, 'w', encoding="Latin-1")
- self._chmod(self._datfile)
- f.close()
+ with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
+ self._chmod(self._datfile)
+ else:
+ f.close()
self._update()
# Read directory file into the in-memory index dict.
@@ -81,12 +82,12 @@ class _Database(collections.MutableMapping):
except OSError:
pass
else:
- for line in f:
- line = line.rstrip()
- key, pos_and_siz_pair = eval(line)
- key = key.encode('Latin-1')
- self._index[key] = pos_and_siz_pair
- f.close()
+ with f:
+ for line in f:
+ line = line.rstrip()
+ key, pos_and_siz_pair = eval(line)
+ key = key.encode('Latin-1')
+ self._index[key] = pos_and_siz_pair
# Write the index dict to the directory file. The original directory
# file (if any) is renamed with a .bak extension first. If a .bak
@@ -108,13 +109,13 @@ class _Database(collections.MutableMapping):
except OSError:
pass
- f = self._io.open(self._dirfile, 'w', encoding="Latin-1")
- self._chmod(self._dirfile)
- for key, pos_and_siz_pair in self._index.items():
- # Use Latin-1 since it has no qualms with any value in any
- # position; UTF-8, though, does care sometimes.
- f.write("%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair))
- f.close()
+ with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f:
+ self._chmod(self._dirfile)
+ for key, pos_and_siz_pair in self._index.items():
+ # Use Latin-1 since it has no qualms with any value in any
+ # position; UTF-8, though, does care sometimes.
+ entry = "%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair)
+ f.write(entry)
sync = _commit
@@ -127,10 +128,9 @@ class _Database(collections.MutableMapping):
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)
- dat = f.read(siz)
- f.close()
+ with _io.open(self._datfile, 'rb') as f:
+ f.seek(pos)
+ dat = f.read(siz)
return dat
# Append val to the data file, starting at a _BLOCKSIZE-aligned
@@ -138,14 +138,13 @@ class _Database(collections.MutableMapping):
# to get to an aligned offset. Return pair
# (starting offset of val, len(val))
def _addval(self, val):
- f = _io.open(self._datfile, 'rb+')
- f.seek(0, 2)
- pos = int(f.tell())
- npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
- f.write(b'\0'*(npos-pos))
- pos = npos
- f.write(val)
- f.close()
+ with _io.open(self._datfile, 'rb+') as f:
+ f.seek(0, 2)
+ pos = int(f.tell())
+ npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
+ f.write(b'\0'*(npos-pos))
+ pos = npos
+ f.write(val)
return (pos, len(val))
# Write val to the data file, starting at offset pos. The caller
@@ -153,10 +152,9 @@ class _Database(collections.MutableMapping):
# pos to hold val, without overwriting some other value. Return
# pair (pos, len(val)).
def _setval(self, pos, val):
- f = _io.open(self._datfile, 'rb+')
- f.seek(pos)
- f.write(val)
- f.close()
+ with _io.open(self._datfile, 'rb+') as f:
+ f.seek(pos)
+ f.write(val)
return (pos, len(val))
# key is a new key whose associated value starts in the data file
@@ -164,10 +162,9 @@ class _Database(collections.MutableMapping):
# the in-memory index dict, and append one to the directory file.
def _addkey(self, key, pos_and_siz_pair):
self._index[key] = pos_and_siz_pair
- f = _io.open(self._dirfile, 'a', encoding="Latin-1")
- self._chmod(self._dirfile)
- f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
- f.close()
+ with _io.open(self._dirfile, 'a', encoding="Latin-1") as f:
+ self._chmod(self._dirfile)
+ f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
def __setitem__(self, key, val):
if isinstance(key, str):
diff --git a/Misc/NEWS b/Misc/NEWS
index 7033bb4..487799c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
Library
-------
+- Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
+ files closing. Patch by Claudiu Popa.
+
- Issue #21491: socketserver: Fix a race condition in child processes reaping.
- Issue #21832: Require named tuple inputs to be exact strings.