blob: 128a62742ae32ae2d13f64eed34a4b5082f38419 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Create portable serialized representations of Python objects.
Guido van Rossuma48061a1995-01-10 00:31:14 +00002
Guido van Rossume467be61997-12-05 19:42:42 +00003See module cPickle for a (much) faster implementation.
4See module copy_reg for a mechanism for registering custom picklers.
Guido van Rossuma48061a1995-01-10 00:31:14 +00005
Guido van Rossume467be61997-12-05 19:42:42 +00006Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00007
Guido van Rossume467be61997-12-05 19:42:42 +00008 Pickler
9 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000010
Guido van Rossume467be61997-12-05 19:42:42 +000011Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000012
Guido van Rossume467be61997-12-05 19:42:42 +000013 dump(object, file)
14 dumps(object) -> string
15 load(file) -> object
16 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000017
Guido van Rossume467be61997-12-05 19:42:42 +000018Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000019
Fred Drakefe82acc1998-02-13 03:24:48 +000020 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000021 format_version
22 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000023
Guido van Rossuma48061a1995-01-10 00:31:14 +000024"""
25
Guido van Rossum743d17e1998-09-15 20:25:57 +000026__version__ = "$Revision$" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +000027
28from types import *
Guido van Rossum4fb5b281997-09-12 20:07:24 +000029from copy_reg import dispatch_table, safe_constructors
Guido van Rossumd3703791998-10-22 20:15:36 +000030import string
31import marshal
32import sys
33import struct
Guido van Rossuma48061a1995-01-10 00:31:14 +000034
Guido van Rossumd3703791998-10-22 20:15:36 +000035format_version = "1.3" # File format version we write
36compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000037
38mdumps = marshal.dumps
39mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000040
Guido van Rossum26e89d02000-06-29 16:15:52 +000041class PickleError(Exception): pass
42class PicklingError(PickleError): pass
43class UnpicklingError(PickleError): pass
Guido van Rossum7849da81995-03-09 14:08:35 +000044
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000045try:
46 from org.python.core import PyStringMap
47except ImportError:
48 PyStringMap = None
49
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000050MARK = '('
51STOP = '.'
52POP = '0'
53POP_MARK = '1'
54DUP = '2'
55FLOAT = 'F'
56INT = 'I'
57BININT = 'J'
58BININT1 = 'K'
59LONG = 'L'
60BININT2 = 'M'
61NONE = 'N'
62PERSID = 'P'
63BINPERSID = 'Q'
64REDUCE = 'R'
65STRING = 'S'
66BINSTRING = 'T'
67SHORT_BINSTRING = 'U'
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +000068UNICODE = 'V'
69BINUNICODE = 'X'
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000070APPEND = 'a'
71BUILD = 'b'
72GLOBAL = 'c'
73DICT = 'd'
74EMPTY_DICT = '}'
75APPENDS = 'e'
76GET = 'g'
77BINGET = 'h'
78INST = 'i'
79LONG_BINGET = 'j'
80LIST = 'l'
81EMPTY_LIST = ']'
82OBJ = 'o'
83PUT = 'p'
84BINPUT = 'q'
85LONG_BINPUT = 'r'
86SETITEM = 's'
87TUPLE = 't'
88EMPTY_TUPLE = ')'
89SETITEMS = 'u'
Guido van Rossumd3703791998-10-22 20:15:36 +000090BINFLOAT = 'G'
Guido van Rossuma48061a1995-01-10 00:31:14 +000091
92class Pickler:
93
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000094 def __init__(self, file, bin = 0):
95 self.write = file.write
96 self.memo = {}
97 self.bin = bin
Guido van Rossuma48061a1995-01-10 00:31:14 +000098
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000099 def dump(self, object):
100 self.save(object)
101 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000102
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000103 def put(self, i):
104 if (self.bin):
105 s = mdumps(i)[1:]
106 if (i < 256):
107 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000108
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000109 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000110
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000111 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000113 def get(self, i):
114 if (self.bin):
115 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000116
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000117 if (i < 256):
118 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000119
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000120 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000121
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000122 return GET + `i` + '\n'
123
124 def save(self, object, pers_save = 0):
125 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000126
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000127 if (not pers_save):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000128 pid = self.persistent_id(object)
129 if (pid is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000130 self.save_pers(pid)
131 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000133 d = id(object)
134
135 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000136
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 if ((t is TupleType) and (len(object) == 0)):
138 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000139 self.save_empty_tuple(object)
140 else:
141 self.save_tuple(object)
142 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000143
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000144 if memo.has_key(d):
145 self.write(self.get(memo[d][0]))
146 return
147
148 try:
149 f = self.dispatch[t]
150 except KeyError:
151 pid = self.inst_persistent_id(object)
152 if pid is not None:
153 self.save_pers(pid)
154 return
155
156 try:
157 reduce = dispatch_table[t]
158 except KeyError:
159 try:
160 reduce = object.__reduce__
161 except AttributeError:
162 raise PicklingError, \
Guido van Rossum08a92cb1999-10-10 21:14:25 +0000163 "can't pickle %s object: %s" % (`t.__name__`,
164 `object`)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000165 else:
166 tup = reduce()
167 else:
168 tup = reduce(object)
169
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000170 if type(tup) is StringType:
171 self.save_global(object, tup)
172 return
Guido van Rossumd1f49841997-12-10 23:40:18 +0000173
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000174 if (type(tup) is not TupleType):
175 raise PicklingError, "Value returned by %s must be a " \
176 "tuple" % reduce
177
178 l = len(tup)
179
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000180 if ((l != 2) and (l != 3)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000181 raise PicklingError, "tuple returned by %s must contain " \
182 "only two or three elements" % reduce
183
184 callable = tup[0]
185 arg_tup = tup[1]
186
187 if (l > 2):
188 state = tup[2]
189 else:
190 state = None
191
Guido van Rossumd1f49841997-12-10 23:40:18 +0000192 if type(arg_tup) is not TupleType and arg_tup is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000193 raise PicklingError, "Second element of tuple returned " \
194 "by %s must be a tuple" % reduce
195
196 self.save_reduce(callable, arg_tup, state)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000197 memo_len = len(memo)
198 self.write(self.put(memo_len))
199 memo[d] = (memo_len, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000200 return
201
202 f(self, object)
203
204 def persistent_id(self, object):
205 return None
206
207 def inst_persistent_id(self, object):
208 return None
209
210 def save_pers(self, pid):
211 if (not self.bin):
212 self.write(PERSID + str(pid) + '\n')
213 else:
214 self.save(pid, 1)
215 self.write(BINPERSID)
216
217 def save_reduce(self, callable, arg_tup, state = None):
218 write = self.write
219 save = self.save
220
221 save(callable)
222 save(arg_tup)
223 write(REDUCE)
224
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000225 if (state is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000226 save(state)
227 write(BUILD)
228
229 dispatch = {}
230
231 def save_none(self, object):
232 self.write(NONE)
233 dispatch[NoneType] = save_none
234
235 def save_int(self, object):
236 if (self.bin):
237 i = mdumps(object)[1:]
238 if (i[-2:] == '\000\000'):
239 if (i[-3] == '\000'):
240 self.write(BININT1 + i[:-3])
241 return
242
243 self.write(BININT2 + i[:-2])
244 return
245
246 self.write(BININT + i)
247 else:
248 self.write(INT + `object` + '\n')
249 dispatch[IntType] = save_int
250
251 def save_long(self, object):
252 self.write(LONG + `object` + '\n')
253 dispatch[LongType] = save_long
254
Guido van Rossumd3703791998-10-22 20:15:36 +0000255 def save_float(self, object, pack=struct.pack):
256 if self.bin:
257 self.write(BINFLOAT + pack('>d', object))
258 else:
259 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000260 dispatch[FloatType] = save_float
261
262 def save_string(self, object):
263 d = id(object)
264 memo = self.memo
265
266 if (self.bin):
267 l = len(object)
268 s = mdumps(l)[1:]
269 if (l < 256):
270 self.write(SHORT_BINSTRING + s[0] + object)
271 else:
272 self.write(BINSTRING + s + object)
273 else:
274 self.write(STRING + `object` + '\n')
275
276 memo_len = len(memo)
277 self.write(self.put(memo_len))
278 memo[d] = (memo_len, object)
279 dispatch[StringType] = save_string
280
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000281 def save_unicode(self, object):
282 d = id(object)
283 memo = self.memo
284
285 if (self.bin):
286 encoding = object.encode('utf-8')
287 l = len(encoding)
288 s = mdumps(l)[1:]
289 self.write(BINUNICODE + s + encoding)
290 else:
291 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
292
293 memo_len = len(memo)
294 self.write(self.put(memo_len))
295 memo[d] = (memo_len, object)
296 dispatch[UnicodeType] = save_unicode
297
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000298 def save_tuple(self, object):
299
300 write = self.write
301 save = self.save
302 memo = self.memo
303
304 d = id(object)
305
306 write(MARK)
307
308 for element in object:
309 save(element)
310
311 if (len(object) and memo.has_key(d)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000312 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000313 write(POP_MARK + self.get(memo[d][0]))
314 return
315
Guido van Rossum599174f1998-03-31 16:30:28 +0000316 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000317 return
318
319 memo_len = len(memo)
320 self.write(TUPLE + self.put(memo_len))
321 memo[d] = (memo_len, object)
322 dispatch[TupleType] = save_tuple
323
324 def save_empty_tuple(self, object):
325 self.write(EMPTY_TUPLE)
326
327 def save_list(self, object):
328 d = id(object)
329
330 write = self.write
331 save = self.save
332 memo = self.memo
333
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000334 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335 write(EMPTY_LIST)
336 else:
337 write(MARK + LIST)
338
339 memo_len = len(memo)
340 write(self.put(memo_len))
341 memo[d] = (memo_len, object)
342
343 using_appends = (self.bin and (len(object) > 1))
344
345 if (using_appends):
346 write(MARK)
347
348 for element in object:
349 save(element)
350
351 if (not using_appends):
352 write(APPEND)
353
354 if (using_appends):
355 write(APPENDS)
356 dispatch[ListType] = save_list
357
358 def save_dict(self, object):
359 d = id(object)
360
361 write = self.write
362 save = self.save
363 memo = self.memo
364
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000365 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000366 write(EMPTY_DICT)
367 else:
368 write(MARK + DICT)
369
370 memo_len = len(memo)
371 self.write(self.put(memo_len))
372 memo[d] = (memo_len, object)
373
374 using_setitems = (self.bin and (len(object) > 1))
375
376 if (using_setitems):
377 write(MARK)
378
379 items = object.items()
380 for key, value in items:
381 save(key)
382 save(value)
383
384 if (not using_setitems):
385 write(SETITEM)
386
387 if (using_setitems):
388 write(SETITEMS)
389
390 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000391 if not PyStringMap is None:
392 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000393
394 def save_inst(self, object):
395 d = id(object)
396 cls = object.__class__
397
398 memo = self.memo
399 write = self.write
400 save = self.save
401
402 if hasattr(object, '__getinitargs__'):
403 args = object.__getinitargs__()
404 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000405 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 else:
407 args = ()
408
409 write(MARK)
410
411 if (self.bin):
412 save(cls)
413
414 for arg in args:
415 save(arg)
416
417 memo_len = len(memo)
418 if (self.bin):
419 write(OBJ + self.put(memo_len))
420 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000421 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000422 self.put(memo_len))
423
424 memo[d] = (memo_len, object)
425
426 try:
427 getstate = object.__getstate__
428 except AttributeError:
429 stuff = object.__dict__
430 else:
431 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000432 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000433 save(stuff)
434 write(BUILD)
435 dispatch[InstanceType] = save_inst
436
437 def save_global(self, object, name = None):
438 write = self.write
439 memo = self.memo
440
441 if (name is None):
442 name = object.__name__
443
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000444 try:
445 module = object.__module__
446 except AttributeError:
447 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000448
449 memo_len = len(memo)
450 write(GLOBAL + module + '\n' + name + '\n' +
451 self.put(memo_len))
452 memo[id(object)] = (memo_len, object)
453 dispatch[ClassType] = save_global
454 dispatch[FunctionType] = save_global
455 dispatch[BuiltinFunctionType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000456
Guido van Rossuma48061a1995-01-10 00:31:14 +0000457
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000458def _keep_alive(x, memo):
459 """Keeps a reference to the object x in the memo.
460
461 Because we remember objects by their id, we have
462 to assure that possibly temporary objects are kept
463 alive by referencing them.
464 We store a reference at the id of the memo, which should
465 normally not be used unless someone tries to deepcopy
466 the memo itself...
467 """
468 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000469 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000470 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000471 # aha, this is the first one :-)
472 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000473
474
Guido van Rossuma48061a1995-01-10 00:31:14 +0000475classmap = {}
476
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000477# This is no longer used to find classes, but still for functions
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000478def whichmodule(cls, clsname):
479 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000480
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000481 Search sys.modules for the module.
482 Cache in classmap.
483 Return a module name.
484 If the class cannot be found, return __main__.
485 """
486 if classmap.has_key(cls):
487 return classmap[cls]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000488
489 for name, module in sys.modules.items():
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000490 if name != '__main__' and \
491 hasattr(module, clsname) and \
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000492 getattr(module, clsname) is cls:
493 break
494 else:
495 name = '__main__'
496 classmap[cls] = name
497 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000498
499
500class Unpickler:
501
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000502 def __init__(self, file):
503 self.readline = file.readline
504 self.read = file.read
505 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000506
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000507 def load(self):
508 self.mark = ['spam'] # Any new unique object
509 self.stack = []
510 self.append = self.stack.append
511 read = self.read
512 dispatch = self.dispatch
513 try:
514 while 1:
515 key = read(1)
516 dispatch[key](self)
517 except STOP, value:
518 return value
519
520 def marker(self):
521 stack = self.stack
522 mark = self.mark
523 k = len(stack)-1
524 while stack[k] is not mark: k = k-1
525 return k
526
527 dispatch = {}
528
529 def load_eof(self):
530 raise EOFError
531 dispatch[''] = load_eof
532
533 def load_persid(self):
534 pid = self.readline()[:-1]
535 self.append(self.persistent_load(pid))
536 dispatch[PERSID] = load_persid
537
538 def load_binpersid(self):
539 stack = self.stack
540
541 pid = stack[-1]
542 del stack[-1]
543
544 self.append(self.persistent_load(pid))
545 dispatch[BINPERSID] = load_binpersid
546
547 def load_none(self):
548 self.append(None)
549 dispatch[NONE] = load_none
550
551 def load_int(self):
Guido van Rossume6805461997-12-10 19:36:41 +0000552 self.append(string.atoi(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000553 dispatch[INT] = load_int
554
555 def load_binint(self):
556 self.append(mloads('i' + self.read(4)))
557 dispatch[BININT] = load_binint
558
559 def load_binint1(self):
560 self.append(mloads('i' + self.read(1) + '\000\000\000'))
561 dispatch[BININT1] = load_binint1
562
563 def load_binint2(self):
564 self.append(mloads('i' + self.read(2) + '\000\000'))
565 dispatch[BININT2] = load_binint2
566
567 def load_long(self):
568 self.append(string.atol(self.readline()[:-1], 0))
569 dispatch[LONG] = load_long
570
571 def load_float(self):
572 self.append(string.atof(self.readline()[:-1]))
573 dispatch[FLOAT] = load_float
574
Guido van Rossumd3703791998-10-22 20:15:36 +0000575 def load_binfloat(self, unpack=struct.unpack):
576 self.append(unpack('>d', self.read(8))[0])
577 dispatch[BINFLOAT] = load_binfloat
578
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000579 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000580 rep = self.readline()[:-1]
581 if not self._is_string_secure(rep):
582 raise ValueError, "insecure string pickle"
583 self.append(eval(rep,
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000584 {'__builtins__': {}})) # Let's be careful
585 dispatch[STRING] = load_string
586
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000587 def _is_string_secure(self, s):
588 """Return true if s contains a string that is safe to eval
589
590 The definition of secure string is based on the implementation
591 in cPickle. s is secure as long as it only contains a quoted
592 string and optional trailing whitespace.
593 """
594 q = s[0]
595 if q not in ("'", '"'):
596 return 0
597 # find the closing quote
598 offset = 1
599 i = None
600 while 1:
601 try:
602 i = s.index(q, offset)
603 except ValueError:
604 # if there is an error the first time, there is no
605 # close quote
606 if offset == 1:
607 return 0
608 if s[i-1] != '\\':
609 break
610 # check to see if this one is escaped
611 nslash = 0
612 j = i - 1
613 while j >= offset and s[j] == '\\':
614 j = j - 1
615 nslash = nslash + 1
616 if nslash % 2 == 0:
617 break
618 offset = i + 1
619 for c in s[i+1:]:
620 if ord(c) > 32:
621 return 0
622 return 1
623
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000624 def load_binstring(self):
625 len = mloads('i' + self.read(4))
626 self.append(self.read(len))
627 dispatch[BINSTRING] = load_binstring
628
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000629 def load_unicode(self):
630 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
631 dispatch[UNICODE] = load_unicode
632
633 def load_binunicode(self):
634 len = mloads('i' + self.read(4))
635 self.append(unicode(self.read(len),'utf-8'))
636 dispatch[BINUNICODE] = load_binunicode
637
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000638 def load_short_binstring(self):
639 len = mloads('i' + self.read(1) + '\000\000\000')
640 self.append(self.read(len))
641 dispatch[SHORT_BINSTRING] = load_short_binstring
642
643 def load_tuple(self):
644 k = self.marker()
645 self.stack[k:] = [tuple(self.stack[k+1:])]
646 dispatch[TUPLE] = load_tuple
647
648 def load_empty_tuple(self):
649 self.stack.append(())
650 dispatch[EMPTY_TUPLE] = load_empty_tuple
651
652 def load_empty_list(self):
653 self.stack.append([])
654 dispatch[EMPTY_LIST] = load_empty_list
655
656 def load_empty_dictionary(self):
657 self.stack.append({})
658 dispatch[EMPTY_DICT] = load_empty_dictionary
659
660 def load_list(self):
661 k = self.marker()
662 self.stack[k:] = [self.stack[k+1:]]
663 dispatch[LIST] = load_list
664
665 def load_dict(self):
666 k = self.marker()
667 d = {}
668 items = self.stack[k+1:]
669 for i in range(0, len(items), 2):
670 key = items[i]
671 value = items[i+1]
672 d[key] = value
673 self.stack[k:] = [d]
674 dispatch[DICT] = load_dict
675
676 def load_inst(self):
677 k = self.marker()
678 args = tuple(self.stack[k+1:])
679 del self.stack[k:]
680 module = self.readline()[:-1]
681 name = self.readline()[:-1]
682 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000683 instantiated = 0
684 if (not args and type(klass) is ClassType and
685 not hasattr(klass, "__getinitargs__")):
686 try:
687 value = _EmptyClass()
688 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000689 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000690 except RuntimeError:
691 # In restricted execution, assignment to inst.__class__ is
692 # prohibited
693 pass
694 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000695 try:
696 value = apply(klass, args)
697 except TypeError, err:
698 raise TypeError, "in constructor for %s: %s" % (
699 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000700 self.append(value)
701 dispatch[INST] = load_inst
702
703 def load_obj(self):
704 stack = self.stack
705 k = self.marker()
706 klass = stack[k + 1]
707 del stack[k + 1]
708 args = tuple(stack[k + 1:])
709 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000710 instantiated = 0
711 if (not args and type(klass) is ClassType and
712 not hasattr(klass, "__getinitargs__")):
713 try:
714 value = _EmptyClass()
715 value.__class__ = klass
716 instantiated = 1
717 except RuntimeError:
718 # In restricted execution, assignment to inst.__class__ is
719 # prohibited
720 pass
721 if not instantiated:
722 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000723 self.append(value)
724 dispatch[OBJ] = load_obj
725
726 def load_global(self):
727 module = self.readline()[:-1]
728 name = self.readline()[:-1]
729 klass = self.find_class(module, name)
730 self.append(klass)
731 dispatch[GLOBAL] = load_global
732
733 def find_class(self, module, name):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000734 try:
Guido van Rossum397032a1999-03-25 21:58:59 +0000735 __import__(module)
736 mod = sys.modules[module]
737 klass = getattr(mod, name)
738 except (ImportError, KeyError, AttributeError):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000739 raise SystemError, \
740 "Failed to import class %s from module %s" % \
741 (name, module)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000742 return klass
743
744 def load_reduce(self):
745 stack = self.stack
746
747 callable = stack[-2]
748 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000749 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000750
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000751 if type(callable) is not ClassType:
752 if not safe_constructors.has_key(callable):
753 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000754 safe = callable.__safe_for_unpickling__
755 except AttributeError:
756 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000757
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000758 if (not safe):
759 raise UnpicklingError, "%s is not safe for " \
760 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000761
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000762 if arg_tup is None:
763 value = callable.__basicnew__()
764 else:
765 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000766 self.append(value)
767 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000768
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769 def load_pop(self):
770 del self.stack[-1]
771 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000772
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000773 def load_pop_mark(self):
774 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000775 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000776 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000777
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000778 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000779 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000780 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000781
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 def load_get(self):
783 self.append(self.memo[self.readline()[:-1]])
784 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000785
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000786 def load_binget(self):
787 i = mloads('i' + self.read(1) + '\000\000\000')
788 self.append(self.memo[`i`])
789 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000790
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000791 def load_long_binget(self):
792 i = mloads('i' + self.read(4))
793 self.append(self.memo[`i`])
794 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000795
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000796 def load_put(self):
797 self.memo[self.readline()[:-1]] = self.stack[-1]
798 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000799
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000800 def load_binput(self):
801 i = mloads('i' + self.read(1) + '\000\000\000')
802 self.memo[`i`] = self.stack[-1]
803 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000804
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000805 def load_long_binput(self):
806 i = mloads('i' + self.read(4))
807 self.memo[`i`] = self.stack[-1]
808 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000809
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000810 def load_append(self):
811 stack = self.stack
812 value = stack[-1]
813 del stack[-1]
814 list = stack[-1]
815 list.append(value)
816 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000817
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000818 def load_appends(self):
819 stack = self.stack
820 mark = self.marker()
821 list = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000822 for i in range(mark + 1, len(stack)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000823 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000824
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000825 del stack[mark:]
826 dispatch[APPENDS] = load_appends
827
828 def load_setitem(self):
829 stack = self.stack
830 value = stack[-1]
831 key = stack[-2]
832 del stack[-2:]
833 dict = stack[-1]
834 dict[key] = value
835 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000836
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000837 def load_setitems(self):
838 stack = self.stack
839 mark = self.marker()
840 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000841 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000842 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000843
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000844 del stack[mark:]
845 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000846
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000847 def load_build(self):
848 stack = self.stack
849 value = stack[-1]
850 del stack[-1]
851 inst = stack[-1]
852 try:
853 setstate = inst.__setstate__
854 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000855 try:
856 inst.__dict__.update(value)
857 except RuntimeError:
858 # XXX In restricted execution, the instance's __dict__ is not
859 # accessible. Use the old way of unpickling the instance
860 # variables. This is a semantic different when unpickling in
861 # restricted vs. unrestricted modes.
862 for k, v in value.items():
863 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000864 else:
865 setstate(value)
866 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000867
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000868 def load_mark(self):
869 self.append(self.mark)
870 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000871
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000872 def load_stop(self):
873 value = self.stack[-1]
874 del self.stack[-1]
875 raise STOP, value
876 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000877
Guido van Rossume467be61997-12-05 19:42:42 +0000878# Helper class for load_inst/load_obj
879
880class _EmptyClass:
881 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +0000882
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000883# Shorthands
884
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000885from StringIO import StringIO
886
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000887def dump(object, file, bin = 0):
888 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000889
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000890def dumps(object, bin = 0):
891 file = StringIO()
892 Pickler(file, bin).dump(object)
893 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000894
895def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000896 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000897
898def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000899 file = StringIO(str)
900 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000901
902
903# The rest is used for testing only
904
Guido van Rossuma48061a1995-01-10 00:31:14 +0000905class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000906 def __cmp__(self, other):
907 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000908
909def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000910 fn = 'out'
911 c = C()
912 c.foo = 1
913 c.bar = 2
914 x = [0, 1, 2, 3]
915 y = ('abc', 'abc', c, c)
916 x.append(y)
917 x.append(y)
918 x.append(5)
919 f = open(fn, 'w')
920 F = Pickler(f)
921 F.dump(x)
922 f.close()
923 f = open(fn, 'r')
924 U = Unpickler(f)
925 x2 = U.load()
926 print x
927 print x2
928 print x == x2
929 print map(id, x)
930 print map(id, x2)
931 print F.memo
932 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000933
934if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000935 test()