blob: 8fb534d53f306832ba0d725073d0753c5a0a58b8 [file] [log] [blame]
Guido van Rossumaa3828a1999-03-25 22:38:49 +00001# Test the pickle module
2
3DATA = """(lp0
4I0
5aL1L
6aF2.0
7ac__builtin__
8complex
9p1
10(F3.0
11F0.0
12tp2
13Rp3
14a(S'abc'
15p4
16g4
17(i__main__
18C
19p5
20(dp6
21S'foo'
22p7
23I1
24sS'bar'
25p8
26I2
27sbg5
28tp9
29ag9
30aI5
31a.
32"""
33
34BINDATA = ']q\000(K\000L1L\012G@\000\000\000\000\000\000\000c__builtin__\012complex\012q\001(G@\010\000\000\000\000\000\000G\000\000\000\000\000\000\000\000tq\002Rq\003(U\003abcq\004h\004(c__main__\012C\012q\005oq\006}q\007(U\003fooq\010K\001U\003barq\011K\002ubh\006tq\012h\012K\005e.'
35
36import pickle
37
38class C:
39 def __cmp__(self, other):
40 return cmp(self.__dict__, other.__dict__)
41
42import __main__
43__main__.C = C
44
45def dotest():
46 c = C()
47 c.foo = 1
48 c.bar = 2
49 x = [0, 1L, 2.0, 3.0+0j]
50 y = ('abc', 'abc', c, c)
51 x.append(y)
52 x.append(y)
53 x.append(5)
54 print "dumps()"
55 s = pickle.dumps(x)
56 print "loads()"
57 x2 = pickle.loads(s)
58 if x2 == x: print "ok"
59 else: print "bad"
60 print "loads() DATA"
61 x2 = pickle.loads(DATA)
62 if x2 == x: print "ok"
63 else: print "bad"
64 print "dumps() binary"
65 s = pickle.dumps(x, 1)
66 print "loads() binary"
67 x2 = pickle.loads(s)
68 if x2 == x: print "ok"
69 else: print "bad"
70 print "loads() BINDATA"
71 x2 = pickle.loads(BINDATA)
72 if x2 == x: print "ok"
73 else: print "bad"
74
75dotest()