annotate Lib/shelve.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 7b54a3cedbe0
children 314d0d603f06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
1 """Manage shelves of pickled objects.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
2
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
3 A "shelf" is a persistent, dictionary-like object. The difference
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
4 with dbm databases is that the values (not the keys!) in a shelf can
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
5 be essentially arbitrary Python objects -- anything that the "pickle"
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
6 module can handle. This includes most class instances, recursive data
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
7 types, and objects containing lots of shared sub-objects. The keys
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
8 are ordinary strings.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
9
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
10 To summarize the interface (key is a string, data is an arbitrary
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
11 object):
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
12
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
13 import shelve
2356
6a94119844eb minute comment changes
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
14 d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
15
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
16 d[key] = data # store data at key (overwrites old data if
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
17 # using an existing key)
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
18 data = d[key] # retrieve data at key (raise KeyError if no
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
19 # such key)
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
20 del d[key] # delete data stored at key (raises KeyError
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
21 # if no such key)
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
22 flag = d.has_key(key) # true if the key exists
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
23 list = d.keys() # a list of all existing keys (slow!)
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
24
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
25 d.close() # close it
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
26
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
27 Dependent on the implementation, closing a persistent dictionary may
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
28 or may not be necessary to flush changes to disk.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
29 """
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 import pickle
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 import StringIO
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
34
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 class Shelf:
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
36 """Base class for shelf implementations.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
37
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
38 This is initialized with a dictionary-like object.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
39 See the module's __doc__ string for an overview of the interface.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
40 """
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def __init__(self, dict):
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self.dict = dict
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def keys(self):
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return self.dict.keys()
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def __len__(self):
4263
7b54a3cedbe0 Fix bogus len() call
Guido van Rossum <guido@python.org>
parents: 2967
diff changeset
49 return len(self.dict)
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def has_key(self, key):
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 return self.dict.has_key(key)
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def __getitem__(self, key):
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
55 f = StringIO.StringIO(self.dict[key])
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
56 return pickle.Unpickler(f).load()
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def __setitem__(self, key, value):
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 f = StringIO.StringIO()
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 p = pickle.Pickler(f)
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 p.dump(value)
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.dict[key] = f.getvalue()
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 def __delitem__(self, key):
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 del self.dict[key]
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 def close(self):
2439
c1d27043c22f fix stupid bug (db should be dict)
Guido van Rossum <guido@python.org>
parents: 2356
diff changeset
68 if hasattr(self.dict, 'close'):
c1d27043c22f fix stupid bug (db should be dict)
Guido van Rossum <guido@python.org>
parents: 2356
diff changeset
69 self.dict.close()
c1d27043c22f fix stupid bug (db should be dict)
Guido van Rossum <guido@python.org>
parents: 2356
diff changeset
70 self.dict = None
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
72 def __del__(self):
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
73 self.close()
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
74
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
75
2967
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
76 class BsdDbShelf(Shelf):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
77 """Shelf implementation using the "BSD" db interface.
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
78
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
79 The actual database is opened using one of thethe "bsddb" modules
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
80 "open" routines (i.e. bsddb.hashopen, bsddb.btopen or bsddb.rnopen.)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
81
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
82 This class is initialized with the the database object
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
83 returned from one of the bsddb open functions.
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
84
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
85 See the module's __doc__ string for an overview of the interface.
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
86 """
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
87
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
88 def __init__(self, dict):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
89 Shelf.__init__(self, dict)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
90
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
91 def set_location(self, key):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
92 (key, value) = self.dict.set_location(key)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
93 f = StringIO.StringIO(value)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
94 return (key, pickle.Unpickler(f).load())
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
95
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
96 def next(self):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
97 (key, value) = self.dict.next()
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
98 f = StringIO.StringIO(value)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
99 return (key, pickle.Unpickler(f).load())
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
100
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
101 def previous(self):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
102 (key, value) = self.dict.previous()
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
103 f = StringIO.StringIO(value)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
104 return (key, pickle.Unpickler(f).load())
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
105
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
106 def first(self):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
107 (key, value) = self.dict.first()
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
108 f = StringIO.StringIO(value)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
109 return (key, pickle.Unpickler(f).load())
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
110
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
111 def last(self):
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
112 (key, value) = self.dict.last()
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
113 f = StringIO.StringIO(value)
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
114 return (key, pickle.Unpickler(f).load())
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
115
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
116
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
117 class DbfilenameShelf(Shelf):
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
118 """Shelf implementation using the "anydbm" generic dbm interface.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
119
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
120 This is initialized with the filename for the dbm database.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
121 See the module's __doc__ string for an overview of the interface.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
122 """
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
2967
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
124 def __init__(self, filename, flag='c'):
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 import anydbm
2967
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
126 Shelf.__init__(self, anydbm.open(filename, flag))
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
127
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
128
2967
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
129 def open(filename, flag='c'):
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
130 """Open a persistent dictionary for reading and writing.
2100
4574ed657cc9 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
132 Argument is the filename for the dbm database.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
133 See the module's __doc__ string for an overview of the interface.
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
134 """
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
135
2967
74953a41607c renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents: 2439
diff changeset
136 return DbfilenameShelf(filename, flag)