mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
Make gdbm and dumbdbm use byte strings. Updated their tests.
This commit is contained in:
parent
517bcfeb6b
commit
6252e10ed9
7 changed files with 68 additions and 61 deletions
|
@ -21,12 +21,11 @@ is read when the database is opened, and some updates rewrite the whole index)
|
|||
|
||||
"""
|
||||
|
||||
import io as _io
|
||||
import os as _os
|
||||
import __builtin__
|
||||
import UserDict
|
||||
|
||||
_open = __builtin__.open
|
||||
|
||||
_BLOCKSIZE = 512
|
||||
|
||||
error = IOError # For anydbm
|
||||
|
@ -42,7 +41,7 @@ class _Database(UserDict.DictMixin):
|
|||
# _commit() finish successfully, we can't ignore shutdown races
|
||||
# here, and _commit() must not reference any globals.
|
||||
_os = _os # for _commit()
|
||||
_open = _open # for _commit()
|
||||
_io = _io # for _commit()
|
||||
|
||||
def __init__(self, filebasename, mode):
|
||||
self._mode = mode
|
||||
|
@ -66,9 +65,9 @@ class _Database(UserDict.DictMixin):
|
|||
|
||||
# Mod by Jack: create data file if needed
|
||||
try:
|
||||
f = _open(self._datfile, 'r')
|
||||
f = _io.open(self._datfile, 'r')
|
||||
except IOError:
|
||||
f = _open(self._datfile, 'w')
|
||||
f = _io.open(self._datfile, 'w')
|
||||
self._chmod(self._datfile)
|
||||
f.close()
|
||||
self._update()
|
||||
|
@ -77,7 +76,7 @@ class _Database(UserDict.DictMixin):
|
|||
def _update(self):
|
||||
self._index = {}
|
||||
try:
|
||||
f = _open(self._dirfile)
|
||||
f = _io.open(self._dirfile, 'r')
|
||||
except IOError:
|
||||
pass
|
||||
else:
|
||||
|
@ -107,7 +106,7 @@ class _Database(UserDict.DictMixin):
|
|||
except self._os.error:
|
||||
pass
|
||||
|
||||
f = self._open(self._dirfile, 'w')
|
||||
f = self._io.open(self._dirfile, 'w')
|
||||
self._chmod(self._dirfile)
|
||||
for key, pos_and_siz_pair in self._index.items():
|
||||
f.write("%r, %r\n" % (key, pos_and_siz_pair))
|
||||
|
@ -117,7 +116,7 @@ class _Database(UserDict.DictMixin):
|
|||
|
||||
def __getitem__(self, key):
|
||||
pos, siz = self._index[key] # may raise KeyError
|
||||
f = _open(self._datfile, 'rb')
|
||||
f = _io.open(self._datfile, 'rb')
|
||||
f.seek(pos)
|
||||
dat = f.read(siz)
|
||||
f.close()
|
||||
|
@ -128,11 +127,11 @@ class _Database(UserDict.DictMixin):
|
|||
# to get to an aligned offset. Return pair
|
||||
# (starting offset of val, len(val))
|
||||
def _addval(self, val):
|
||||
f = _open(self._datfile, 'rb+')
|
||||
f = _io.open(self._datfile, 'rb+')
|
||||
f.seek(0, 2)
|
||||
pos = int(f.tell())
|
||||
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
|
||||
f.write('\0'*(npos-pos))
|
||||
f.write(b'\0'*(npos-pos))
|
||||
pos = npos
|
||||
f.write(val)
|
||||
f.close()
|
||||
|
@ -143,7 +142,7 @@ class _Database(UserDict.DictMixin):
|
|||
# pos to hold val, without overwriting some other value. Return
|
||||
# pair (pos, len(val)).
|
||||
def _setval(self, pos, val):
|
||||
f = _open(self._datfile, 'rb+')
|
||||
f = _io.open(self._datfile, 'rb+')
|
||||
f.seek(pos)
|
||||
f.write(val)
|
||||
f.close()
|
||||
|
@ -154,14 +153,16 @@ class _Database(UserDict.DictMixin):
|
|||
# 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 = _open(self._dirfile, 'a')
|
||||
f = _io.open(self._dirfile, 'a')
|
||||
self._chmod(self._dirfile)
|
||||
f.write("%r, %r\n" % (key, pos_and_siz_pair))
|
||||
f.close()
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
if not type(key) == type('') == type(val):
|
||||
raise TypeError, "keys and values must be strings"
|
||||
if not isinstance(key, basestring):
|
||||
raise TypeError("keys must be strings")
|
||||
if not isinstance(val, (str8, bytes)):
|
||||
raise TypeError("values must be byte strings")
|
||||
if key not in self._index:
|
||||
self._addkey(key, self._addval(val))
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue