Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Pickling Algorithm |
| 3 | ------------------ |
| 4 | |
| 5 | This module implements a basic but powerful algorithm for "pickling" (a.k.a. |
| 6 | serializing, marshalling or flattening) nearly arbitrary Python objects. |
| 7 | This is a more primitive notion than persistency -- although pickle |
| 8 | reads and writes file objects, it does not handle the issue of naming |
| 9 | persistent objects, nor the (even more complicated) area of concurrent |
| 10 | access to persistent objects. The pickle module can transform a complex |
| 11 | object into a byte stream and it can transform the byte stream into |
| 12 | an object with the same internal structure. The most obvious thing to |
| 13 | do with these byte streams is to write them onto a file, but it is also |
| 14 | conceivable to send them across a network or store them in a database. |
| 15 | |
| 16 | Unlike the built-in marshal module, pickle handles the following correctly: |
| 17 | |
| 18 | - recursive objects |
| 19 | - pointer sharing |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 20 | - classes and class instances |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 21 | |
| 22 | Pickle is Python-specific. This has the advantage that there are no |
| 23 | restrictions imposed by external standards such as CORBA (which probably |
| 24 | can't represent pointer sharing or recursive objects); however it means |
| 25 | that non-Python programs may not be able to reconstruct pickled Python |
| 26 | objects. |
| 27 | |
| 28 | Pickle uses a printable ASCII representation. This is slightly more |
| 29 | voluminous than a binary representation. However, small integers actually |
| 30 | take *less* space when represented as minimal-size decimal strings than |
| 31 | when represented as 32-bit binary numbers, and strings are only much longer |
| 32 | if they contain control characters or 8-bit characters. The big advantage |
| 33 | of using printable ASCII (and of some other characteristics of pickle's |
| 34 | representation) is that for debugging or recovery purposes it is possible |
| 35 | for a human to read the pickled file with a standard text editor. (I could |
| 36 | have gone a step further and used a notation like S-expressions, but the |
| 37 | parser would have been considerably more complicated and slower, and the |
| 38 | files would probably have become much larger.) |
| 39 | |
| 40 | Pickle doesn't handle code objects, which marshal does. |
| 41 | I suppose pickle could, and maybe it should, but there's probably no |
| 42 | great need for it right now (as long as marshal continues to be used |
| 43 | for reading and writing code objects), and at least this avoids |
| 44 | the possibility of smuggling Trojan horses into a program. |
| 45 | |
| 46 | For the benefit of persistency modules written using pickle, it supports |
| 47 | the notion of a reference to an object outside the pickled data stream. |
| 48 | Such objects are referenced by a name, which is an arbitrary string of |
| 49 | printable ASCII characters. The resolution of such names is not defined |
| 50 | by the pickle module -- the persistent object module will have to implement |
| 51 | a method "persistent_load". To write references to persistent objects, |
| 52 | the persistent module must define a method "persistent_id" which returns |
| 53 | either None or the persistent ID of the object. |
| 54 | |
| 55 | There are some restrictions on the pickling of class instances. |
| 56 | |
| 57 | First of all, the class must be defined at the top level in a module. |
| 58 | |
Guido van Rossum | 37a6f16 | 1996-08-08 18:35:22 +0000 | [diff] [blame] | 59 | Next, it must normally be possible to create class instances by |
| 60 | calling the class without arguments. Usually, this is best |
| 61 | accomplished by providing default values for all arguments to its |
| 62 | __init__ method (if it has one). If this is undesirable, the |
| 63 | class can define a method __getinitargs__, which should return a |
| 64 | *tuple* containing the arguments to be passed to the class |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 65 | constructor. |
| 66 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 67 | Classes can influence how their instances are pickled -- if the class defines |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 68 | the method __getstate__, it is called and the return state is pickled |
| 69 | as the contents for the instance, and if the class defines the |
| 70 | method __setstate__, it is called with the unpickled state. (Note |
| 71 | that these methods can also be used to implement copying class instances.) |
| 72 | If there is no __getstate__ method, the instance's __dict__ |
| 73 | is pickled. If there is no __setstate__ method, the pickled object |
| 74 | must be a dictionary and its items are assigned to the new instance's |
| 75 | dictionary. (If a class defines both __getstate__ and __setstate__, |
| 76 | the state object needn't be a dictionary -- these methods can do what they |
| 77 | want.) |
| 78 | |
| 79 | Note that when class instances are pickled, their class's code and data |
| 80 | is not pickled along with them. Only the instance data is pickled. |
| 81 | This is done on purpose, so you can fix bugs in a class or add methods and |
| 82 | still load objects that were created with an earlier version of the |
| 83 | class. If you plan to have long-lived objects that will see many versions |
| 84 | of a class, it may be worth to put a version number in the objects so |
| 85 | that suitable conversions can be made by the class's __setstate__ method. |
| 86 | |
| 87 | The interface is as follows: |
| 88 | |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 89 | To pickle an object x onto a file f, open for writing: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 91 | p = pickle.Pickler(f) |
| 92 | p.dump(x) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 93 | |
| 94 | To unpickle an object x from a file f, open for reading: |
| 95 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 96 | u = pickle.Unpickler(f) |
| 97 | x = u.load() |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 98 | |
| 99 | The Pickler class only calls the method f.write with a string argument |
| 100 | (XXX possibly the interface should pass f.write instead of f). |
| 101 | The Unpickler calls the methods f.read(with an integer argument) |
| 102 | and f.readline(without argument), both returning a string. |
| 103 | It is explicitly allowed to pass non-file objects here, as long as they |
| 104 | have the right methods. |
| 105 | |
| 106 | The following types can be pickled: |
| 107 | |
| 108 | - None |
| 109 | - integers, long integers, floating point numbers |
| 110 | - strings |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 111 | - tuples, lists and dictionaries containing only picklable objects |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 112 | - class instances whose __dict__ or __setstate__() is picklable |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 113 | - classes |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 114 | |
| 115 | Attempts to pickle unpicklable objects will raise an exception |
| 116 | after having written an unspecified number of bytes to the file argument. |
| 117 | |
| 118 | It is possible to make multiple calls to Pickler.dump() or to |
| 119 | Unpickler.load(), as long as there is a one-to-one correspondence |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 120 | between pickler and Unpickler objects and between dump and load calls |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 121 | for any pair of corresponding Pickler and Unpicklers. WARNING: this |
| 122 | is intended for pickleing multiple objects without intervening modifications |
| 123 | to the objects or their parts. If you modify an object and then pickle |
| 124 | it again using the same Pickler instance, the object is not pickled |
| 125 | again -- a reference to it is pickled and the Unpickler will return |
| 126 | the old value, not the modified one. (XXX There are two problems here: |
| 127 | (a) detecting changes, and (b) marshalling a minimal set of changes. |
| 128 | I have no answers. Garbage Collection may also become a problem here.) |
| 129 | """ |
| 130 | |
Guido van Rossum | 8be9a11 | 1997-04-25 19:52:29 +0000 | [diff] [blame] | 131 | __version__ = "1.8" # Code version |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 132 | |
| 133 | from types import * |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 134 | from copy_reg import * |
| 135 | import string, marshal |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 137 | format_version = "1.2" # File format version we write |
| 138 | compatible_formats = ["1.0", "1.1"] # Old format versions we can read |
| 139 | |
| 140 | mdumps = marshal.dumps |
| 141 | mloads = marshal.loads |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 143 | PicklingError = "pickle.PicklingError" |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 144 | UnpicklingError = "pickle.UnpicklingError" |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 146 | MARK = '(' |
| 147 | STOP = '.' |
| 148 | POP = '0' |
| 149 | POP_MARK = '1' |
| 150 | DUP = '2' |
| 151 | FLOAT = 'F' |
| 152 | INT = 'I' |
| 153 | BININT = 'J' |
| 154 | BININT1 = 'K' |
| 155 | LONG = 'L' |
| 156 | BININT2 = 'M' |
| 157 | NONE = 'N' |
| 158 | PERSID = 'P' |
| 159 | BINPERSID = 'Q' |
| 160 | REDUCE = 'R' |
| 161 | STRING = 'S' |
| 162 | BINSTRING = 'T' |
| 163 | SHORT_BINSTRING = 'U' |
| 164 | APPEND = 'a' |
| 165 | BUILD = 'b' |
| 166 | GLOBAL = 'c' |
| 167 | DICT = 'd' |
| 168 | EMPTY_DICT = '}' |
| 169 | APPENDS = 'e' |
| 170 | GET = 'g' |
| 171 | BINGET = 'h' |
| 172 | INST = 'i' |
| 173 | LONG_BINGET = 'j' |
| 174 | LIST = 'l' |
| 175 | EMPTY_LIST = ']' |
| 176 | OBJ = 'o' |
| 177 | PUT = 'p' |
| 178 | BINPUT = 'q' |
| 179 | LONG_BINPUT = 'r' |
| 180 | SETITEM = 's' |
| 181 | TUPLE = 't' |
| 182 | EMPTY_TUPLE = ')' |
| 183 | SETITEMS = 'u' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 184 | |
| 185 | class Pickler: |
| 186 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 187 | def __init__(self, file, bin = 0): |
| 188 | self.write = file.write |
| 189 | self.memo = {} |
| 190 | self.bin = bin |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 192 | def dump(self, object): |
| 193 | self.save(object) |
| 194 | self.write(STOP) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 196 | def dump_special(self, callable, args, state = None): |
| 197 | if (type(args) is not TupleType): |
| 198 | raise PicklingError, "Second argument to dump_special " \ |
| 199 | "must be a tuple" |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 201 | self.save_reduce(callable, args, state) |
| 202 | self.write(STOP) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 204 | def put(self, i): |
| 205 | if (self.bin): |
| 206 | s = mdumps(i)[1:] |
| 207 | if (i < 256): |
| 208 | return BINPUT + s[0] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 210 | return LONG_BINPUT + s |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 212 | return PUT + `i` + '\n' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 214 | def get(self, i): |
| 215 | if (self.bin): |
| 216 | s = mdumps(i)[1:] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 218 | if (i < 256): |
| 219 | return BINGET + s[0] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 221 | return LONG_BINGET + s |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 223 | return GET + `i` + '\n' |
| 224 | |
| 225 | def save(self, object, pers_save = 0): |
| 226 | memo = self.memo |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 228 | if (not pers_save): |
| 229 | pid = self.persistent_id(object) |
| 230 | if (pid is not None): |
| 231 | self.save_pers(pid) |
| 232 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 234 | d = id(object) |
| 235 | |
| 236 | t = type(object) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 238 | if ((t is TupleType) and (len(object) == 0)): |
| 239 | if (self.bin): |
| 240 | self.save_empty_tuple(object) |
| 241 | else: |
| 242 | self.save_tuple(object) |
| 243 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 245 | if memo.has_key(d): |
| 246 | self.write(self.get(memo[d][0])) |
| 247 | return |
| 248 | |
| 249 | try: |
| 250 | f = self.dispatch[t] |
| 251 | except KeyError: |
| 252 | pid = self.inst_persistent_id(object) |
| 253 | if pid is not None: |
| 254 | self.save_pers(pid) |
| 255 | return |
| 256 | |
| 257 | try: |
| 258 | reduce = dispatch_table[t] |
| 259 | except KeyError: |
| 260 | try: |
| 261 | reduce = object.__reduce__ |
| 262 | except AttributeError: |
| 263 | raise PicklingError, \ |
| 264 | "can't pickle %s objects" % `t.__name__` |
| 265 | else: |
| 266 | tup = reduce() |
| 267 | else: |
| 268 | tup = reduce(object) |
| 269 | |
| 270 | if (type(tup) is not TupleType): |
| 271 | raise PicklingError, "Value returned by %s must be a " \ |
| 272 | "tuple" % reduce |
| 273 | |
| 274 | l = len(tup) |
| 275 | |
| 276 | if ((l != 2) and (l != 3)): |
| 277 | raise PicklingError, "tuple returned by %s must contain " \ |
| 278 | "only two or three elements" % reduce |
| 279 | |
| 280 | callable = tup[0] |
| 281 | arg_tup = tup[1] |
| 282 | |
| 283 | if (l > 2): |
| 284 | state = tup[2] |
| 285 | else: |
| 286 | state = None |
| 287 | |
| 288 | if (type(arg_tup) is not TupleType): |
| 289 | raise PicklingError, "Second element of tuple returned " \ |
| 290 | "by %s must be a tuple" % reduce |
| 291 | |
| 292 | self.save_reduce(callable, arg_tup, state) |
| 293 | return |
| 294 | |
| 295 | f(self, object) |
| 296 | |
| 297 | def persistent_id(self, object): |
| 298 | return None |
| 299 | |
| 300 | def inst_persistent_id(self, object): |
| 301 | return None |
| 302 | |
| 303 | def save_pers(self, pid): |
| 304 | if (not self.bin): |
| 305 | self.write(PERSID + str(pid) + '\n') |
| 306 | else: |
| 307 | self.save(pid, 1) |
| 308 | self.write(BINPERSID) |
| 309 | |
| 310 | def save_reduce(self, callable, arg_tup, state = None): |
| 311 | write = self.write |
| 312 | save = self.save |
| 313 | |
| 314 | save(callable) |
| 315 | save(arg_tup) |
| 316 | write(REDUCE) |
| 317 | |
| 318 | if (state is not None): |
| 319 | save(state) |
| 320 | write(BUILD) |
| 321 | |
| 322 | dispatch = {} |
| 323 | |
| 324 | def save_none(self, object): |
| 325 | self.write(NONE) |
| 326 | dispatch[NoneType] = save_none |
| 327 | |
| 328 | def save_int(self, object): |
| 329 | if (self.bin): |
| 330 | i = mdumps(object)[1:] |
| 331 | if (i[-2:] == '\000\000'): |
| 332 | if (i[-3] == '\000'): |
| 333 | self.write(BININT1 + i[:-3]) |
| 334 | return |
| 335 | |
| 336 | self.write(BININT2 + i[:-2]) |
| 337 | return |
| 338 | |
| 339 | self.write(BININT + i) |
| 340 | else: |
| 341 | self.write(INT + `object` + '\n') |
| 342 | dispatch[IntType] = save_int |
| 343 | |
| 344 | def save_long(self, object): |
| 345 | self.write(LONG + `object` + '\n') |
| 346 | dispatch[LongType] = save_long |
| 347 | |
| 348 | def save_float(self, object): |
| 349 | self.write(FLOAT + `object` + '\n') |
| 350 | dispatch[FloatType] = save_float |
| 351 | |
| 352 | def save_string(self, object): |
| 353 | d = id(object) |
| 354 | memo = self.memo |
| 355 | |
| 356 | if (self.bin): |
| 357 | l = len(object) |
| 358 | s = mdumps(l)[1:] |
| 359 | if (l < 256): |
| 360 | self.write(SHORT_BINSTRING + s[0] + object) |
| 361 | else: |
| 362 | self.write(BINSTRING + s + object) |
| 363 | else: |
| 364 | self.write(STRING + `object` + '\n') |
| 365 | |
| 366 | memo_len = len(memo) |
| 367 | self.write(self.put(memo_len)) |
| 368 | memo[d] = (memo_len, object) |
| 369 | dispatch[StringType] = save_string |
| 370 | |
| 371 | def save_tuple(self, object): |
| 372 | |
| 373 | write = self.write |
| 374 | save = self.save |
| 375 | memo = self.memo |
| 376 | |
| 377 | d = id(object) |
| 378 | |
| 379 | write(MARK) |
| 380 | |
| 381 | for element in object: |
| 382 | save(element) |
| 383 | |
| 384 | if (len(object) and memo.has_key(d)): |
| 385 | if (self.bin): |
| 386 | write(POP_MARK + self.get(memo[d][0])) |
| 387 | return |
| 388 | |
| 389 | write(POP * (len(object) + 1) + self.get(mem[d][0])) |
| 390 | return |
| 391 | |
| 392 | memo_len = len(memo) |
| 393 | self.write(TUPLE + self.put(memo_len)) |
| 394 | memo[d] = (memo_len, object) |
| 395 | dispatch[TupleType] = save_tuple |
| 396 | |
| 397 | def save_empty_tuple(self, object): |
| 398 | self.write(EMPTY_TUPLE) |
| 399 | |
| 400 | def save_list(self, object): |
| 401 | d = id(object) |
| 402 | |
| 403 | write = self.write |
| 404 | save = self.save |
| 405 | memo = self.memo |
| 406 | |
| 407 | if (self.bin): |
| 408 | write(EMPTY_LIST) |
| 409 | else: |
| 410 | write(MARK + LIST) |
| 411 | |
| 412 | memo_len = len(memo) |
| 413 | write(self.put(memo_len)) |
| 414 | memo[d] = (memo_len, object) |
| 415 | |
| 416 | using_appends = (self.bin and (len(object) > 1)) |
| 417 | |
| 418 | if (using_appends): |
| 419 | write(MARK) |
| 420 | |
| 421 | for element in object: |
| 422 | save(element) |
| 423 | |
| 424 | if (not using_appends): |
| 425 | write(APPEND) |
| 426 | |
| 427 | if (using_appends): |
| 428 | write(APPENDS) |
| 429 | dispatch[ListType] = save_list |
| 430 | |
| 431 | def save_dict(self, object): |
| 432 | d = id(object) |
| 433 | |
| 434 | write = self.write |
| 435 | save = self.save |
| 436 | memo = self.memo |
| 437 | |
| 438 | if (self.bin): |
| 439 | write(EMPTY_DICT) |
| 440 | else: |
| 441 | write(MARK + DICT) |
| 442 | |
| 443 | memo_len = len(memo) |
| 444 | self.write(self.put(memo_len)) |
| 445 | memo[d] = (memo_len, object) |
| 446 | |
| 447 | using_setitems = (self.bin and (len(object) > 1)) |
| 448 | |
| 449 | if (using_setitems): |
| 450 | write(MARK) |
| 451 | |
| 452 | items = object.items() |
| 453 | for key, value in items: |
| 454 | save(key) |
| 455 | save(value) |
| 456 | |
| 457 | if (not using_setitems): |
| 458 | write(SETITEM) |
| 459 | |
| 460 | if (using_setitems): |
| 461 | write(SETITEMS) |
| 462 | |
| 463 | dispatch[DictionaryType] = save_dict |
| 464 | |
| 465 | def save_inst(self, object): |
| 466 | d = id(object) |
| 467 | cls = object.__class__ |
| 468 | |
| 469 | memo = self.memo |
| 470 | write = self.write |
| 471 | save = self.save |
| 472 | |
| 473 | if hasattr(object, '__getinitargs__'): |
| 474 | args = object.__getinitargs__() |
| 475 | len(args) # XXX Assert it's a sequence |
| 476 | else: |
| 477 | args = () |
| 478 | |
| 479 | write(MARK) |
| 480 | |
| 481 | if (self.bin): |
| 482 | save(cls) |
| 483 | |
| 484 | for arg in args: |
| 485 | save(arg) |
| 486 | |
| 487 | memo_len = len(memo) |
| 488 | if (self.bin): |
| 489 | write(OBJ + self.put(memo_len)) |
| 490 | else: |
| 491 | module = whichmodule(cls, cls.__name__) |
| 492 | name = cls.__name__ |
| 493 | write(INST + module + '\n' + name + '\n' + |
| 494 | self.put(memo_len)) |
| 495 | |
| 496 | memo[d] = (memo_len, object) |
| 497 | |
| 498 | try: |
| 499 | getstate = object.__getstate__ |
| 500 | except AttributeError: |
| 501 | stuff = object.__dict__ |
| 502 | else: |
| 503 | stuff = getstate() |
| 504 | save(stuff) |
| 505 | write(BUILD) |
| 506 | dispatch[InstanceType] = save_inst |
| 507 | |
| 508 | def save_global(self, object, name = None): |
| 509 | write = self.write |
| 510 | memo = self.memo |
| 511 | |
| 512 | if (name is None): |
| 513 | name = object.__name__ |
| 514 | |
| 515 | module = whichmodule(object, name) |
| 516 | |
| 517 | memo_len = len(memo) |
| 518 | write(GLOBAL + module + '\n' + name + '\n' + |
| 519 | self.put(memo_len)) |
| 520 | memo[id(object)] = (memo_len, object) |
| 521 | dispatch[ClassType] = save_global |
| 522 | dispatch[FunctionType] = save_global |
| 523 | dispatch[BuiltinFunctionType] = save_global |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 525 | |
| 526 | classmap = {} |
| 527 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 528 | def whichmodule(cls, clsname): |
| 529 | """Figure out the module in which a class occurs. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 531 | Search sys.modules for the module. |
| 532 | Cache in classmap. |
| 533 | Return a module name. |
| 534 | If the class cannot be found, return __main__. |
| 535 | """ |
| 536 | if classmap.has_key(cls): |
| 537 | return classmap[cls] |
| 538 | import sys |
| 539 | |
| 540 | for name, module in sys.modules.items(): |
Guido van Rossum | 8be9a11 | 1997-04-25 19:52:29 +0000 | [diff] [blame] | 541 | if name != '__main__' and \ |
| 542 | hasattr(module, clsname) and \ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 543 | getattr(module, clsname) is cls: |
| 544 | break |
| 545 | else: |
| 546 | name = '__main__' |
| 547 | classmap[cls] = name |
| 548 | return name |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 549 | |
| 550 | |
| 551 | class Unpickler: |
| 552 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 553 | def __init__(self, file): |
| 554 | self.readline = file.readline |
| 555 | self.read = file.read |
| 556 | self.memo = {} |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 558 | def load(self): |
| 559 | self.mark = ['spam'] # Any new unique object |
| 560 | self.stack = [] |
| 561 | self.append = self.stack.append |
| 562 | read = self.read |
| 563 | dispatch = self.dispatch |
| 564 | try: |
| 565 | while 1: |
| 566 | key = read(1) |
| 567 | dispatch[key](self) |
| 568 | except STOP, value: |
| 569 | return value |
| 570 | |
| 571 | def marker(self): |
| 572 | stack = self.stack |
| 573 | mark = self.mark |
| 574 | k = len(stack)-1 |
| 575 | while stack[k] is not mark: k = k-1 |
| 576 | return k |
| 577 | |
| 578 | dispatch = {} |
| 579 | |
| 580 | def load_eof(self): |
| 581 | raise EOFError |
| 582 | dispatch[''] = load_eof |
| 583 | |
| 584 | def load_persid(self): |
| 585 | pid = self.readline()[:-1] |
| 586 | self.append(self.persistent_load(pid)) |
| 587 | dispatch[PERSID] = load_persid |
| 588 | |
| 589 | def load_binpersid(self): |
| 590 | stack = self.stack |
| 591 | |
| 592 | pid = stack[-1] |
| 593 | del stack[-1] |
| 594 | |
| 595 | self.append(self.persistent_load(pid)) |
| 596 | dispatch[BINPERSID] = load_binpersid |
| 597 | |
| 598 | def load_none(self): |
| 599 | self.append(None) |
| 600 | dispatch[NONE] = load_none |
| 601 | |
| 602 | def load_int(self): |
| 603 | self.append(string.atoi(self.readline()[:-1], 0)) |
| 604 | dispatch[INT] = load_int |
| 605 | |
| 606 | def load_binint(self): |
| 607 | self.append(mloads('i' + self.read(4))) |
| 608 | dispatch[BININT] = load_binint |
| 609 | |
| 610 | def load_binint1(self): |
| 611 | self.append(mloads('i' + self.read(1) + '\000\000\000')) |
| 612 | dispatch[BININT1] = load_binint1 |
| 613 | |
| 614 | def load_binint2(self): |
| 615 | self.append(mloads('i' + self.read(2) + '\000\000')) |
| 616 | dispatch[BININT2] = load_binint2 |
| 617 | |
| 618 | def load_long(self): |
| 619 | self.append(string.atol(self.readline()[:-1], 0)) |
| 620 | dispatch[LONG] = load_long |
| 621 | |
| 622 | def load_float(self): |
| 623 | self.append(string.atof(self.readline()[:-1])) |
| 624 | dispatch[FLOAT] = load_float |
| 625 | |
| 626 | def load_string(self): |
| 627 | self.append(eval(self.readline()[:-1], |
| 628 | {'__builtins__': {}})) # Let's be careful |
| 629 | dispatch[STRING] = load_string |
| 630 | |
| 631 | def load_binstring(self): |
| 632 | len = mloads('i' + self.read(4)) |
| 633 | self.append(self.read(len)) |
| 634 | dispatch[BINSTRING] = load_binstring |
| 635 | |
| 636 | def load_short_binstring(self): |
| 637 | len = mloads('i' + self.read(1) + '\000\000\000') |
| 638 | self.append(self.read(len)) |
| 639 | dispatch[SHORT_BINSTRING] = load_short_binstring |
| 640 | |
| 641 | def load_tuple(self): |
| 642 | k = self.marker() |
| 643 | self.stack[k:] = [tuple(self.stack[k+1:])] |
| 644 | dispatch[TUPLE] = load_tuple |
| 645 | |
| 646 | def load_empty_tuple(self): |
| 647 | self.stack.append(()) |
| 648 | dispatch[EMPTY_TUPLE] = load_empty_tuple |
| 649 | |
| 650 | def load_empty_list(self): |
| 651 | self.stack.append([]) |
| 652 | dispatch[EMPTY_LIST] = load_empty_list |
| 653 | |
| 654 | def load_empty_dictionary(self): |
| 655 | self.stack.append({}) |
| 656 | dispatch[EMPTY_DICT] = load_empty_dictionary |
| 657 | |
| 658 | def load_list(self): |
| 659 | k = self.marker() |
| 660 | self.stack[k:] = [self.stack[k+1:]] |
| 661 | dispatch[LIST] = load_list |
| 662 | |
| 663 | def load_dict(self): |
| 664 | k = self.marker() |
| 665 | d = {} |
| 666 | items = self.stack[k+1:] |
| 667 | for i in range(0, len(items), 2): |
| 668 | key = items[i] |
| 669 | value = items[i+1] |
| 670 | d[key] = value |
| 671 | self.stack[k:] = [d] |
| 672 | dispatch[DICT] = load_dict |
| 673 | |
| 674 | def load_inst(self): |
| 675 | k = self.marker() |
| 676 | args = tuple(self.stack[k+1:]) |
| 677 | del self.stack[k:] |
| 678 | module = self.readline()[:-1] |
| 679 | name = self.readline()[:-1] |
| 680 | klass = self.find_class(module, name) |
Guido van Rossum | 8be9a11 | 1997-04-25 19:52:29 +0000 | [diff] [blame] | 681 | ## if (type(klass) is not ClassType): |
| 682 | ## raise SystemError, "Imported object %s from module %s is " \ |
| 683 | ## "not a class" % (name, module) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 684 | |
| 685 | value = apply(klass, args) |
| 686 | self.append(value) |
| 687 | dispatch[INST] = load_inst |
| 688 | |
| 689 | def load_obj(self): |
| 690 | stack = self.stack |
| 691 | k = self.marker() |
| 692 | klass = stack[k + 1] |
| 693 | del stack[k + 1] |
| 694 | args = tuple(stack[k + 1:]) |
| 695 | del stack[k:] |
| 696 | value = apply(klass, args) |
| 697 | self.append(value) |
| 698 | dispatch[OBJ] = load_obj |
| 699 | |
| 700 | def load_global(self): |
| 701 | module = self.readline()[:-1] |
| 702 | name = self.readline()[:-1] |
| 703 | klass = self.find_class(module, name) |
| 704 | self.append(klass) |
| 705 | dispatch[GLOBAL] = load_global |
| 706 | |
| 707 | def find_class(self, module, name): |
| 708 | env = {} |
| 709 | |
| 710 | try: |
| 711 | exec 'from %s import %s' % (module, name) in env |
| 712 | except ImportError: |
| 713 | raise SystemError, \ |
| 714 | "Failed to import class %s from module %s" % \ |
| 715 | (name, module) |
| 716 | klass = env[name] |
| 717 | return klass |
| 718 | |
| 719 | def load_reduce(self): |
| 720 | stack = self.stack |
| 721 | |
| 722 | callable = stack[-2] |
| 723 | arg_tup = stack[-1] |
| 724 | del stack[-2:] |
| 725 | |
| 726 | if (type(callable) is not ClassType): |
| 727 | if (not safe_constructors.has_key(callable)): |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 728 | try: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 729 | safe = callable.__safe_for_unpickling__ |
| 730 | except AttributeError: |
| 731 | safe = None |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 732 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 733 | if (not safe): |
| 734 | raise UnpicklingError, "%s is not safe for " \ |
| 735 | "unpickling" % callable |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 737 | value = apply(callable, arg_tup) |
| 738 | self.append(value) |
| 739 | dispatch[REDUCE] = load_reduce |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 741 | def load_pop(self): |
| 742 | del self.stack[-1] |
| 743 | dispatch[POP] = load_pop |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 745 | def load_pop_mark(self): |
| 746 | k = self.marker() |
| 747 | del self.stack[k:] |
| 748 | dispatch[POP_MARK] = load_pop_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 749 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 750 | def load_dup(self): |
| 751 | self.append(stack[-1]) |
| 752 | dispatch[DUP] = load_dup |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 754 | def load_get(self): |
| 755 | self.append(self.memo[self.readline()[:-1]]) |
| 756 | dispatch[GET] = load_get |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 757 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 758 | def load_binget(self): |
| 759 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 760 | self.append(self.memo[`i`]) |
| 761 | dispatch[BINGET] = load_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 763 | def load_long_binget(self): |
| 764 | i = mloads('i' + self.read(4)) |
| 765 | self.append(self.memo[`i`]) |
| 766 | dispatch[LONG_BINGET] = load_long_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 767 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 768 | def load_put(self): |
| 769 | self.memo[self.readline()[:-1]] = self.stack[-1] |
| 770 | dispatch[PUT] = load_put |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 771 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 772 | def load_binput(self): |
| 773 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 774 | self.memo[`i`] = self.stack[-1] |
| 775 | dispatch[BINPUT] = load_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 776 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 777 | def load_long_binput(self): |
| 778 | i = mloads('i' + self.read(4)) |
| 779 | self.memo[`i`] = self.stack[-1] |
| 780 | dispatch[LONG_BINPUT] = load_long_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 782 | def load_append(self): |
| 783 | stack = self.stack |
| 784 | value = stack[-1] |
| 785 | del stack[-1] |
| 786 | list = stack[-1] |
| 787 | list.append(value) |
| 788 | dispatch[APPEND] = load_append |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 790 | def load_appends(self): |
| 791 | stack = self.stack |
| 792 | mark = self.marker() |
| 793 | list = stack[mark - 1] |
| 794 | for i in range(mark + 1, len(stack)): |
| 795 | list.append(stack[i]) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 797 | del stack[mark:] |
| 798 | dispatch[APPENDS] = load_appends |
| 799 | |
| 800 | def load_setitem(self): |
| 801 | stack = self.stack |
| 802 | value = stack[-1] |
| 803 | key = stack[-2] |
| 804 | del stack[-2:] |
| 805 | dict = stack[-1] |
| 806 | dict[key] = value |
| 807 | dispatch[SETITEM] = load_setitem |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 809 | def load_setitems(self): |
| 810 | stack = self.stack |
| 811 | mark = self.marker() |
| 812 | dict = stack[mark - 1] |
| 813 | for i in range(mark + 1, len(stack), 2): |
| 814 | dict[stack[i]] = stack[i + 1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 816 | del stack[mark:] |
| 817 | dispatch[SETITEMS] = load_setitems |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 819 | def load_build(self): |
| 820 | stack = self.stack |
| 821 | value = stack[-1] |
| 822 | del stack[-1] |
| 823 | inst = stack[-1] |
| 824 | try: |
| 825 | setstate = inst.__setstate__ |
| 826 | except AttributeError: |
| 827 | for key in value.keys(): |
| 828 | setattr(inst, key, value[key]) |
| 829 | else: |
| 830 | setstate(value) |
| 831 | dispatch[BUILD] = load_build |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 832 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 833 | def load_mark(self): |
| 834 | self.append(self.mark) |
| 835 | dispatch[MARK] = load_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 836 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 837 | def load_stop(self): |
| 838 | value = self.stack[-1] |
| 839 | del self.stack[-1] |
| 840 | raise STOP, value |
| 841 | dispatch[STOP] = load_stop |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 842 | |
| 843 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 844 | # Shorthands |
| 845 | |
Guido van Rossum | c7c5e69 | 1996-07-22 22:26:07 +0000 | [diff] [blame] | 846 | from StringIO import StringIO |
| 847 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 848 | def dump(object, file, bin = 0): |
| 849 | Pickler(file, bin).dump(object) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 850 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 851 | def dumps(object, bin = 0): |
| 852 | file = StringIO() |
| 853 | Pickler(file, bin).dump(object) |
| 854 | return file.getvalue() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 855 | |
| 856 | def load(file): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 857 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 858 | |
| 859 | def loads(str): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 860 | file = StringIO(str) |
| 861 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 862 | |
| 863 | |
| 864 | # The rest is used for testing only |
| 865 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 866 | class C: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 867 | def __cmp__(self, other): |
| 868 | return cmp(self.__dict__, other.__dict__) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 869 | |
| 870 | def test(): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 871 | fn = 'out' |
| 872 | c = C() |
| 873 | c.foo = 1 |
| 874 | c.bar = 2 |
| 875 | x = [0, 1, 2, 3] |
| 876 | y = ('abc', 'abc', c, c) |
| 877 | x.append(y) |
| 878 | x.append(y) |
| 879 | x.append(5) |
| 880 | f = open(fn, 'w') |
| 881 | F = Pickler(f) |
| 882 | F.dump(x) |
| 883 | f.close() |
| 884 | f = open(fn, 'r') |
| 885 | U = Unpickler(f) |
| 886 | x2 = U.load() |
| 887 | print x |
| 888 | print x2 |
| 889 | print x == x2 |
| 890 | print map(id, x) |
| 891 | print map(id, x2) |
| 892 | print F.memo |
| 893 | print U.memo |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 894 | |
| 895 | if __name__ == '__main__': |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 896 | test() |