blob: dde3dee525f64c0ae03486b379d292e017cd7ee8 [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
59Next, it must normally be possible to create class instances by calling
60the class without arguments. If this is undesirable, the class can
61define a method __getinitargs__ (XXX not a pretty name!), which should
62return a *tuple* containing the arguments to be passed to the class
63constructor.
64
Guido van Rossum0c891ce1995-03-14 15:09:05 +000065Classes can influence how their instances are pickled -- if the class defines
Guido van Rossuma48061a1995-01-10 00:31:14 +000066the method __getstate__, it is called and the return state is pickled
67as the contents for the instance, and if the class defines the
68method __setstate__, it is called with the unpickled state. (Note
69that these methods can also be used to implement copying class instances.)
70If there is no __getstate__ method, the instance's __dict__
71is pickled. If there is no __setstate__ method, the pickled object
72must be a dictionary and its items are assigned to the new instance's
73dictionary. (If a class defines both __getstate__ and __setstate__,
74the state object needn't be a dictionary -- these methods can do what they
75want.)
76
77Note that when class instances are pickled, their class's code and data
78is not pickled along with them. Only the instance data is pickled.
79This is done on purpose, so you can fix bugs in a class or add methods and
80still load objects that were created with an earlier version of the
81class. If you plan to have long-lived objects that will see many versions
82of a class, it may be worth to put a version number in the objects so
83that suitable conversions can be made by the class's __setstate__ method.
84
85The interface is as follows:
86
Guido van Rossum256cbd71995-02-16 16:30:50 +000087To pickle an object x onto a file f, open for writing:
Guido van Rossuma48061a1995-01-10 00:31:14 +000088
89 p = pickle.Pickler(f)
90 p.dump(x)
91
92To unpickle an object x from a file f, open for reading:
93
94 u = pickle.Unpickler(f)
Guido van Rossum48aa82e1995-04-10 11:34:46 +000095 x = u.load()
Guido van Rossuma48061a1995-01-10 00:31:14 +000096
97The Pickler class only calls the method f.write with a string argument
98(XXX possibly the interface should pass f.write instead of f).
99The Unpickler calls the methods f.read(with an integer argument)
100and f.readline(without argument), both returning a string.
101It is explicitly allowed to pass non-file objects here, as long as they
102have the right methods.
103
104The following types can be pickled:
105
106- None
107- integers, long integers, floating point numbers
108- strings
Guido van Rossum256cbd71995-02-16 16:30:50 +0000109- tuples, lists and dictionaries containing only picklable objects
Guido van Rossuma48061a1995-01-10 00:31:14 +0000110- class instances whose __dict__ or __setstate__() is picklable
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000111- classes
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112
113Attempts to pickle unpicklable objects will raise an exception
114after having written an unspecified number of bytes to the file argument.
115
116It is possible to make multiple calls to Pickler.dump() or to
117Unpickler.load(), as long as there is a one-to-one correspondence
Guido van Rossum256cbd71995-02-16 16:30:50 +0000118between pickler and Unpickler objects and between dump and load calls
Guido van Rossuma48061a1995-01-10 00:31:14 +0000119for any pair of corresponding Pickler and Unpicklers. WARNING: this
120is intended for pickleing multiple objects without intervening modifications
121to the objects or their parts. If you modify an object and then pickle
122it again using the same Pickler instance, the object is not pickled
123again -- a reference to it is pickled and the Unpickler will return
124the old value, not the modified one. (XXX There are two problems here:
125(a) detecting changes, and (b) marshalling a minimal set of changes.
126I have no answers. Garbage Collection may also become a problem here.)
127"""
128
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000129__version__ = "1.6" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +0000130
131from types import *
132import string
133
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000134format_version = "1.1" # File format version we write
135compatible_formats = ["1.0"] # Old format versions we can read
136
Guido van Rossum7849da81995-03-09 14:08:35 +0000137PicklingError = "pickle.PicklingError"
138
Guido van Rossuma48061a1995-01-10 00:31:14 +0000139AtomicTypes = [NoneType, IntType, FloatType, StringType]
140
141def safe(object):
142 t = type(object)
143 if t in AtomicTypes:
144 return 1
145 if t is TupleType:
146 for item in object:
147 if not safe(item): return 0
148 return 1
149 return 0
150
151MARK = '('
152POP = '0'
153DUP = '2'
154STOP = '.'
155TUPLE = 't'
156LIST = 'l'
157DICT = 'd'
158INST = 'i'
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000159CLASS = 'c'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000160GET = 'g'
161PUT = 'p'
162APPEND = 'a'
163SETITEM = 's'
164BUILD = 'b'
165NONE = 'N'
166INT = 'I'
167LONG = 'L'
168FLOAT = 'F'
169STRING = 'S'
170PERSID = 'P'
171AtomicKeys = [NONE, INT, LONG, FLOAT, STRING]
172AtomicMap = {
173 NoneType: NONE,
174 IntType: INT,
175 LongType: LONG,
176 FloatType: FLOAT,
177 StringType: STRING,
178}
179
180class Pickler:
181
182 def __init__(self, file):
183 self.write = file.write
184 self.memo = {}
185
186 def dump(self, object):
187 self.save(object)
188 self.write(STOP)
189
190 def save(self, object):
191 pid = self.persistent_id(object)
192 if pid:
193 self.write(PERSID + str(pid) + '\n')
194 return
195 d = id(object)
196 if self.memo.has_key(d):
197 self.write(GET + `d` + '\n')
198 return
199 t = type(object)
Guido van Rossum7849da81995-03-09 14:08:35 +0000200 try:
201 f = self.dispatch[t]
202 except KeyError:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000203 if hasattr(object, '__class__'):
204 f = self.dispatch[InstanceType]
205 else:
206 raise PicklingError, \
207 "can't pickle %s objects" % `t.__name__`
Guido van Rossum7849da81995-03-09 14:08:35 +0000208 f(self, object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000209
210 def persistent_id(self, object):
211 return None
212
213 dispatch = {}
214
215 def save_none(self, object):
216 self.write(NONE)
217 dispatch[NoneType] = save_none
218
219 def save_int(self, object):
220 self.write(INT + `object` + '\n')
221 dispatch[IntType] = save_int
222
223 def save_long(self, object):
224 self.write(LONG + `object` + '\n')
225 dispatch[LongType] = save_long
226
227 def save_float(self, object):
228 self.write(FLOAT + `object` + '\n')
229 dispatch[FloatType] = save_float
230
231 def save_string(self, object):
232 d = id(object)
233 self.write(STRING + `object` + '\n')
234 self.write(PUT + `d` + '\n')
235 self.memo[d] = object
236 dispatch[StringType] = save_string
237
238 def save_tuple(self, object):
239 d = id(object)
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000240 write = self.write
241 save = self.save
242 has_key = self.memo.has_key
243 write(MARK)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000244 n = len(object)
245 for k in range(n):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000246 save(object[k])
247 if has_key(d):
Guido van Rossuma48061a1995-01-10 00:31:14 +0000248 # Saving object[k] has saved us!
249 while k >= 0:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000250 write(POP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000251 k = k-1
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000252 write(GET + `d` + '\n')
Guido van Rossuma48061a1995-01-10 00:31:14 +0000253 break
254 else:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000255 write(TUPLE + PUT + `d` + '\n')
Guido van Rossuma48061a1995-01-10 00:31:14 +0000256 self.memo[d] = object
257 dispatch[TupleType] = save_tuple
258
259 def save_list(self, object):
260 d = id(object)
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000261 write = self.write
262 save = self.save
263 write(MARK)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000264 n = len(object)
265 for k in range(n):
266 item = object[k]
267 if not safe(item):
268 break
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000269 save(item)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000270 else:
271 k = n
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000272 write(LIST + PUT + `d` + '\n')
Guido van Rossuma48061a1995-01-10 00:31:14 +0000273 self.memo[d] = object
274 for k in range(k, n):
275 item = object[k]
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000276 save(item)
277 write(APPEND)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000278 dispatch[ListType] = save_list
279
280 def save_dict(self, object):
281 d = id(object)
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000282 write = self.write
283 save = self.save
284 write(MARK)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000285 items = object.items()
286 n = len(items)
287 for k in range(n):
288 key, value = items[k]
289 if not safe(key) or not safe(value):
290 break
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000291 save(key)
292 save(value)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000293 else:
294 k = n
295 self.write(DICT + PUT + `d` + '\n')
296 self.memo[d] = object
297 for k in range(k, n):
298 key, value = items[k]
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000299 save(key)
300 save(value)
301 write(SETITEM)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000302 dispatch[DictionaryType] = save_dict
303
304 def save_inst(self, object):
305 d = id(object)
306 cls = object.__class__
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000307 write = self.write
308 save = self.save
Guido van Rossuma48061a1995-01-10 00:31:14 +0000309 module = whichmodule(cls)
310 name = cls.__name__
311 if hasattr(object, '__getinitargs__'):
312 args = object.__getinitargs__()
313 len(args) # XXX Assert it's a sequence
314 else:
315 args = ()
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000316 write(MARK)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000317 for arg in args:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000318 save(arg)
319 write(INST + module + '\n' + name + '\n' +
320 PUT + `d` + '\n')
Guido van Rossuma48061a1995-01-10 00:31:14 +0000321 self.memo[d] = object
322 try:
323 getstate = object.__getstate__
324 except AttributeError:
325 stuff = object.__dict__
326 else:
327 stuff = getstate()
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000328 save(stuff)
329 write(BUILD)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000330 dispatch[InstanceType] = save_inst
331
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000332 def save_class(self, object):
333 d = id(object)
334 module = whichmodule(object)
335 name = object.__name__
336 self.write(CLASS + module + '\n' + name + '\n' +
337 PUT + `d` + '\n')
338 dispatch[ClassType] = save_class
339
Guido van Rossuma48061a1995-01-10 00:31:14 +0000340
341classmap = {}
342
343def whichmodule(cls):
344 """Figure out the module in which a class occurs.
345
346 Search sys.modules for the module.
347 Cache in classmap.
348 Return a module name.
349 If the class cannot be found, return __main__.
350 """
351 if classmap.has_key(cls):
352 return classmap[cls]
353 import sys
354 clsname = cls.__name__
355 for name, module in sys.modules.items():
Guido van Rossumf71c79b1995-06-22 18:51:23 +0000356 if name != '__main__' and \
Guido van Rossuma48061a1995-01-10 00:31:14 +0000357 hasattr(module, clsname) and \
358 getattr(module, clsname) is cls:
359 break
360 else:
361 name = '__main__'
362 classmap[cls] = name
363 return name
364
365
366class Unpickler:
367
368 def __init__(self, file):
369 self.readline = file.readline
370 self.read = file.read
371 self.memo = {}
372
373 def load(self):
374 self.mark = ['spam'] # Any new unique object
375 self.stack = []
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000376 self.append = self.stack.append
377 read = self.read
378 dispatch = self.dispatch
Guido van Rossuma48061a1995-01-10 00:31:14 +0000379 try:
380 while 1:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000381 key = read(1)
382 dispatch[key](self)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000383 except STOP, value:
384 return value
385
386 def marker(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000387 stack = self.stack
388 mark = self.mark
389 k = len(stack)-1
390 while stack[k] is not mark: k = k-1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000391 return k
392
393 dispatch = {}
394
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000395 def load_eof(self):
396 raise EOFError
397 dispatch[''] = load_eof
398
Guido van Rossuma48061a1995-01-10 00:31:14 +0000399 def load_persid(self):
400 pid = self.readline()[:-1]
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000401 self.append(self.persistent_load(pid))
Guido van Rossuma48061a1995-01-10 00:31:14 +0000402 dispatch[PERSID] = load_persid
403
404 def load_none(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000405 self.append(None)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000406 dispatch[NONE] = load_none
407
Guido van Rossum78536471996-04-12 13:36:27 +0000408 def load_int(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000409 self.append(string.atoi(self.readline()[:-1], 0))
Guido van Rossum78536471996-04-12 13:36:27 +0000410 dispatch[INT] = load_int
411
412 def load_long(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000413 self.append(string.atol(self.readline()[:-1], 0))
Guido van Rossum78536471996-04-12 13:36:27 +0000414 dispatch[LONG] = load_long
415
416 def load_float(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000417 self.append(string.atof(self.readline()[:-1]))
Guido van Rossum78536471996-04-12 13:36:27 +0000418 dispatch[FLOAT] = load_float
419
420 def load_string(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000421 self.append(eval(self.readline()[:-1],
422 {'__builtins__': {}})) # Let's be careful
Guido van Rossum78536471996-04-12 13:36:27 +0000423 dispatch[STRING] = load_string
Guido van Rossuma48061a1995-01-10 00:31:14 +0000424
425 def load_tuple(self):
426 k = self.marker()
427 self.stack[k:] = [tuple(self.stack[k+1:])]
428 dispatch[TUPLE] = load_tuple
429
430 def load_list(self):
431 k = self.marker()
432 self.stack[k:] = [self.stack[k+1:]]
433 dispatch[LIST] = load_list
434
435 def load_dict(self):
436 k = self.marker()
437 d = {}
438 items = self.stack[k+1:]
439 for i in range(0, len(items), 2):
440 key = items[i]
441 value = items[i+1]
442 d[key] = value
443 self.stack[k:] = [d]
444 dispatch[DICT] = load_dict
445
446 def load_inst(self):
447 k = self.marker()
448 args = tuple(self.stack[k+1:])
449 del self.stack[k:]
450 module = self.readline()[:-1]
451 name = self.readline()[:-1]
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000452 klass = self.find_class(module, name)
453 value = apply(klass, args)
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000454 self.append(value)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000455 dispatch[INST] = load_inst
456
457 def load_class(self):
458 module = self.readline()[:-1]
459 name = self.readline()[:-1]
460 klass = self.find_class(module, name)
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000461 self.append(klass)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000462 return klass
463 dispatch[CLASS] = load_class
464
465 def find_class(self, module, name):
Guido van Rossuma48061a1995-01-10 00:31:14 +0000466 env = {}
467 try:
468 exec 'from %s import %s' % (module, name) in env
469 except ImportError:
470 raise SystemError, \
471 "Failed to import class %s from module %s" % \
472 (name, module)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000473 klass = env[name]
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000474 # if type(klass) != ClassType:
475 if (type(klass) is FunctionType or
476 type(klass) is BuiltinFunctionType):
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000477 raise SystemError, \
478 "Imported object %s from module %s is not a class" % \
479 (name, module)
480 return klass
Guido van Rossuma48061a1995-01-10 00:31:14 +0000481
482 def load_pop(self):
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000483 del self.stack[-1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000484 dispatch[POP] = load_pop
485
486 def load_dup(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000487 self.append(stack[-1])
Guido van Rossuma48061a1995-01-10 00:31:14 +0000488 dispatch[DUP] = load_dup
489
490 def load_get(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000491 self.append(self.memo[self.readline()[:-1]])
Guido van Rossuma48061a1995-01-10 00:31:14 +0000492 dispatch[GET] = load_get
493
494 def load_put(self):
Guido van Rossum78536471996-04-12 13:36:27 +0000495 self.memo[self.readline()[:-1]] = self.stack[-1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000496 dispatch[PUT] = load_put
497
498 def load_append(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000499 stack = self.stack
500 value = stack[-1]
501 del stack[-1]
502 list = stack[-1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000503 list.append(value)
504 dispatch[APPEND] = load_append
505
506 def load_setitem(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000507 stack = self.stack
508 value = stack[-1]
509 key = stack[-2]
510 del stack[-2:]
511 dict = stack[-1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000512 dict[key] = value
513 dispatch[SETITEM] = load_setitem
514
515 def load_build(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000516 stack = self.stack
517 value = stack[-1]
518 del stack[-1]
519 inst = stack[-1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000520 try:
521 setstate = inst.__setstate__
522 except AttributeError:
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000523 instdict = inst.__dict__
Guido van Rossuma48061a1995-01-10 00:31:14 +0000524 for key in value.keys():
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000525 instdict[key] = value[key]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000526 else:
527 setstate(value)
528 dispatch[BUILD] = load_build
529
530 def load_mark(self):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000531 self.append(self.mark)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000532 dispatch[MARK] = load_mark
533
534 def load_stop(self):
535 value = self.stack[-1]
536 del self.stack[-1]
537 raise STOP, value
538 dispatch[STOP] = load_stop
539
540
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000541# Shorthands
542
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000543from StringIO import StringIO
544
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000545def dump(object, file):
546 Pickler(file).dump(object)
547
548def dumps(object):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000549 file = StringIO()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000550 Pickler(file).dump(object)
551 return file.getvalue()
552
553def load(file):
554 return Unpickler(file).load()
555
556def loads(str):
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000557 file = StringIO(str)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000558 return Unpickler(file).load()
559
560
561# The rest is used for testing only
562
Guido van Rossuma48061a1995-01-10 00:31:14 +0000563class C:
564 def __cmp__(self, other):
565 return cmp(self.__dict__, other.__dict__)
566
567def test():
568 fn = 'pickle_tmp'
569 c = C()
570 c.foo = 1
Guido van Rossum955c5d11996-05-15 22:49:57 +0000571 c.bar = 2L
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000572 x = [0, 1, 2, 3]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000573 y = ('abc', 'abc', c, c)
574 x.append(y)
575 x.append(y)
576 x.append(5)
577 f = open(fn, 'w')
578 F = Pickler(f)
579 F.dump(x)
580 f.close()
581 f = open(fn, 'r')
582 U = Unpickler(f)
583 x2 = U.load()
584 print x
585 print x2
586 print x == x2
587 print map(id, x)
588 print map(id, x2)
589 print F.memo
590 print U.memo
591
592if __name__ == '__main__':
593 test()