blob: a38f4f69924c9ba43ecc7dc201fff056730df224 [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 Rossum8be9a111997-04-25 19:52:29 +0000131__version__ = "1.8" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
133from types import *
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000134from copy_reg import dispatch_table, safe_constructors
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000135import 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)
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000293 memo_len = len(memo)
294 self.write(self.put(memo_len))
295 memo[d] = (memo_len, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000296 return
297
298 f(self, object)
299
300 def persistent_id(self, object):
301 return None
302
303 def inst_persistent_id(self, object):
304 return None
305
306 def save_pers(self, pid):
307 if (not self.bin):
308 self.write(PERSID + str(pid) + '\n')
309 else:
310 self.save(pid, 1)
311 self.write(BINPERSID)
312
313 def save_reduce(self, callable, arg_tup, state = None):
314 write = self.write
315 save = self.save
316
317 save(callable)
318 save(arg_tup)
319 write(REDUCE)
320
321 if (state is not None):
322 save(state)
323 write(BUILD)
324
325 dispatch = {}
326
327 def save_none(self, object):
328 self.write(NONE)
329 dispatch[NoneType] = save_none
330
331 def save_int(self, object):
332 if (self.bin):
333 i = mdumps(object)[1:]
334 if (i[-2:] == '\000\000'):
335 if (i[-3] == '\000'):
336 self.write(BININT1 + i[:-3])
337 return
338
339 self.write(BININT2 + i[:-2])
340 return
341
342 self.write(BININT + i)
343 else:
344 self.write(INT + `object` + '\n')
345 dispatch[IntType] = save_int
346
347 def save_long(self, object):
348 self.write(LONG + `object` + '\n')
349 dispatch[LongType] = save_long
350
351 def save_float(self, object):
352 self.write(FLOAT + `object` + '\n')
353 dispatch[FloatType] = save_float
354
355 def save_string(self, object):
356 d = id(object)
357 memo = self.memo
358
359 if (self.bin):
360 l = len(object)
361 s = mdumps(l)[1:]
362 if (l < 256):
363 self.write(SHORT_BINSTRING + s[0] + object)
364 else:
365 self.write(BINSTRING + s + object)
366 else:
367 self.write(STRING + `object` + '\n')
368
369 memo_len = len(memo)
370 self.write(self.put(memo_len))
371 memo[d] = (memo_len, object)
372 dispatch[StringType] = save_string
373
374 def save_tuple(self, object):
375
376 write = self.write
377 save = self.save
378 memo = self.memo
379
380 d = id(object)
381
382 write(MARK)
383
384 for element in object:
385 save(element)
386
387 if (len(object) and memo.has_key(d)):
388 if (self.bin):
389 write(POP_MARK + self.get(memo[d][0]))
390 return
391
392 write(POP * (len(object) + 1) + self.get(mem[d][0]))
393 return
394
395 memo_len = len(memo)
396 self.write(TUPLE + self.put(memo_len))
397 memo[d] = (memo_len, object)
398 dispatch[TupleType] = save_tuple
399
400 def save_empty_tuple(self, object):
401 self.write(EMPTY_TUPLE)
402
403 def save_list(self, object):
404 d = id(object)
405
406 write = self.write
407 save = self.save
408 memo = self.memo
409
410 if (self.bin):
411 write(EMPTY_LIST)
412 else:
413 write(MARK + LIST)
414
415 memo_len = len(memo)
416 write(self.put(memo_len))
417 memo[d] = (memo_len, object)
418
419 using_appends = (self.bin and (len(object) > 1))
420
421 if (using_appends):
422 write(MARK)
423
424 for element in object:
425 save(element)
426
427 if (not using_appends):
428 write(APPEND)
429
430 if (using_appends):
431 write(APPENDS)
432 dispatch[ListType] = save_list
433
434 def save_dict(self, object):
435 d = id(object)
436
437 write = self.write
438 save = self.save
439 memo = self.memo
440
441 if (self.bin):
442 write(EMPTY_DICT)
443 else:
444 write(MARK + DICT)
445
446 memo_len = len(memo)
447 self.write(self.put(memo_len))
448 memo[d] = (memo_len, object)
449
450 using_setitems = (self.bin and (len(object) > 1))
451
452 if (using_setitems):
453 write(MARK)
454
455 items = object.items()
456 for key, value in items:
457 save(key)
458 save(value)
459
460 if (not using_setitems):
461 write(SETITEM)
462
463 if (using_setitems):
464 write(SETITEMS)
465
466 dispatch[DictionaryType] = save_dict
467
468 def save_inst(self, object):
469 d = id(object)
470 cls = object.__class__
471
472 memo = self.memo
473 write = self.write
474 save = self.save
475
476 if hasattr(object, '__getinitargs__'):
477 args = object.__getinitargs__()
478 len(args) # XXX Assert it's a sequence
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000479 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000480 else:
481 args = ()
482
483 write(MARK)
484
485 if (self.bin):
486 save(cls)
487
488 for arg in args:
489 save(arg)
490
491 memo_len = len(memo)
492 if (self.bin):
493 write(OBJ + self.put(memo_len))
494 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000495 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000496 self.put(memo_len))
497
498 memo[d] = (memo_len, object)
499
500 try:
501 getstate = object.__getstate__
502 except AttributeError:
503 stuff = object.__dict__
504 else:
505 stuff = getstate()
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000506 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000507 save(stuff)
508 write(BUILD)
509 dispatch[InstanceType] = save_inst
510
511 def save_global(self, object, name = None):
512 write = self.write
513 memo = self.memo
514
515 if (name is None):
516 name = object.__name__
517
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000518 try:
519 module = object.__module__
520 except AttributeError:
521 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000522
523 memo_len = len(memo)
524 write(GLOBAL + module + '\n' + name + '\n' +
525 self.put(memo_len))
526 memo[id(object)] = (memo_len, object)
527 dispatch[ClassType] = save_global
528 dispatch[FunctionType] = save_global
529 dispatch[BuiltinFunctionType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000530
Guido van Rossuma48061a1995-01-10 00:31:14 +0000531
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000532def _keep_alive(x, memo):
533 """Keeps a reference to the object x in the memo.
534
535 Because we remember objects by their id, we have
536 to assure that possibly temporary objects are kept
537 alive by referencing them.
538 We store a reference at the id of the memo, which should
539 normally not be used unless someone tries to deepcopy
540 the memo itself...
541 """
542 try:
543 memo[id(memo)].append(x)
544 except KeyError:
545 # aha, this is the first one :-)
546 memo[id(memo)]=[x]
547
548
Guido van Rossuma48061a1995-01-10 00:31:14 +0000549classmap = {}
550
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000551# This is no longer used to find classes, but still for functions
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000552def whichmodule(cls, clsname):
553 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000554
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000555 Search sys.modules for the module.
556 Cache in classmap.
557 Return a module name.
558 If the class cannot be found, return __main__.
559 """
560 if classmap.has_key(cls):
561 return classmap[cls]
562 import sys
563
564 for name, module in sys.modules.items():
Guido van Rossum8be9a111997-04-25 19:52:29 +0000565 if name != '__main__' and \
566 hasattr(module, clsname) and \
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000567 getattr(module, clsname) is cls:
568 break
569 else:
570 name = '__main__'
571 classmap[cls] = name
572 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000573
574
575class Unpickler:
576
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000577 def __init__(self, file):
578 self.readline = file.readline
579 self.read = file.read
580 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000581
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000582 def load(self):
583 self.mark = ['spam'] # Any new unique object
584 self.stack = []
585 self.append = self.stack.append
586 read = self.read
587 dispatch = self.dispatch
588 try:
589 while 1:
590 key = read(1)
591 dispatch[key](self)
592 except STOP, value:
593 return value
594
595 def marker(self):
596 stack = self.stack
597 mark = self.mark
598 k = len(stack)-1
599 while stack[k] is not mark: k = k-1
600 return k
601
602 dispatch = {}
603
604 def load_eof(self):
605 raise EOFError
606 dispatch[''] = load_eof
607
608 def load_persid(self):
609 pid = self.readline()[:-1]
610 self.append(self.persistent_load(pid))
611 dispatch[PERSID] = load_persid
612
613 def load_binpersid(self):
614 stack = self.stack
615
616 pid = stack[-1]
617 del stack[-1]
618
619 self.append(self.persistent_load(pid))
620 dispatch[BINPERSID] = load_binpersid
621
622 def load_none(self):
623 self.append(None)
624 dispatch[NONE] = load_none
625
626 def load_int(self):
627 self.append(string.atoi(self.readline()[:-1], 0))
628 dispatch[INT] = load_int
629
630 def load_binint(self):
631 self.append(mloads('i' + self.read(4)))
632 dispatch[BININT] = load_binint
633
634 def load_binint1(self):
635 self.append(mloads('i' + self.read(1) + '\000\000\000'))
636 dispatch[BININT1] = load_binint1
637
638 def load_binint2(self):
639 self.append(mloads('i' + self.read(2) + '\000\000'))
640 dispatch[BININT2] = load_binint2
641
642 def load_long(self):
643 self.append(string.atol(self.readline()[:-1], 0))
644 dispatch[LONG] = load_long
645
646 def load_float(self):
647 self.append(string.atof(self.readline()[:-1]))
648 dispatch[FLOAT] = load_float
649
650 def load_string(self):
651 self.append(eval(self.readline()[:-1],
652 {'__builtins__': {}})) # Let's be careful
653 dispatch[STRING] = load_string
654
655 def load_binstring(self):
656 len = mloads('i' + self.read(4))
657 self.append(self.read(len))
658 dispatch[BINSTRING] = load_binstring
659
660 def load_short_binstring(self):
661 len = mloads('i' + self.read(1) + '\000\000\000')
662 self.append(self.read(len))
663 dispatch[SHORT_BINSTRING] = load_short_binstring
664
665 def load_tuple(self):
666 k = self.marker()
667 self.stack[k:] = [tuple(self.stack[k+1:])]
668 dispatch[TUPLE] = load_tuple
669
670 def load_empty_tuple(self):
671 self.stack.append(())
672 dispatch[EMPTY_TUPLE] = load_empty_tuple
673
674 def load_empty_list(self):
675 self.stack.append([])
676 dispatch[EMPTY_LIST] = load_empty_list
677
678 def load_empty_dictionary(self):
679 self.stack.append({})
680 dispatch[EMPTY_DICT] = load_empty_dictionary
681
682 def load_list(self):
683 k = self.marker()
684 self.stack[k:] = [self.stack[k+1:]]
685 dispatch[LIST] = load_list
686
687 def load_dict(self):
688 k = self.marker()
689 d = {}
690 items = self.stack[k+1:]
691 for i in range(0, len(items), 2):
692 key = items[i]
693 value = items[i+1]
694 d[key] = value
695 self.stack[k:] = [d]
696 dispatch[DICT] = load_dict
697
698 def load_inst(self):
699 k = self.marker()
700 args = tuple(self.stack[k+1:])
701 del self.stack[k:]
702 module = self.readline()[:-1]
703 name = self.readline()[:-1]
704 klass = self.find_class(module, name)
Guido van Rossum8be9a111997-04-25 19:52:29 +0000705## if (type(klass) is not ClassType):
706## raise SystemError, "Imported object %s from module %s is " \
707## "not a class" % (name, module)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000708
709 value = apply(klass, args)
710 self.append(value)
711 dispatch[INST] = load_inst
712
713 def load_obj(self):
714 stack = self.stack
715 k = self.marker()
716 klass = stack[k + 1]
717 del stack[k + 1]
718 args = tuple(stack[k + 1:])
719 del stack[k:]
720 value = apply(klass, args)
721 self.append(value)
722 dispatch[OBJ] = load_obj
723
724 def load_global(self):
725 module = self.readline()[:-1]
726 name = self.readline()[:-1]
727 klass = self.find_class(module, name)
728 self.append(klass)
729 dispatch[GLOBAL] = load_global
730
731 def find_class(self, module, name):
732 env = {}
733
734 try:
735 exec 'from %s import %s' % (module, name) in env
736 except ImportError:
737 raise SystemError, \
738 "Failed to import class %s from module %s" % \
739 (name, module)
740 klass = env[name]
741 return klass
742
743 def load_reduce(self):
744 stack = self.stack
745
746 callable = stack[-2]
747 arg_tup = stack[-1]
748 del stack[-2:]
749
750 if (type(callable) is not ClassType):
751 if (not safe_constructors.has_key(callable)):
Guido van Rossuma48061a1995-01-10 00:31:14 +0000752 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000753 safe = callable.__safe_for_unpickling__
754 except AttributeError:
755 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000756
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000757 if (not safe):
758 raise UnpicklingError, "%s is not safe for " \
759 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000760
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000761 value = apply(callable, arg_tup)
762 self.append(value)
763 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000764
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 def load_pop(self):
766 del self.stack[-1]
767 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000768
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769 def load_pop_mark(self):
770 k = self.marker()
771 del self.stack[k:]
772 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000773
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000774 def load_dup(self):
775 self.append(stack[-1])
776 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000777
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000778 def load_get(self):
779 self.append(self.memo[self.readline()[:-1]])
780 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000781
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 def load_binget(self):
783 i = mloads('i' + self.read(1) + '\000\000\000')
784 self.append(self.memo[`i`])
785 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000786
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000787 def load_long_binget(self):
788 i = mloads('i' + self.read(4))
789 self.append(self.memo[`i`])
790 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000791
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000792 def load_put(self):
793 self.memo[self.readline()[:-1]] = self.stack[-1]
794 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000795
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000796 def load_binput(self):
797 i = mloads('i' + self.read(1) + '\000\000\000')
798 self.memo[`i`] = self.stack[-1]
799 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000800
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000801 def load_long_binput(self):
802 i = mloads('i' + self.read(4))
803 self.memo[`i`] = self.stack[-1]
804 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000805
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000806 def load_append(self):
807 stack = self.stack
808 value = stack[-1]
809 del stack[-1]
810 list = stack[-1]
811 list.append(value)
812 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000813
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000814 def load_appends(self):
815 stack = self.stack
816 mark = self.marker()
817 list = stack[mark - 1]
818 for i in range(mark + 1, len(stack)):
819 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000820
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000821 del stack[mark:]
822 dispatch[APPENDS] = load_appends
823
824 def load_setitem(self):
825 stack = self.stack
826 value = stack[-1]
827 key = stack[-2]
828 del stack[-2:]
829 dict = stack[-1]
830 dict[key] = value
831 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000832
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000833 def load_setitems(self):
834 stack = self.stack
835 mark = self.marker()
836 dict = stack[mark - 1]
837 for i in range(mark + 1, len(stack), 2):
838 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000839
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000840 del stack[mark:]
841 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000842
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000843 def load_build(self):
844 stack = self.stack
845 value = stack[-1]
846 del stack[-1]
847 inst = stack[-1]
848 try:
849 setstate = inst.__setstate__
850 except AttributeError:
Guido van Rossumd6ead321997-09-08 02:08:11 +0000851 inst.__dict__.update(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000852 else:
853 setstate(value)
854 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000855
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000856 def load_mark(self):
857 self.append(self.mark)
858 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000859
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000860 def load_stop(self):
861 value = self.stack[-1]
862 del self.stack[-1]
863 raise STOP, value
864 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000865
866
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000867# Shorthands
868
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000869from StringIO import StringIO
870
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000871def dump(object, file, bin = 0):
872 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000873
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000874def dumps(object, bin = 0):
875 file = StringIO()
876 Pickler(file, bin).dump(object)
877 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000878
879def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000880 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000881
882def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000883 file = StringIO(str)
884 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000885
886
887# The rest is used for testing only
888
Guido van Rossuma48061a1995-01-10 00:31:14 +0000889class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000890 def __cmp__(self, other):
891 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000892
893def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000894 fn = 'out'
895 c = C()
896 c.foo = 1
897 c.bar = 2
898 x = [0, 1, 2, 3]
899 y = ('abc', 'abc', c, c)
900 x.append(y)
901 x.append(y)
902 x.append(5)
903 f = open(fn, 'w')
904 F = Pickler(f)
905 F.dump(x)
906 f.close()
907 f = open(fn, 'r')
908 U = Unpickler(f)
909 x2 = U.load()
910 print x
911 print x2
912 print x == x2
913 print map(id, x)
914 print map(id, x2)
915 print F.memo
916 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000917
918if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000919 test()