blob: 29557e368558aa2af04cd579beb047bfb9186bb7 [file] [log] [blame]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001"""\
2Pickling Algorithm
3------------------
4
5This module implements a basic but powerful algorithm for "pickling" (a.k.a.
6serializing, marshalling or flattening) nearly arbitrary Python objects.
7This is a more primitive notion than persistency -- although pickle
8reads and writes file objects, it does not handle the issue of naming
9persistent objects, nor the (even more complicated) area of concurrent
10access to persistent objects. The pickle module can transform a complex
11object into a byte stream and it can transform the byte stream into
12an object with the same internal structure. The most obvious thing to
13do with these byte streams is to write them onto a file, but it is also
14conceivable to send them across a network or store them in a database.
15
16Unlike the built-in marshal module, pickle handles the following correctly:
17
18- recursive objects
19- pointer sharing
Guido van Rossum0c891ce1995-03-14 15:09:05 +000020- classes and class instances
Guido van Rossuma48061a1995-01-10 00:31:14 +000021
22Pickle is Python-specific. This has the advantage that there are no
23restrictions imposed by external standards such as CORBA (which probably
24can't represent pointer sharing or recursive objects); however it means
25that non-Python programs may not be able to reconstruct pickled Python
26objects.
27
28Pickle uses a printable ASCII representation. This is slightly more
29voluminous than a binary representation. However, small integers actually
30take *less* space when represented as minimal-size decimal strings than
31when represented as 32-bit binary numbers, and strings are only much longer
32if they contain control characters or 8-bit characters. The big advantage
33of using printable ASCII (and of some other characteristics of pickle's
34representation) is that for debugging or recovery purposes it is possible
35for a human to read the pickled file with a standard text editor. (I could
36have gone a step further and used a notation like S-expressions, but the
37parser would have been considerably more complicated and slower, and the
38files would probably have become much larger.)
39
40Pickle doesn't handle code objects, which marshal does.
41I suppose pickle could, and maybe it should, but there's probably no
42great need for it right now (as long as marshal continues to be used
43for reading and writing code objects), and at least this avoids
44the possibility of smuggling Trojan horses into a program.
45
46For the benefit of persistency modules written using pickle, it supports
47the notion of a reference to an object outside the pickled data stream.
48Such objects are referenced by a name, which is an arbitrary string of
49printable ASCII characters. The resolution of such names is not defined
50by the pickle module -- the persistent object module will have to implement
51a method "persistent_load". To write references to persistent objects,
52the persistent module must define a method "persistent_id" which returns
53either None or the persistent ID of the object.
54
55There are some restrictions on the pickling of class instances.
56
57First of all, the class must be defined at the top level in a module.
58
Guido van Rossum37a6f161996-08-08 18:35:22 +000059Next, it must normally be possible to create class instances by
60calling the class without arguments. Usually, this is best
61accomplished by providing default values for all arguments to its
62__init__ method (if it has one). If this is undesirable, the
63class can define a method __getinitargs__, which should return a
64*tuple* containing the arguments to be passed to the class
Guido van Rossuma48061a1995-01-10 00:31:14 +000065constructor.
66
Guido van Rossum0c891ce1995-03-14 15:09:05 +000067Classes can influence how their instances are pickled -- if the class defines
Guido van Rossuma48061a1995-01-10 00:31:14 +000068the method __getstate__, it is called and the return state is pickled
69as the contents for the instance, and if the class defines the
70method __setstate__, it is called with the unpickled state. (Note
71that these methods can also be used to implement copying class instances.)
72If there is no __getstate__ method, the instance's __dict__
73is pickled. If there is no __setstate__ method, the pickled object
74must be a dictionary and its items are assigned to the new instance's
75dictionary. (If a class defines both __getstate__ and __setstate__,
76the state object needn't be a dictionary -- these methods can do what they
77want.)
78
79Note that when class instances are pickled, their class's code and data
80is not pickled along with them. Only the instance data is pickled.
81This is done on purpose, so you can fix bugs in a class or add methods and
82still load objects that were created with an earlier version of the
83class. If you plan to have long-lived objects that will see many versions
84of a class, it may be worth to put a version number in the objects so
85that suitable conversions can be made by the class's __setstate__ method.
86
87The interface is as follows:
88
Guido van Rossum256cbd71995-02-16 16:30:50 +000089To pickle an object x onto a file f, open for writing:
Guido van Rossuma48061a1995-01-10 00:31:14 +000090
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000091 p = pickle.Pickler(f)
92 p.dump(x)
Guido van Rossuma48061a1995-01-10 00:31:14 +000093
94To unpickle an object x from a file f, open for reading:
95
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000096 u = pickle.Unpickler(f)
97 x = u.load()
Guido van Rossuma48061a1995-01-10 00:31:14 +000098
99The Pickler class only calls the method f.write with a string argument
100(XXX possibly the interface should pass f.write instead of f).
101The Unpickler calls the methods f.read(with an integer argument)
102and f.readline(without argument), both returning a string.
103It is explicitly allowed to pass non-file objects here, as long as they
104have the right methods.
105
106The following types can be pickled:
107
108- None
109- integers, long integers, floating point numbers
110- strings
Guido van Rossum256cbd71995-02-16 16:30:50 +0000111- tuples, lists and dictionaries containing only picklable objects
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112- class instances whose __dict__ or __setstate__() is picklable
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000113- classes
Guido van Rossuma48061a1995-01-10 00:31:14 +0000114
115Attempts to pickle unpicklable objects will raise an exception
116after having written an unspecified number of bytes to the file argument.
117
118It is possible to make multiple calls to Pickler.dump() or to
119Unpickler.load(), as long as there is a one-to-one correspondence
Guido van Rossum256cbd71995-02-16 16:30:50 +0000120between pickler and Unpickler objects and between dump and load calls
Guido van Rossuma48061a1995-01-10 00:31:14 +0000121for any pair of corresponding Pickler and Unpicklers. WARNING: this
122is intended for pickleing multiple objects without intervening modifications
123to the objects or their parts. If you modify an object and then pickle
124it again using the same Pickler instance, the object is not pickled
125again -- a reference to it is pickled and the Unpickler will return
126the old value, not the modified one. (XXX There are two problems here:
127(a) detecting changes, and (b) marshalling a minimal set of changes.
128I have no answers. Garbage Collection may also become a problem here.)
129"""
130
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000131__version__ = "1.7" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
133from types import *
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000134from copy_reg import *
135import string, marshal
Guido van Rossuma48061a1995-01-10 00:31:14 +0000136
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000137format_version = "1.2" # File format version we write
138compatible_formats = ["1.0", "1.1"] # Old format versions we can read
139
140mdumps = marshal.dumps
141mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000142
Guido van Rossum7849da81995-03-09 14:08:35 +0000143PicklingError = "pickle.PicklingError"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000144UnpicklingError = "pickle.UnpicklingError"
Guido van Rossum7849da81995-03-09 14:08:35 +0000145
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000146MARK = '('
147STOP = '.'
148POP = '0'
149POP_MARK = '1'
150DUP = '2'
151FLOAT = 'F'
152INT = 'I'
153BININT = 'J'
154BININT1 = 'K'
155LONG = 'L'
156BININT2 = 'M'
157NONE = 'N'
158PERSID = 'P'
159BINPERSID = 'Q'
160REDUCE = 'R'
161STRING = 'S'
162BINSTRING = 'T'
163SHORT_BINSTRING = 'U'
164APPEND = 'a'
165BUILD = 'b'
166GLOBAL = 'c'
167DICT = 'd'
168EMPTY_DICT = '}'
169APPENDS = 'e'
170GET = 'g'
171BINGET = 'h'
172INST = 'i'
173LONG_BINGET = 'j'
174LIST = 'l'
175EMPTY_LIST = ']'
176OBJ = 'o'
177PUT = 'p'
178BINPUT = 'q'
179LONG_BINPUT = 'r'
180SETITEM = 's'
181TUPLE = 't'
182EMPTY_TUPLE = ')'
183SETITEMS = 'u'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000184
185class Pickler:
186
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000187 def __init__(self, file, bin = 0):
188 self.write = file.write
189 self.memo = {}
190 self.bin = bin
Guido van Rossuma48061a1995-01-10 00:31:14 +0000191
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000192 def dump(self, object):
193 self.save(object)
194 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000195
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000196 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 Rossuma48061a1995-01-10 00:31:14 +0000200
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000201 self.save_reduce(callable, args, state)
202 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000203
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000204 def put(self, i):
205 if (self.bin):
206 s = mdumps(i)[1:]
207 if (i < 256):
208 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000209
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000210 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000211
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000212 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000213
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000214 def get(self, i):
215 if (self.bin):
216 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000217
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000218 if (i < 256):
219 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000220
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000221 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000222
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000223 return GET + `i` + '\n'
224
225 def save(self, object, pers_save = 0):
226 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000227
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000228 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 Rossuma48061a1995-01-10 00:31:14 +0000233
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000234 d = id(object)
235
236 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000238 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 Rossuma48061a1995-01-10 00:31:14 +0000244
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000245 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 Rossum0c891ce1995-03-14 15:09:05 +0000524
Guido van Rossuma48061a1995-01-10 00:31:14 +0000525
526classmap = {}
527
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000528def whichmodule(cls, clsname):
529 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000530
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000531 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():
541 if hasattr(module, clsname) and \
542 getattr(module, clsname) is cls:
543 break
544 else:
545 name = '__main__'
546 classmap[cls] = name
547 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000548
549
550class Unpickler:
551
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000552 def __init__(self, file):
553 self.readline = file.readline
554 self.read = file.read
555 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000556
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000557 def load(self):
558 self.mark = ['spam'] # Any new unique object
559 self.stack = []
560 self.append = self.stack.append
561 read = self.read
562 dispatch = self.dispatch
563 try:
564 while 1:
565 key = read(1)
566 dispatch[key](self)
567 except STOP, value:
568 return value
569
570 def marker(self):
571 stack = self.stack
572 mark = self.mark
573 k = len(stack)-1
574 while stack[k] is not mark: k = k-1
575 return k
576
577 dispatch = {}
578
579 def load_eof(self):
580 raise EOFError
581 dispatch[''] = load_eof
582
583 def load_persid(self):
584 pid = self.readline()[:-1]
585 self.append(self.persistent_load(pid))
586 dispatch[PERSID] = load_persid
587
588 def load_binpersid(self):
589 stack = self.stack
590
591 pid = stack[-1]
592 del stack[-1]
593
594 self.append(self.persistent_load(pid))
595 dispatch[BINPERSID] = load_binpersid
596
597 def load_none(self):
598 self.append(None)
599 dispatch[NONE] = load_none
600
601 def load_int(self):
602 self.append(string.atoi(self.readline()[:-1], 0))
603 dispatch[INT] = load_int
604
605 def load_binint(self):
606 self.append(mloads('i' + self.read(4)))
607 dispatch[BININT] = load_binint
608
609 def load_binint1(self):
610 self.append(mloads('i' + self.read(1) + '\000\000\000'))
611 dispatch[BININT1] = load_binint1
612
613 def load_binint2(self):
614 self.append(mloads('i' + self.read(2) + '\000\000'))
615 dispatch[BININT2] = load_binint2
616
617 def load_long(self):
618 self.append(string.atol(self.readline()[:-1], 0))
619 dispatch[LONG] = load_long
620
621 def load_float(self):
622 self.append(string.atof(self.readline()[:-1]))
623 dispatch[FLOAT] = load_float
624
625 def load_string(self):
626 self.append(eval(self.readline()[:-1],
627 {'__builtins__': {}})) # Let's be careful
628 dispatch[STRING] = load_string
629
630 def load_binstring(self):
631 len = mloads('i' + self.read(4))
632 self.append(self.read(len))
633 dispatch[BINSTRING] = load_binstring
634
635 def load_short_binstring(self):
636 len = mloads('i' + self.read(1) + '\000\000\000')
637 self.append(self.read(len))
638 dispatch[SHORT_BINSTRING] = load_short_binstring
639
640 def load_tuple(self):
641 k = self.marker()
642 self.stack[k:] = [tuple(self.stack[k+1:])]
643 dispatch[TUPLE] = load_tuple
644
645 def load_empty_tuple(self):
646 self.stack.append(())
647 dispatch[EMPTY_TUPLE] = load_empty_tuple
648
649 def load_empty_list(self):
650 self.stack.append([])
651 dispatch[EMPTY_LIST] = load_empty_list
652
653 def load_empty_dictionary(self):
654 self.stack.append({})
655 dispatch[EMPTY_DICT] = load_empty_dictionary
656
657 def load_list(self):
658 k = self.marker()
659 self.stack[k:] = [self.stack[k+1:]]
660 dispatch[LIST] = load_list
661
662 def load_dict(self):
663 k = self.marker()
664 d = {}
665 items = self.stack[k+1:]
666 for i in range(0, len(items), 2):
667 key = items[i]
668 value = items[i+1]
669 d[key] = value
670 self.stack[k:] = [d]
671 dispatch[DICT] = load_dict
672
673 def load_inst(self):
674 k = self.marker()
675 args = tuple(self.stack[k+1:])
676 del self.stack[k:]
677 module = self.readline()[:-1]
678 name = self.readline()[:-1]
679 klass = self.find_class(module, name)
680 if (type(klass) is not ClassType):
681 raise SystemError, "Imported object %s from module %s is " \
682 "not a class" % (name, module)
683
684 value = apply(klass, args)
685 self.append(value)
686 dispatch[INST] = load_inst
687
688 def load_obj(self):
689 stack = self.stack
690 k = self.marker()
691 klass = stack[k + 1]
692 del stack[k + 1]
693 args = tuple(stack[k + 1:])
694 del stack[k:]
695 value = apply(klass, args)
696 self.append(value)
697 dispatch[OBJ] = load_obj
698
699 def load_global(self):
700 module = self.readline()[:-1]
701 name = self.readline()[:-1]
702 klass = self.find_class(module, name)
703 self.append(klass)
704 dispatch[GLOBAL] = load_global
705
706 def find_class(self, module, name):
707 env = {}
708
709 try:
710 exec 'from %s import %s' % (module, name) in env
711 except ImportError:
712 raise SystemError, \
713 "Failed to import class %s from module %s" % \
714 (name, module)
715 klass = env[name]
716 return klass
717
718 def load_reduce(self):
719 stack = self.stack
720
721 callable = stack[-2]
722 arg_tup = stack[-1]
723 del stack[-2:]
724
725 if (type(callable) is not ClassType):
726 if (not safe_constructors.has_key(callable)):
Guido van Rossuma48061a1995-01-10 00:31:14 +0000727 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000728 safe = callable.__safe_for_unpickling__
729 except AttributeError:
730 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000731
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000732 if (not safe):
733 raise UnpicklingError, "%s is not safe for " \
734 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000735
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000736 value = apply(callable, arg_tup)
737 self.append(value)
738 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000739
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000740 def load_pop(self):
741 del self.stack[-1]
742 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000743
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000744 def load_pop_mark(self):
745 k = self.marker()
746 del self.stack[k:]
747 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000748
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000749 def load_dup(self):
750 self.append(stack[-1])
751 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000752
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000753 def load_get(self):
754 self.append(self.memo[self.readline()[:-1]])
755 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000756
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000757 def load_binget(self):
758 i = mloads('i' + self.read(1) + '\000\000\000')
759 self.append(self.memo[`i`])
760 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000761
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000762 def load_long_binget(self):
763 i = mloads('i' + self.read(4))
764 self.append(self.memo[`i`])
765 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000766
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000767 def load_put(self):
768 self.memo[self.readline()[:-1]] = self.stack[-1]
769 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000770
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000771 def load_binput(self):
772 i = mloads('i' + self.read(1) + '\000\000\000')
773 self.memo[`i`] = self.stack[-1]
774 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000775
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000776 def load_long_binput(self):
777 i = mloads('i' + self.read(4))
778 self.memo[`i`] = self.stack[-1]
779 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000780
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000781 def load_append(self):
782 stack = self.stack
783 value = stack[-1]
784 del stack[-1]
785 list = stack[-1]
786 list.append(value)
787 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000788
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000789 def load_appends(self):
790 stack = self.stack
791 mark = self.marker()
792 list = stack[mark - 1]
793 for i in range(mark + 1, len(stack)):
794 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000795
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000796 del stack[mark:]
797 dispatch[APPENDS] = load_appends
798
799 def load_setitem(self):
800 stack = self.stack
801 value = stack[-1]
802 key = stack[-2]
803 del stack[-2:]
804 dict = stack[-1]
805 dict[key] = value
806 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000807
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000808 def load_setitems(self):
809 stack = self.stack
810 mark = self.marker()
811 dict = stack[mark - 1]
812 for i in range(mark + 1, len(stack), 2):
813 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000814
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000815 del stack[mark:]
816 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000817
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000818 def load_build(self):
819 stack = self.stack
820 value = stack[-1]
821 del stack[-1]
822 inst = stack[-1]
823 try:
824 setstate = inst.__setstate__
825 except AttributeError:
826 for key in value.keys():
827 setattr(inst, key, value[key])
828 else:
829 setstate(value)
830 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000831
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000832 def load_mark(self):
833 self.append(self.mark)
834 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000835
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000836 def load_stop(self):
837 value = self.stack[-1]
838 del self.stack[-1]
839 raise STOP, value
840 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000841
842
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000843# Shorthands
844
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000845from StringIO import StringIO
846
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000847def dump(object, file, bin = 0):
848 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000849
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000850def dumps(object, bin = 0):
851 file = StringIO()
852 Pickler(file, bin).dump(object)
853 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000854
855def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000856 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000857
858def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000859 file = StringIO(str)
860 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000861
862
863# The rest is used for testing only
864
Guido van Rossuma48061a1995-01-10 00:31:14 +0000865class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000866 def __cmp__(self, other):
867 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000868
869def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000870 fn = 'out'
871 c = C()
872 c.foo = 1
873 c.bar = 2
874 x = [0, 1, 2, 3]
875 y = ('abc', 'abc', c, c)
876 x.append(y)
877 x.append(y)
878 x.append(5)
879 f = open(fn, 'w')
880 F = Pickler(f)
881 F.dump(x)
882 f.close()
883 f = open(fn, 'r')
884 U = Unpickler(f)
885 x2 = U.load()
886 print x
887 print x2
888 print x == x2
889 print map(id, x)
890 print map(id, x2)
891 print F.memo
892 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000893
894if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000895 test()