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 | |
| 59 | Next, it must normally be possible to create class instances by calling |
| 60 | the class without arguments. If this is undesirable, the class can |
| 61 | define a method __getinitargs__ (XXX not a pretty name!), which should |
| 62 | return a *tuple* containing the arguments to be passed to the class |
| 63 | constructor. |
| 64 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 65 | 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] | 66 | the method __getstate__, it is called and the return state is pickled |
| 67 | as the contents for the instance, and if the class defines the |
| 68 | method __setstate__, it is called with the unpickled state. (Note |
| 69 | that these methods can also be used to implement copying class instances.) |
| 70 | If there is no __getstate__ method, the instance's __dict__ |
| 71 | is pickled. If there is no __setstate__ method, the pickled object |
| 72 | must be a dictionary and its items are assigned to the new instance's |
| 73 | dictionary. (If a class defines both __getstate__ and __setstate__, |
| 74 | the state object needn't be a dictionary -- these methods can do what they |
| 75 | want.) |
| 76 | |
| 77 | Note that when class instances are pickled, their class's code and data |
| 78 | is not pickled along with them. Only the instance data is pickled. |
| 79 | This is done on purpose, so you can fix bugs in a class or add methods and |
| 80 | still load objects that were created with an earlier version of the |
| 81 | class. If you plan to have long-lived objects that will see many versions |
| 82 | of a class, it may be worth to put a version number in the objects so |
| 83 | that suitable conversions can be made by the class's __setstate__ method. |
| 84 | |
| 85 | The interface is as follows: |
| 86 | |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 87 | 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] | 88 | |
| 89 | p = pickle.Pickler(f) |
| 90 | p.dump(x) |
| 91 | |
| 92 | To unpickle an object x from a file f, open for reading: |
| 93 | |
| 94 | u = pickle.Unpickler(f) |
Guido van Rossum | 48aa82e | 1995-04-10 11:34:46 +0000 | [diff] [blame] | 95 | x = u.load() |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 96 | |
| 97 | The Pickler class only calls the method f.write with a string argument |
| 98 | (XXX possibly the interface should pass f.write instead of f). |
| 99 | The Unpickler calls the methods f.read(with an integer argument) |
| 100 | and f.readline(without argument), both returning a string. |
| 101 | It is explicitly allowed to pass non-file objects here, as long as they |
| 102 | have the right methods. |
| 103 | |
| 104 | The following types can be pickled: |
| 105 | |
| 106 | - None |
| 107 | - integers, long integers, floating point numbers |
| 108 | - strings |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 109 | - tuples, lists and dictionaries containing only picklable objects |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 110 | - class instances whose __dict__ or __setstate__() is picklable |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 111 | - classes |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 112 | |
| 113 | Attempts to pickle unpicklable objects will raise an exception |
| 114 | after having written an unspecified number of bytes to the file argument. |
| 115 | |
| 116 | It is possible to make multiple calls to Pickler.dump() or to |
| 117 | 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] | 118 | between pickler and Unpickler objects and between dump and load calls |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 119 | for any pair of corresponding Pickler and Unpicklers. WARNING: this |
| 120 | is intended for pickleing multiple objects without intervening modifications |
| 121 | to the objects or their parts. If you modify an object and then pickle |
| 122 | it again using the same Pickler instance, the object is not pickled |
| 123 | again -- a reference to it is pickled and the Unpickler will return |
| 124 | the old value, not the modified one. (XXX There are two problems here: |
| 125 | (a) detecting changes, and (b) marshalling a minimal set of changes. |
| 126 | I have no answers. Garbage Collection may also become a problem here.) |
| 127 | """ |
| 128 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 129 | __version__ = "1.5" # Code version |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 130 | |
| 131 | from types import * |
| 132 | import string |
| 133 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 134 | format_version = "1.1" # File format version we write |
| 135 | compatible_formats = ["1.0"] # Old format versions we can read |
| 136 | |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 137 | PicklingError = "pickle.PicklingError" |
| 138 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 139 | AtomicTypes = [NoneType, IntType, FloatType, StringType] |
| 140 | |
| 141 | def safe(object): |
| 142 | t = type(object) |
| 143 | if t in AtomicTypes: |
| 144 | return 1 |
| 145 | if t is TupleType: |
| 146 | for item in object: |
| 147 | if not safe(item): return 0 |
| 148 | return 1 |
| 149 | return 0 |
| 150 | |
| 151 | MARK = '(' |
| 152 | POP = '0' |
| 153 | DUP = '2' |
| 154 | STOP = '.' |
| 155 | TUPLE = 't' |
| 156 | LIST = 'l' |
| 157 | DICT = 'd' |
| 158 | INST = 'i' |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 159 | CLASS = 'c' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 160 | GET = 'g' |
| 161 | PUT = 'p' |
| 162 | APPEND = 'a' |
| 163 | SETITEM = 's' |
| 164 | BUILD = 'b' |
| 165 | NONE = 'N' |
| 166 | INT = 'I' |
| 167 | LONG = 'L' |
| 168 | FLOAT = 'F' |
| 169 | STRING = 'S' |
| 170 | PERSID = 'P' |
| 171 | AtomicKeys = [NONE, INT, LONG, FLOAT, STRING] |
| 172 | AtomicMap = { |
| 173 | NoneType: NONE, |
| 174 | IntType: INT, |
| 175 | LongType: LONG, |
| 176 | FloatType: FLOAT, |
| 177 | StringType: STRING, |
| 178 | } |
| 179 | |
| 180 | class Pickler: |
| 181 | |
| 182 | def __init__(self, file): |
| 183 | self.write = file.write |
| 184 | self.memo = {} |
| 185 | |
| 186 | def dump(self, object): |
| 187 | self.save(object) |
| 188 | self.write(STOP) |
| 189 | |
| 190 | def save(self, object): |
| 191 | pid = self.persistent_id(object) |
| 192 | if pid: |
| 193 | self.write(PERSID + str(pid) + '\n') |
| 194 | return |
| 195 | d = id(object) |
| 196 | if self.memo.has_key(d): |
| 197 | self.write(GET + `d` + '\n') |
| 198 | return |
| 199 | t = type(object) |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 200 | try: |
| 201 | f = self.dispatch[t] |
| 202 | except KeyError: |
| 203 | raise PicklingError, \ |
| 204 | "can't pickle %s objects" % `t.__name__` |
| 205 | f(self, object) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 206 | |
| 207 | def persistent_id(self, object): |
| 208 | return None |
| 209 | |
| 210 | dispatch = {} |
| 211 | |
| 212 | def save_none(self, object): |
| 213 | self.write(NONE) |
| 214 | dispatch[NoneType] = save_none |
| 215 | |
| 216 | def save_int(self, object): |
| 217 | self.write(INT + `object` + '\n') |
| 218 | dispatch[IntType] = save_int |
| 219 | |
| 220 | def save_long(self, object): |
| 221 | self.write(LONG + `object` + '\n') |
| 222 | dispatch[LongType] = save_long |
| 223 | |
| 224 | def save_float(self, object): |
| 225 | self.write(FLOAT + `object` + '\n') |
| 226 | dispatch[FloatType] = save_float |
| 227 | |
| 228 | def save_string(self, object): |
| 229 | d = id(object) |
| 230 | self.write(STRING + `object` + '\n') |
| 231 | self.write(PUT + `d` + '\n') |
| 232 | self.memo[d] = object |
| 233 | dispatch[StringType] = save_string |
| 234 | |
| 235 | def save_tuple(self, object): |
| 236 | d = id(object) |
| 237 | self.write(MARK) |
| 238 | n = len(object) |
| 239 | for k in range(n): |
| 240 | self.save(object[k]) |
| 241 | if self.memo.has_key(d): |
| 242 | # Saving object[k] has saved us! |
| 243 | while k >= 0: |
| 244 | self.write(POP) |
| 245 | k = k-1 |
| 246 | self.write(GET + `d` + '\n') |
| 247 | break |
| 248 | else: |
| 249 | self.write(TUPLE + PUT + `d` + '\n') |
| 250 | self.memo[d] = object |
| 251 | dispatch[TupleType] = save_tuple |
| 252 | |
| 253 | def save_list(self, object): |
| 254 | d = id(object) |
| 255 | self.write(MARK) |
| 256 | n = len(object) |
| 257 | for k in range(n): |
| 258 | item = object[k] |
| 259 | if not safe(item): |
| 260 | break |
| 261 | self.save(item) |
| 262 | else: |
| 263 | k = n |
| 264 | self.write(LIST + PUT + `d` + '\n') |
| 265 | self.memo[d] = object |
| 266 | for k in range(k, n): |
| 267 | item = object[k] |
| 268 | self.save(item) |
| 269 | self.write(APPEND) |
| 270 | dispatch[ListType] = save_list |
| 271 | |
| 272 | def save_dict(self, object): |
| 273 | d = id(object) |
| 274 | self.write(MARK) |
| 275 | items = object.items() |
| 276 | n = len(items) |
| 277 | for k in range(n): |
| 278 | key, value = items[k] |
| 279 | if not safe(key) or not safe(value): |
| 280 | break |
| 281 | self.save(key) |
| 282 | self.save(value) |
| 283 | else: |
| 284 | k = n |
| 285 | self.write(DICT + PUT + `d` + '\n') |
| 286 | self.memo[d] = object |
| 287 | for k in range(k, n): |
| 288 | key, value = items[k] |
| 289 | self.save(key) |
| 290 | self.save(value) |
| 291 | self.write(SETITEM) |
| 292 | dispatch[DictionaryType] = save_dict |
| 293 | |
| 294 | def save_inst(self, object): |
| 295 | d = id(object) |
| 296 | cls = object.__class__ |
| 297 | module = whichmodule(cls) |
| 298 | name = cls.__name__ |
| 299 | if hasattr(object, '__getinitargs__'): |
| 300 | args = object.__getinitargs__() |
| 301 | len(args) # XXX Assert it's a sequence |
| 302 | else: |
| 303 | args = () |
| 304 | self.write(MARK) |
| 305 | for arg in args: |
| 306 | self.save(arg) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 307 | self.write(INST + module + '\n' + name + '\n' + |
| 308 | PUT + `d` + '\n') |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 309 | self.memo[d] = object |
| 310 | try: |
| 311 | getstate = object.__getstate__ |
| 312 | except AttributeError: |
| 313 | stuff = object.__dict__ |
| 314 | else: |
| 315 | stuff = getstate() |
| 316 | self.save(stuff) |
| 317 | self.write(BUILD) |
| 318 | dispatch[InstanceType] = save_inst |
| 319 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 320 | def save_class(self, object): |
| 321 | d = id(object) |
| 322 | module = whichmodule(object) |
| 323 | name = object.__name__ |
| 324 | self.write(CLASS + module + '\n' + name + '\n' + |
| 325 | PUT + `d` + '\n') |
| 326 | dispatch[ClassType] = save_class |
| 327 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 328 | |
| 329 | classmap = {} |
| 330 | |
| 331 | def whichmodule(cls): |
| 332 | """Figure out the module in which a class occurs. |
| 333 | |
| 334 | Search sys.modules for the module. |
| 335 | Cache in classmap. |
| 336 | Return a module name. |
| 337 | If the class cannot be found, return __main__. |
| 338 | """ |
| 339 | if classmap.has_key(cls): |
| 340 | return classmap[cls] |
| 341 | import sys |
| 342 | clsname = cls.__name__ |
| 343 | for name, module in sys.modules.items(): |
Guido van Rossum | f71c79b | 1995-06-22 18:51:23 +0000 | [diff] [blame] | 344 | if name != '__main__' and \ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 345 | hasattr(module, clsname) and \ |
| 346 | getattr(module, clsname) is cls: |
| 347 | break |
| 348 | else: |
| 349 | name = '__main__' |
| 350 | classmap[cls] = name |
| 351 | return name |
| 352 | |
| 353 | |
| 354 | class Unpickler: |
| 355 | |
| 356 | def __init__(self, file): |
| 357 | self.readline = file.readline |
| 358 | self.read = file.read |
| 359 | self.memo = {} |
| 360 | |
| 361 | def load(self): |
| 362 | self.mark = ['spam'] # Any new unique object |
| 363 | self.stack = [] |
| 364 | try: |
| 365 | while 1: |
| 366 | key = self.read(1) |
| 367 | self.dispatch[key](self) |
| 368 | except STOP, value: |
| 369 | return value |
| 370 | |
| 371 | def marker(self): |
| 372 | k = len(self.stack)-1 |
| 373 | while self.stack[k] != self.mark: k = k-1 |
| 374 | return k |
| 375 | |
| 376 | dispatch = {} |
| 377 | |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 378 | def load_eof(self): |
| 379 | raise EOFError |
| 380 | dispatch[''] = load_eof |
| 381 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 382 | def load_persid(self): |
| 383 | pid = self.readline()[:-1] |
Guido van Rossum | e0bfd50 | 1995-08-07 20:16:58 +0000 | [diff] [blame] | 384 | self.stack.append(self.persistent_load(pid)) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 385 | dispatch[PERSID] = load_persid |
| 386 | |
| 387 | def load_none(self): |
| 388 | self.stack.append(None) |
| 389 | dispatch[NONE] = load_none |
| 390 | |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 391 | def load_int(self): |
Guido van Rossum | 955c5d1 | 1996-05-15 22:49:57 +0000 | [diff] [blame] | 392 | self.stack.append(string.atoi(self.readline()[:-1], 0)) |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 393 | dispatch[INT] = load_int |
| 394 | |
| 395 | def load_long(self): |
Guido van Rossum | 955c5d1 | 1996-05-15 22:49:57 +0000 | [diff] [blame] | 396 | self.stack.append(string.atol(self.readline()[:-1], 0)) |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 397 | dispatch[LONG] = load_long |
| 398 | |
| 399 | def load_float(self): |
| 400 | self.stack.append(string.atof(self.readline()[:-1])) |
| 401 | dispatch[FLOAT] = load_float |
| 402 | |
| 403 | def load_string(self): |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 404 | self.stack.append(eval(self.readline()[:-1])) |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 405 | dispatch[STRING] = load_string |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 406 | |
| 407 | def load_tuple(self): |
| 408 | k = self.marker() |
| 409 | self.stack[k:] = [tuple(self.stack[k+1:])] |
| 410 | dispatch[TUPLE] = load_tuple |
| 411 | |
| 412 | def load_list(self): |
| 413 | k = self.marker() |
| 414 | self.stack[k:] = [self.stack[k+1:]] |
| 415 | dispatch[LIST] = load_list |
| 416 | |
| 417 | def load_dict(self): |
| 418 | k = self.marker() |
| 419 | d = {} |
| 420 | items = self.stack[k+1:] |
| 421 | for i in range(0, len(items), 2): |
| 422 | key = items[i] |
| 423 | value = items[i+1] |
| 424 | d[key] = value |
| 425 | self.stack[k:] = [d] |
| 426 | dispatch[DICT] = load_dict |
| 427 | |
| 428 | def load_inst(self): |
| 429 | k = self.marker() |
| 430 | args = tuple(self.stack[k+1:]) |
| 431 | del self.stack[k:] |
| 432 | module = self.readline()[:-1] |
| 433 | name = self.readline()[:-1] |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 434 | klass = self.find_class(module, name) |
| 435 | value = apply(klass, args) |
| 436 | self.stack.append(value) |
| 437 | dispatch[INST] = load_inst |
| 438 | |
| 439 | def load_class(self): |
| 440 | module = self.readline()[:-1] |
| 441 | name = self.readline()[:-1] |
| 442 | klass = self.find_class(module, name) |
| 443 | self.stack.append(klass) |
| 444 | return klass |
| 445 | dispatch[CLASS] = load_class |
| 446 | |
| 447 | def find_class(self, module, name): |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 448 | env = {} |
| 449 | try: |
| 450 | exec 'from %s import %s' % (module, name) in env |
| 451 | except ImportError: |
| 452 | raise SystemError, \ |
| 453 | "Failed to import class %s from module %s" % \ |
| 454 | (name, module) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 455 | klass = env[name] |
| 456 | if type(klass) != ClassType: |
| 457 | raise SystemError, \ |
| 458 | "Imported object %s from module %s is not a class" % \ |
| 459 | (name, module) |
| 460 | return klass |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 461 | |
| 462 | def load_pop(self): |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 463 | del self.stack[-1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 464 | dispatch[POP] = load_pop |
| 465 | |
| 466 | def load_dup(self): |
| 467 | stack.append(stack[-1]) |
| 468 | dispatch[DUP] = load_dup |
| 469 | |
| 470 | def load_get(self): |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 471 | self.stack.append(self.memo[self.readline()[:-1]]) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 472 | dispatch[GET] = load_get |
| 473 | |
| 474 | def load_put(self): |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 475 | self.memo[self.readline()[:-1]] = self.stack[-1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 476 | dispatch[PUT] = load_put |
| 477 | |
| 478 | def load_append(self): |
| 479 | value = self.stack[-1] |
| 480 | del self.stack[-1] |
| 481 | list = self.stack[-1] |
| 482 | list.append(value) |
| 483 | dispatch[APPEND] = load_append |
| 484 | |
| 485 | def load_setitem(self): |
| 486 | value = self.stack[-1] |
| 487 | key = self.stack[-2] |
| 488 | del self.stack[-2:] |
| 489 | dict = self.stack[-1] |
| 490 | dict[key] = value |
| 491 | dispatch[SETITEM] = load_setitem |
| 492 | |
| 493 | def load_build(self): |
| 494 | value = self.stack[-1] |
| 495 | del self.stack[-1] |
| 496 | inst = self.stack[-1] |
| 497 | try: |
| 498 | setstate = inst.__setstate__ |
| 499 | except AttributeError: |
| 500 | for key in value.keys(): |
| 501 | inst.__dict__[key] = value[key] |
| 502 | else: |
| 503 | setstate(value) |
| 504 | dispatch[BUILD] = load_build |
| 505 | |
| 506 | def load_mark(self): |
| 507 | self.stack.append(self.mark) |
| 508 | dispatch[MARK] = load_mark |
| 509 | |
| 510 | def load_stop(self): |
| 511 | value = self.stack[-1] |
| 512 | del self.stack[-1] |
| 513 | raise STOP, value |
| 514 | dispatch[STOP] = load_stop |
| 515 | |
| 516 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 517 | # Shorthands |
| 518 | |
| 519 | def dump(object, file): |
| 520 | Pickler(file).dump(object) |
| 521 | |
| 522 | def dumps(object): |
| 523 | import StringIO |
| 524 | file = StringIO.StringIO() |
| 525 | Pickler(file).dump(object) |
| 526 | return file.getvalue() |
| 527 | |
| 528 | def load(file): |
| 529 | return Unpickler(file).load() |
| 530 | |
| 531 | def loads(str): |
| 532 | import StringIO |
| 533 | file = StringIO.StringIO(str) |
| 534 | return Unpickler(file).load() |
| 535 | |
| 536 | |
| 537 | # The rest is used for testing only |
| 538 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 539 | class C: |
| 540 | def __cmp__(self, other): |
| 541 | return cmp(self.__dict__, other.__dict__) |
| 542 | |
| 543 | def test(): |
| 544 | fn = 'pickle_tmp' |
| 545 | c = C() |
| 546 | c.foo = 1 |
Guido van Rossum | 955c5d1 | 1996-05-15 22:49:57 +0000 | [diff] [blame] | 547 | c.bar = 2L |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 548 | x = [0,1,2,3] |
| 549 | y = ('abc', 'abc', c, c) |
| 550 | x.append(y) |
| 551 | x.append(y) |
| 552 | x.append(5) |
| 553 | f = open(fn, 'w') |
| 554 | F = Pickler(f) |
| 555 | F.dump(x) |
| 556 | f.close() |
| 557 | f = open(fn, 'r') |
| 558 | U = Unpickler(f) |
| 559 | x2 = U.load() |
| 560 | print x |
| 561 | print x2 |
| 562 | print x == x2 |
| 563 | print map(id, x) |
| 564 | print map(id, x2) |
| 565 | print F.memo |
| 566 | print U.memo |
| 567 | |
| 568 | if __name__ == '__main__': |
| 569 | test() |