mirror of
https://github.com/python/cpython.git
synced 2025-11-26 21:33:10 +00:00
Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
files closing. Patch by Claudiu Popa.
This commit is contained in:
commit
e5243cc713
2 changed files with 36 additions and 36 deletions
|
|
@ -79,9 +79,10 @@ class _Database(collections.MutableMapping):
|
||||||
try:
|
try:
|
||||||
f = _io.open(self._datfile, 'r', encoding="Latin-1")
|
f = _io.open(self._datfile, 'r', encoding="Latin-1")
|
||||||
except OSError:
|
except OSError:
|
||||||
f = _io.open(self._datfile, 'w', encoding="Latin-1")
|
with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
|
||||||
self._chmod(self._datfile)
|
self._chmod(self._datfile)
|
||||||
f.close()
|
else:
|
||||||
|
f.close()
|
||||||
|
|
||||||
# Read directory file into the in-memory index dict.
|
# Read directory file into the in-memory index dict.
|
||||||
def _update(self):
|
def _update(self):
|
||||||
|
|
@ -91,12 +92,12 @@ class _Database(collections.MutableMapping):
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for line in f:
|
with f:
|
||||||
line = line.rstrip()
|
for line in f:
|
||||||
key, pos_and_siz_pair = eval(line)
|
line = line.rstrip()
|
||||||
key = key.encode('Latin-1')
|
key, pos_and_siz_pair = eval(line)
|
||||||
self._index[key] = pos_and_siz_pair
|
key = key.encode('Latin-1')
|
||||||
f.close()
|
self._index[key] = pos_and_siz_pair
|
||||||
|
|
||||||
# Write the index dict to the directory file. The original directory
|
# Write the index dict to the directory file. The original directory
|
||||||
# file (if any) is renamed with a .bak extension first. If a .bak
|
# file (if any) is renamed with a .bak extension first. If a .bak
|
||||||
|
|
@ -118,13 +119,13 @@ class _Database(collections.MutableMapping):
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
f = self._io.open(self._dirfile, 'w', encoding="Latin-1")
|
with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f:
|
||||||
self._chmod(self._dirfile)
|
self._chmod(self._dirfile)
|
||||||
for key, pos_and_siz_pair in self._index.items():
|
for key, pos_and_siz_pair in self._index.items():
|
||||||
# Use Latin-1 since it has no qualms with any value in any
|
# Use Latin-1 since it has no qualms with any value in any
|
||||||
# position; UTF-8, though, does care sometimes.
|
# position; UTF-8, though, does care sometimes.
|
||||||
f.write("%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair))
|
entry = "%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair)
|
||||||
f.close()
|
f.write(entry)
|
||||||
|
|
||||||
sync = _commit
|
sync = _commit
|
||||||
|
|
||||||
|
|
@ -137,10 +138,9 @@ class _Database(collections.MutableMapping):
|
||||||
key = key.encode('utf-8')
|
key = key.encode('utf-8')
|
||||||
self._verify_open()
|
self._verify_open()
|
||||||
pos, siz = self._index[key] # may raise KeyError
|
pos, siz = self._index[key] # may raise KeyError
|
||||||
f = _io.open(self._datfile, 'rb')
|
with _io.open(self._datfile, 'rb') as f:
|
||||||
f.seek(pos)
|
f.seek(pos)
|
||||||
dat = f.read(siz)
|
dat = f.read(siz)
|
||||||
f.close()
|
|
||||||
return dat
|
return dat
|
||||||
|
|
||||||
# Append val to the data file, starting at a _BLOCKSIZE-aligned
|
# Append val to the data file, starting at a _BLOCKSIZE-aligned
|
||||||
|
|
@ -148,14 +148,13 @@ class _Database(collections.MutableMapping):
|
||||||
# to get to an aligned offset. Return pair
|
# to get to an aligned offset. Return pair
|
||||||
# (starting offset of val, len(val))
|
# (starting offset of val, len(val))
|
||||||
def _addval(self, val):
|
def _addval(self, val):
|
||||||
f = _io.open(self._datfile, 'rb+')
|
with _io.open(self._datfile, 'rb+') as f:
|
||||||
f.seek(0, 2)
|
f.seek(0, 2)
|
||||||
pos = int(f.tell())
|
pos = int(f.tell())
|
||||||
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
|
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
|
||||||
f.write(b'\0'*(npos-pos))
|
f.write(b'\0'*(npos-pos))
|
||||||
pos = npos
|
pos = npos
|
||||||
f.write(val)
|
f.write(val)
|
||||||
f.close()
|
|
||||||
return (pos, len(val))
|
return (pos, len(val))
|
||||||
|
|
||||||
# Write val to the data file, starting at offset pos. The caller
|
# Write val to the data file, starting at offset pos. The caller
|
||||||
|
|
@ -163,10 +162,9 @@ class _Database(collections.MutableMapping):
|
||||||
# pos to hold val, without overwriting some other value. Return
|
# pos to hold val, without overwriting some other value. Return
|
||||||
# pair (pos, len(val)).
|
# pair (pos, len(val)).
|
||||||
def _setval(self, pos, val):
|
def _setval(self, pos, val):
|
||||||
f = _io.open(self._datfile, 'rb+')
|
with _io.open(self._datfile, 'rb+') as f:
|
||||||
f.seek(pos)
|
f.seek(pos)
|
||||||
f.write(val)
|
f.write(val)
|
||||||
f.close()
|
|
||||||
return (pos, len(val))
|
return (pos, len(val))
|
||||||
|
|
||||||
# key is a new key whose associated value starts in the data file
|
# key is a new key whose associated value starts in the data file
|
||||||
|
|
@ -174,10 +172,9 @@ class _Database(collections.MutableMapping):
|
||||||
# the in-memory index dict, and append one to the directory file.
|
# the in-memory index dict, and append one to the directory file.
|
||||||
def _addkey(self, key, pos_and_siz_pair):
|
def _addkey(self, key, pos_and_siz_pair):
|
||||||
self._index[key] = pos_and_siz_pair
|
self._index[key] = pos_and_siz_pair
|
||||||
f = _io.open(self._dirfile, 'a', encoding="Latin-1")
|
with _io.open(self._dirfile, 'a', encoding="Latin-1") as f:
|
||||||
self._chmod(self._dirfile)
|
self._chmod(self._dirfile)
|
||||||
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
|
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
|
||||||
f.close()
|
|
||||||
|
|
||||||
def __setitem__(self, key, val):
|
def __setitem__(self, key, val):
|
||||||
if isinstance(key, str):
|
if isinstance(key, str):
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,9 @@ Core and Builtins
|
||||||
Library
|
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 #21491: socketserver: Fix a race condition in child processes reaping.
|
||||||
|
|
||||||
- Issue #21719: Added the ``st_file_attributes`` field to os.stat_result on
|
- Issue #21719: Added the ``st_file_attributes`` field to os.stat_result on
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue