[Python-bugs-list] [ python-Bugs-482460 ] dumbdbm fails to overwrite existing key

noreply@sourceforge.net noreply@sourceforge.net
Fri, 16 Nov 2001 08:16:32 -0800


Bugs item #482460, was opened at 2001-11-16 05:22
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=482460&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Michael McCandless (mikemccand)
Assigned to: Nobody/Anonymous (nobody)
Summary: dumbdbm fails to overwrite existing key

Initial Comment:

Here's a simple test case that shows the bug:

  import dumbdbm

  db = dumbdbm.open('db', 'c')
  db['1'] = 'hello'
  db['1'] = 'hello2'
  db.close()

  db = dumbdbm.open('db', 'c')
  print db['1']
  db.close()

This prints out 'hello' but should print out 'hello2'.  As far as I can tell, this bug did not exist 1.5.2, 
but then appeared in 2.0 and is still present  in 2.2b1.

The problem is, when overwriting the key, dumbdbm fails to update the .dir file.  I believe this 
patch fixes the bug:

> diff -c Lib/dumbdbm.py Lib/dumbdbm.py.new 
*** Lib/dumbdbm.py      Tue Sep  4 15:14:13 2001
--- Lib/dumbdbm.py.new  Fri Nov 16 08:16:42 2001
***************
*** 124,129 ****
--- 124,132 ----
              else:
                  pos, siz = self._addval(val)
                  self._index[key] = pos, siz
+             tup = (pos, siz)
+             if self._index[key] != tup:
+                 self._addkey(key, tup)

      def __delitem__(self, key):
          del self._index[key]




----------------------------------------------------------------------

>Comment By: Skip Montanaro (montanaro)
Date: 2001-11-16 08:16

Message:
Logged In: YES 
user_id=44345

Performance isn't a big issue with dumbdbm, so I think the
code should be as simple as possible.  I have a local
version I was working on a bit.  I simply delete then 
add in __setitem__:

    def __setitem__(self, key, val):
       if not type(key) == type('') == type(val):
            raise TypeError, "keys and values must be strings"
        if self._index.has_key(key):
            del self[key]
            
        (pos, siz) = self._addval(val)
        self._addkey(key, (pos, siz))

I also have code in my local version to reuse space
though.  I also want to add file locking (which is
the motivation for me to be fiddling with this
stuff).



----------------------------------------------------------------------

Comment By: Michael McCandless (mikemccand)
Date: 2001-11-16 05:38

Message:
Logged In: YES 
user_id=323786


Sorry -- that previous patch does not work.  I believe this one does:

> diff -c Lib/dumbdbm.py Lib/dumbdbm.py.new 
*** Lib/dumbdbm.py      Fri Nov 16 08:36:36 2001
--- Lib/dumbdbm.py.new  Fri Nov 16 08:36:50 2001
***************
*** 116,121 ****
--- 116,122 ----
              self._addkey(key, (pos, siz))
          else:
              pos, siz = self._index[key]
+             startTup = (pos, siz)
              oldblocks = (siz + _BLOCKSIZE - 1) / _BLOCKSIZE
              newblocks = (len(val) + _BLOCKSIZE - 1) / _BLOCKSIZE
              if newblocks <= oldblocks:
***************
*** 124,129 ****
--- 125,133 ----
              else:
                  pos, siz = self._addval(val)
                  self._index[key] = pos, siz
+             tup = (pos, siz)
+             if startTup != tup:
+                 self._addkey(key, tup)

      def __delitem__(self, key):
          del self._index[key]

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=482460&group_id=5470