Mercurial > cpython-lru_cache-weakref
annotate Lib/pickle.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 | 4e00e5daa612 |
| children | b7ac7b46b10c |
| rev | line source |
|---|---|
|
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
|
1 """\ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 Pickling Algorithm |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 ------------------ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 This module implements a basic but powerful algorithm for "pickling" (a.k.a. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 serializing, marshalling or flattening) nearly arbitrary Python objects. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 This is a more primitive notion than persistency -- although 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
|
8 reads and writes file objects, it does not handle the issue of naming |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 persistent objects, nor the (even more complicated) area of concurrent |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 access to persistent objects. The pickle module can transform a complex |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 object into a byte stream and it can transform the byte stream into |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 an object with the same internal structure. The most obvious thing to |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 do with these byte streams is to write them onto a file, but it is also |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 conceivable to send them across a network or store them in a database. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 Unlike the built-in marshal module, pickle handles the following correctly: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 - recursive objects |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 - pointer sharing |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
20 - classes and class instances |
|
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
|
21 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Pickle is Python-specific. This has the advantage that there are no |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 restrictions imposed by external standards such as CORBA (which probably |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 can't represent pointer sharing or recursive objects); however it means |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 that non-Python programs may not be able to reconstruct pickled Python |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 objects. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 Pickle uses a printable ASCII representation. This is slightly more |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 voluminous than a binary representation. However, small integers actually |
|
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 take *less* space when represented as minimal-size decimal strings than |
|
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 when represented as 32-bit binary numbers, and strings are only much longer |
|
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 if they contain control characters or 8-bit characters. The big advantage |
|
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 of using printable ASCII (and of some other characteristics of pickle's |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 representation) is that for debugging or recovery purposes it is possible |
|
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 for a human to read the pickled file with a standard text editor. (I could |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 have gone a step further and used a notation like S-expressions, but the |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 parser would have been considerably more complicated and slower, and the |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 files would probably have become much larger.) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 Pickle doesn't handle code objects, which marshal does. |
|
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 I suppose pickle could, and maybe it should, but there's probably no |
|
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 great need for it right now (as long as marshal continues to be used |
|
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 for reading and writing code objects), and at least this avoids |
|
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 the possibility of smuggling Trojan horses into a program. |
|
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 |
|
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 For the benefit of persistency modules written using pickle, it supports |
|
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 the notion of a reference to an object outside the pickled data stream. |
|
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 Such objects are referenced by a name, which is an arbitrary string of |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 printable ASCII characters. The resolution of such names is not defined |
|
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 by the pickle module -- the persistent object module will have to implement |
|
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 a method "persistent_load". To write references to persistent objects, |
|
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 the persistent module must define a method "persistent_id" which returns |
|
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 either None or the persistent ID of the object. |
|
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 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 There are some restrictions on the pickling of class instances. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
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 First of all, the class must be defined at the top level in a module. |
|
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 |
|
3955
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
59 Next, it must normally be possible to create class instances by |
|
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
60 calling the class without arguments. Usually, this is best |
|
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
61 accomplished by providing default values for all arguments to its |
|
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
62 __init__ method (if it has one). If this is undesirable, the |
|
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
63 class can define a method __getinitargs__, which should return a |
|
29015b58e922
Slight update of doc string -- suggest default args for __init__, no
Guido van Rossum <guido@python.org>
parents:
3791
diff
changeset
|
64 *tuple* containing the arguments to be passed to the class |
|
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
|
65 constructor. |
|
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 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
67 Classes can influence how their instances are pickled -- if the class defines |
|
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
|
68 the method __getstate__, it is called and the return state is pickled |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 as the contents for the instance, and if the class defines the |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 method __setstate__, it is called with the unpickled state. (Note |
|
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 that these methods can also be used to implement copying class instances.) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 If there is no __getstate__ method, the instance's __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
|
73 is pickled. If there is no __setstate__ method, the pickled object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 must be a dictionary and its items are assigned to the new instance's |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 dictionary. (If a class defines both __getstate__ and __setstate__, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 the state object needn't be a dictionary -- these methods can do what they |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 want.) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 Note that when class instances are pickled, their class's code and data |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 is not pickled along with them. Only the instance data is pickled. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 This is done on purpose, so you can fix bugs in a class or add methods and |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 still load objects that were created with an earlier version of the |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 class. If you plan to have long-lived objects that will see many versions |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 of a class, it may be worth to put a version number in the objects so |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 that suitable conversions can be made by the class's __setstate__ method. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 The interface is as follows: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
2356
6a94119844eb
minute comment changes
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
89 To pickle an object x onto a file f, open for 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
|
90 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 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
|
92 p.dump(x) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 To unpickle an object x from a file f, open for reading: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 u = pickle.Unpickler(f) |
|
2648
b7e8bc002a24
correct typo in example
Guido van Rossum <guido@python.org>
parents:
2534
diff
changeset
|
97 x = u.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
|
98 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 The Pickler class only calls the method f.write with a string argument |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 (XXX possibly the interface should pass f.write instead of 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
|
101 The Unpickler calls the methods f.read(with an integer argument) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 and f.readline(without argument), both returning a string. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 It is explicitly allowed to pass non-file objects here, as long as they |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 have the right methods. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 The following types can be pickled: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 - None |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 - integers, long integers, floating point numbers |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 - strings |
|
2356
6a94119844eb
minute comment changes
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
111 - tuples, lists and dictionaries containing only picklable objects |
|
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
|
112 - class instances whose __dict__ or __setstate__() is picklable |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
113 - classes |
|
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
|
114 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 Attempts to pickle unpicklable objects will raise an exception |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 after having written an unspecified number of bytes to the file argument. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 It is possible to make multiple calls to Pickler.dump() or to |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 Unpickler.load(), as long as there is a one-to-one correspondence |
|
2356
6a94119844eb
minute comment changes
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
120 between pickler and Unpickler objects and between dump and load calls |
|
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
|
121 for any pair of corresponding Pickler and Unpicklers. WARNING: this |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 is intended for pickleing multiple objects without intervening modifications |
|
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 to the objects or their parts. If you modify an object and then 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
|
124 it again using the same Pickler instance, the object is not pickled |
|
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 again -- a reference to it is pickled and the Unpickler will return |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 the old value, not the modified one. (XXX There are two problems here: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 (a) detecting changes, and (b) marshalling a minimal set of changes. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 I have no answers. Garbage Collection may also become a problem here.) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 """ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
131 __version__ = "1.6" # Code version |
|
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
|
132 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 from types import * |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 import string |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
136 format_version = "1.1" # File format version we write |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
137 compatible_formats = ["1.0"] # Old format versions we can read |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
138 |
|
2507
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
139 PicklingError = "pickle.PicklingError" |
|
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
140 |
|
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
|
141 AtomicTypes = [NoneType, IntType, FloatType, StringType] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 def safe(object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 t = type(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 if t in AtomicTypes: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 return 1 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 if t is TupleType: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 for item in object: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 if not safe(item): return 0 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 return 1 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 return 0 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 MARK = '(' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 POP = '0' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 DUP = '2' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 STOP = '.' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 TUPLE = 't' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 LIST = 'l' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 DICT = 'd' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 INST = 'i' |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
161 CLASS = '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
|
162 GET = 'g' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 PUT = 'p' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 APPEND = 'a' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 SETITEM = 's' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 BUILD = 'b' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 NONE = 'N' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 INT = 'I' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 LONG = 'L' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 FLOAT = '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
|
171 STRING = 'S' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 PERSID = 'P' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 AtomicKeys = [NONE, INT, LONG, FLOAT, STRING] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 AtomicMap = { |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 NoneType: NONE, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 IntType: INT, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 LongType: LONG, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 FloatType: FLOAT, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 StringType: STRING, |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 } |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 class Pickler: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 def __init__(self, file): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 self.write = file.write |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 self.memo = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 def dump(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 self.save(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 self.write(STOP) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 def save(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 pid = self.persistent_id(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 if pid: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 self.write(PERSID + str(pid) + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 return |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 d = id(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 if self.memo.has_key(d): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 self.write(GET + `d` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 t = type(object) |
|
2507
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
202 try: |
|
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
203 f = self.dispatch[t] |
|
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
204 except KeyError: |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
205 if hasattr(object, '__class__'): |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
206 f = self.dispatch[InstanceType] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
207 else: |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
208 raise PicklingError, \ |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
209 "can't pickle %s objects" % `t.__name__` |
|
2507
8ac4c0e1ce17
added PicklingError exception
Guido van Rossum <guido@python.org>
parents:
2475
diff
changeset
|
210 f(self, object) |
|
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
|
211 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 def persistent_id(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 return None |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 dispatch = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 def save_none(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 self.write(NONE) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 dispatch[NoneType] = save_none |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 def save_int(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 self.write(INT + `object` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 dispatch[IntType] = save_int |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 def save_long(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 self.write(LONG + `object` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 dispatch[LongType] = save_long |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 def save_float(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 self.write(FLOAT + `object` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 dispatch[FloatType] = save_float |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 def save_string(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 d = id(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 self.write(STRING + `object` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 self.write(PUT + `d` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 self.memo[d] = object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 dispatch[StringType] = save_string |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 def save_tuple(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 d = id(object) |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
242 write = self.write |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
243 save = self.save |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
244 has_key = self.memo.has_key |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
245 write(MARK) |
|
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
|
246 n = len(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 for k in range(n): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
248 save(object[k]) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
249 if has_key(d): |
|
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
|
250 # Saving object[k] has saved us! |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 while k >= 0: |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
252 write(POP) |
|
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
|
253 k = k-1 |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
254 write(GET + `d` + '\n') |
|
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
|
255 break |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 else: |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
257 write(TUPLE + PUT + `d` + '\n') |
|
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
|
258 self.memo[d] = object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 dispatch[TupleType] = save_tuple |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 def save_list(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 d = id(object) |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
263 write = self.write |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
264 save = self.save |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
265 write(MARK) |
|
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
|
266 n = len(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 for k in range(n): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 item = object[k] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 if not safe(item): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 break |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
271 save(item) |
|
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
|
272 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 k = n |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
274 write(LIST + PUT + `d` + '\n') |
|
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
|
275 self.memo[d] = object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 for k in range(k, n): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 item = object[k] |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
278 save(item) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
279 write(APPEND) |
|
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
|
280 dispatch[ListType] = save_list |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 def save_dict(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 d = id(object) |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
284 write = self.write |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
285 save = self.save |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
286 write(MARK) |
|
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
|
287 items = object.items() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 n = len(items) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 for k in range(n): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 key, value = items[k] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 if not safe(key) or not safe(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
|
292 break |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
293 save(key) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
294 save(value) |
|
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
|
295 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 k = n |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 self.write(DICT + PUT + `d` + '\n') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 self.memo[d] = object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 for k in range(k, n): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 key, value = items[k] |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
301 save(key) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
302 save(value) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
303 write(SETITEM) |
|
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
|
304 dispatch[DictionaryType] = save_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
|
305 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 def save_inst(self, object): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 d = id(object) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 cls = object.__class__ |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
309 write = self.write |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
310 save = self.save |
|
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
|
311 module = whichmodule(cls) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 name = cls.__name__ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 if hasattr(object, '__getinitargs__'): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 args = object.__getinitargs__() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 len(args) # XXX Assert it's a sequence |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 args = () |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
318 write(MARK) |
|
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
|
319 for arg in args: |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
320 save(arg) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
321 write(INST + module + '\n' + name + '\n' + |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
322 PUT + `d` + '\n') |
|
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
|
323 self.memo[d] = object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 try: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 getstate = object.__getstate__ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 except AttributeError: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 stuff = object.__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
|
328 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 stuff = getstate() |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
330 save(stuff) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
331 write(BUILD) |
|
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
|
332 dispatch[InstanceType] = save_inst |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
334 def save_class(self, object): |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
335 d = id(object) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
336 module = whichmodule(object) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
337 name = object.__name__ |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
338 self.write(CLASS + module + '\n' + name + '\n' + |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
339 PUT + `d` + '\n') |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
340 dispatch[ClassType] = save_class |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
341 |
|
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
|
342 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 classmap = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 def whichmodule(cls): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 """Figure out the module in which a class occurs. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 Search sys.modules for the module. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 Cache in classmap. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 Return a module name. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 If the class cannot be found, return __main__. |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 """ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 if classmap.has_key(cls): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 return classmap[cls] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 import sys |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 clsname = cls.__name__ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 for name, module in sys.modules.items(): |
|
2774
b735d3386651
test other name variable
Guido van Rossum <guido@python.org>
parents:
2648
diff
changeset
|
358 if name != '__main__' and \ |
|
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
|
359 hasattr(module, clsname) and \ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 getattr(module, clsname) is cls: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 break |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 name = '__main__' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 classmap[cls] = name |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 return name |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 class Unpickler: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 def __init__(self, file): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 self.readline = file.readline |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 self.read = file.read |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 self.memo = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 def load(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
|
376 self.mark = ['spam'] # Any new unique object |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 self.stack = [] |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
378 self.append = self.stack.append |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
379 read = self.read |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
380 dispatch = self.dispatch |
|
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
|
381 try: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 while 1: |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
383 key = read(1) |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
384 dispatch[key](self) |
|
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
|
385 except STOP, 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
|
386 return 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
|
387 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 def marker(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
389 stack = self.stack |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
390 mark = self.mark |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
391 k = len(stack)-1 |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
392 while stack[k] is not mark: k = k-1 |
|
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
|
393 return k |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 dispatch = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 |
|
2475
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
397 def load_eof(self): |
|
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
398 raise EOFError |
|
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
399 dispatch[''] = load_eof |
|
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
400 |
|
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
|
401 def load_persid(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
|
402 pid = self.readline()[:-1] |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
403 self.append(self.persistent_load(pid)) |
|
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
|
404 dispatch[PERSID] = load_persid |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 def load_none(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
407 self.append(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
|
408 dispatch[NONE] = load_none |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
410 def load_int(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
411 self.append(string.atoi(self.readline()[:-1], 0)) |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
412 dispatch[INT] = load_int |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
413 |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
414 def load_long(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
415 self.append(string.atol(self.readline()[:-1], 0)) |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
416 dispatch[LONG] = load_long |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
417 |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
418 def load_float(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
419 self.append(string.atof(self.readline()[:-1])) |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
420 dispatch[FLOAT] = load_float |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
421 |
|
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
422 def load_string(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
423 self.append(eval(self.readline()[:-1], |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
424 {'__builtins__': {}})) # Let's be careful |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
425 dispatch[STRING] = load_string |
|
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
|
426 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 def load_tuple(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
|
428 k = self.marker() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 self.stack[k:] = [tuple(self.stack[k+1:])] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 dispatch[TUPLE] = load_tuple |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 def load_list(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
|
433 k = self.marker() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 self.stack[k:] = [self.stack[k+1:]] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 dispatch[LIST] = load_list |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 def load_dict(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
|
438 k = self.marker() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 d = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 items = self.stack[k+1:] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 for i in range(0, len(items), 2): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 key = items[i] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 value = items[i+1] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 d[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
|
445 self.stack[k:] = [d] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 dispatch[DICT] = load_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
|
447 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 def load_inst(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
|
449 k = self.marker() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 args = tuple(self.stack[k+1:]) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 del self.stack[k:] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 module = self.readline()[:-1] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 name = self.readline()[:-1] |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
454 klass = self.find_class(module, name) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
455 value = apply(klass, args) |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
456 self.append(value) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
457 dispatch[INST] = load_inst |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
458 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
459 def load_class(self): |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
460 module = self.readline()[:-1] |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
461 name = self.readline()[:-1] |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
462 klass = self.find_class(module, name) |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
463 self.append(klass) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
464 return klass |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
465 dispatch[CLASS] = load_class |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
466 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
467 def find_class(self, module, name): |
|
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
|
468 env = {} |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 try: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 exec 'from %s import %s' % (module, name) in env |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 except ImportError: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 raise SystemError, \ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 "Failed to import class %s from module %s" % \ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 (name, module) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
475 klass = env[name] |
|
4359
4e00e5daa612
Compromise on test in find_class(): a user-defined function is okay,
Guido van Rossum <guido@python.org>
parents:
3980
diff
changeset
|
476 if type(klass) is BuiltinFunctionType: |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
477 raise SystemError, \ |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
478 "Imported object %s from module %s is not a class" % \ |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
479 (name, module) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
480 return klass |
|
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
|
481 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 def load_pop(self): |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
483 del self.stack[-1] |
|
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
|
484 dispatch[POP] = load_pop |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 def load_dup(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
487 self.append(stack[-1]) |
|
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
|
488 dispatch[DUP] = load_dup |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 def load_get(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
491 self.append(self.memo[self.readline()[:-1]]) |
|
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
|
492 dispatch[GET] = load_get |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 def load_put(self): |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
495 self.memo[self.readline()[:-1]] = self.stack[-1] |
|
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
|
496 dispatch[PUT] = load_put |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 def load_append(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
499 stack = self.stack |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
500 value = stack[-1] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
501 del stack[-1] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
502 list = stack[-1] |
|
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
|
503 list.append(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
|
504 dispatch[APPEND] = load_append |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 def load_setitem(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
507 stack = self.stack |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
508 value = stack[-1] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
509 key = stack[-2] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
510 del stack[-2:] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
511 dict = stack[-1] |
|
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
|
512 dict[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
|
513 dispatch[SETITEM] = load_setitem |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 def load_build(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
516 stack = self.stack |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
517 value = stack[-1] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
518 del stack[-1] |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
519 inst = stack[-1] |
|
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
|
520 try: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 setstate = inst.__setstate__ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 except AttributeError: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 for key in value.keys(): |
|
3980
4b5098b1fcef
This is the third time I check in this change :-(
Guido van Rossum <guido@python.org>
parents:
3955
diff
changeset
|
524 setattr(inst, key, value[key]) |
|
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
|
525 else: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 setstate(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
|
527 dispatch[BUILD] = load_build |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 def load_mark(self): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
530 self.append(self.mark) |
|
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
|
531 dispatch[MARK] = load_mark |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 def load_stop(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
|
534 value = self.stack[-1] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 del self.stack[-1] |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 raise STOP, 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
|
537 dispatch[STOP] = load_stop |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
540 # Shorthands |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
541 |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
542 from StringIO import StringIO |
|
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
543 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
544 def dump(object, file): |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
545 Pickler(file).dump(object) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
546 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
547 def dumps(object): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
548 file = StringIO() |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
549 Pickler(file).dump(object) |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
550 return file.getvalue() |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
551 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
552 def load(file): |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
553 return Unpickler(file).load() |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
554 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
555 def loads(str): |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
556 file = StringIO(str) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
557 return Unpickler(file).load() |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
558 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
559 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
560 # The rest is used for testing only |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
561 |
|
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
|
562 class C: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
563 def __cmp__(self, other): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 return cmp(self.__dict__, other.__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
|
565 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
566 def test(): |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 fn = 'pickle_tmp' |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
568 c = C() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
569 c.foo = 1 |
|
3550
a95b350a44da
Set the base for atoi() and atol() to 0, since we're reading Python
Guido van Rossum <guido@python.org>
parents:
3513
diff
changeset
|
570 c.bar = 2L |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
571 x = [0, 1, 2, 3] |
|
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
|
572 y = ('abc', 'abc', c, c) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 x.append(y) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 x.append(y) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
575 x.append(5) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
576 f = open(fn, 'w') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
577 F = 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
|
578 F.dump(x) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 f.close() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
580 f = open(fn, 'r') |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 U = Unpickler(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
|
582 x2 = U.load() |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 print x |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
584 print x2 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 print x == x2 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 print map(id, x) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
587 print map(id, x2) |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
588 print F.memo |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
589 print U.memo |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
590 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
591 if __name__ == '__main__': |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
592 test() |
