Disappearing objects in shelve

Ray O RayOsborn at mac.com
Tue Aug 3 19:48:44 EDT 2004


I have read a number of threads relating to problems with shelve, but
I couldn't find one directly related my experience, so I would
appreciate some advice.  The closest one ended with a recommendation
to upgrade to Python 2.3, but that, unfortunately, is out of my
control.  The system I use runs Python 2.2.2, and whichdb gives
dbhash.

The problem only occurs when serializing class instances; lists don't
seem to be affected.  Every time I attempt to read the instance from
the shelve file, it seems to delete it from the file even though I am
not performing any write operations.   Attempts to use flag='r' in the
shelve.open generated error messages, as did restricting write
permissions to the database.

This is some example code.  'Editor.py' defines the class whose
instance is stored in db["john"], etc.  The file, "editors.db",
originally contained keys "nick and "john".  By the end, the file has
been emptied.

import shelve
import Editor

def main():
    db = shelve.open("editors.db")
    value = db["john"]
    db.close()

    db=shelve.open("editors.db")
    print db.keys()
    db.close()
    
    db = shelve.open("editors.db")
    value = db["nick"]
    db.close()

    db=shelve.open("editors.db")
    print db.keys()
    db.close()
    
main()

>> import test
[ 'john','nick']
['nick']

I got the same result when I attempted a deep copy instead of the
assignment to value.  Anyone know what I'm missing?



More information about the Python-list mailing list