shelve objects set self.dict = 0 in their close() method. this results in errors such as TypeError: unsubscriptable object and AttributeError: 'int' object has no attribute 'has_key'. This is fairly baffling for the user. "self.dict = 0" in close() is present in current svn trunk too.
How about the following patch. With it, you get an IOError. >>> s = shelve.open('/tmp/t', 'c') >>> s.has_key('foo') 0 >>> s.close() >>> s.has_key('foo') Traceback (most recent call last): File "", line 1, in ? File "shelve.py", line 107, in has_key return self.dict.has_key(key) File "shelve.py", line 94, in getdict raise IOError, 'shelf has been closed' IOError: shelf has been closed
I'm working on this one. Alternate patch attached. The problem with the old one is that it slows down every access to the shelf and it prevents assignment to self.dict which has always been allowed. The new patch improves error reporting without changing performance or semantics for shelves prior to closing.