dumbdbm bug fixed

Amos Newcombe anewcombe at earthlink.net
Sat Dec 9 16:11:53 EST 2000

In dumbdbm.py (v 2.0) it says

>- seems to contain a bug when updating...

The bug I found was that the length of an item in the index (.dir) 
file doesn't get updated when the record in the .dat file does.

The fix is simple. Just call self._commit() at the end of 
_Database.__setitem__(), so it looks like this.

	def __setitem__(self, key, val):
		if not type(key) == type('') == type(val):
			raise TypeError, "keys and values must be strings"
		if not self._index.has_key(key):
			(pos, siz) = self._addval(val)
			self._addkey(key, (pos, siz))
			pos, siz = self._index[key]
			oldblocks = (siz + _BLOCKSIZE - 1) / _BLOCKSIZE
			newblocks = (len(val) + _BLOCKSIZE - 1) / _BLOCKSIZE
			if newblocks <= oldblocks:
				pos, siz = self._setval(pos, val)
				self._index[key] = pos, siz
				pos, siz = self._addval(val)
				self._index[key] = pos, siz

Note that in the other branch of the main if...else statement, 
self._addkey() takes care of updating the index.

Amos Newcombe
The difference between theory and practice is that, in theory, there 
is no difference between theory and practice. 

More information about the Python-list mailing list