diff options
author | Guido van Rossum <guido@python.org> | 2007-05-23 20:51:02 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-05-23 20:51:02 (GMT) |
commit | 6252e10ed906eb419a75b310f7c0d6696a4eeb46 (patch) | |
tree | b6b46cf0e5d1528d736cde47d1089aefc46bbf05 /Lib/dumbdbm.py | |
parent | 517bcfeb6be448f47804900ac75c804d8f70a20e (diff) | |
download | cpython-6252e10ed906eb419a75b310f7c0d6696a4eeb46.zip cpython-6252e10ed906eb419a75b310f7c0d6696a4eeb46.tar.gz cpython-6252e10ed906eb419a75b310f7c0d6696a4eeb46.tar.bz2 |
Make gdbm and dumbdbm use byte strings. Updated their tests.
Diffstat (limited to 'Lib/dumbdbm.py')
-rw-r--r-- | Lib/dumbdbm.py | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/Lib/dumbdbm.py b/Lib/dumbdbm.py index 7724ac6..eb1c613 100644 --- a/Lib/dumbdbm.py +++ b/Lib/dumbdbm.py @@ -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: |