Guido van Rossum | aa3828a | 1999-03-25 22:38:49 +0000 | [diff] [blame] | 1 | # Test the pickle module |
| 2 | |
| 3 | DATA = """(lp0 |
| 4 | I0 |
| 5 | aL1L |
| 6 | aF2.0 |
| 7 | ac__builtin__ |
| 8 | complex |
| 9 | p1 |
| 10 | (F3.0 |
| 11 | F0.0 |
| 12 | tp2 |
| 13 | Rp3 |
| 14 | a(S'abc' |
| 15 | p4 |
| 16 | g4 |
| 17 | (i__main__ |
| 18 | C |
| 19 | p5 |
| 20 | (dp6 |
| 21 | S'foo' |
| 22 | p7 |
| 23 | I1 |
| 24 | sS'bar' |
| 25 | p8 |
| 26 | I2 |
| 27 | sbg5 |
| 28 | tp9 |
| 29 | ag9 |
| 30 | aI5 |
| 31 | a. |
| 32 | """ |
| 33 | |
| 34 | BINDATA = ']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 | |
| 36 | import pickle |
| 37 | |
| 38 | class C: |
| 39 | def __cmp__(self, other): |
| 40 | return cmp(self.__dict__, other.__dict__) |
| 41 | |
| 42 | import __main__ |
| 43 | __main__.C = C |
| 44 | |
| 45 | def 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 | |
| 75 | dotest() |