Guido van Rossum | aa3828a | 1999-03-25 22:38:49 +0000 | [diff] [blame] | 1 | # Test the cPickle 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 cPickle |
| 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 = cPickle.dumps(x) |
| 56 | print "loads()" |
| 57 | x2 = cPickle.loads(s) |
| 58 | if x2 == x: print "ok" |
| 59 | else: print "bad" |
| 60 | print "loads() DATA" |
| 61 | x2 = cPickle.loads(DATA) |
| 62 | if x2 == x: print "ok" |
| 63 | else: print "bad" |
| 64 | print "dumps() binary" |
| 65 | s = cPickle.dumps(x, 1) |
| 66 | print "loads() binary" |
| 67 | x2 = cPickle.loads(s) |
| 68 | if x2 == x: print "ok" |
| 69 | else: print "bad" |
| 70 | print "loads() BINDATA" |
| 71 | x2 = cPickle.loads(BINDATA) |
| 72 | if x2 == x: print "ok" |
| 73 | else: print "bad" |
| 74 | |
Guido van Rossum | 89ae2b9 | 1999-03-29 19:59:32 +0000 | [diff] [blame] | 75 | # Test protection against closed files |
| 76 | import tempfile, os |
| 77 | fn = tempfile.mktemp() |
| 78 | f = open(fn, "w") |
| 79 | f.close() |
| 80 | try: |
| 81 | cPickle.dump(123, f) |
Guido van Rossum | 84219682 | 2000-04-21 20:49:58 +0000 | [diff] [blame] | 82 | except ValueError: |
Guido van Rossum | 89ae2b9 | 1999-03-29 19:59:32 +0000 | [diff] [blame] | 83 | pass |
| 84 | else: |
Guido van Rossum | 84219682 | 2000-04-21 20:49:58 +0000 | [diff] [blame] | 85 | print "dump to closed file should raise ValueError" |
Guido van Rossum | 89ae2b9 | 1999-03-29 19:59:32 +0000 | [diff] [blame] | 86 | f = open(fn, "r") |
| 87 | f.close() |
| 88 | try: |
| 89 | cPickle.load(f) |
Guido van Rossum | 84219682 | 2000-04-21 20:49:58 +0000 | [diff] [blame] | 90 | except ValueError: |
Guido van Rossum | 89ae2b9 | 1999-03-29 19:59:32 +0000 | [diff] [blame] | 91 | pass |
| 92 | else: |
Guido van Rossum | 84219682 | 2000-04-21 20:49:58 +0000 | [diff] [blame] | 93 | print "load from closed file should raise ValueError" |
Guido van Rossum | 8ff764f | 1999-04-01 15:32:30 +0000 | [diff] [blame] | 94 | os.remove(fn) |
Guido van Rossum | 89ae2b9 | 1999-03-29 19:59:32 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | ba508a2 | 1999-07-13 15:23:42 +0000 | [diff] [blame] | 96 | # Test specific bad cases |
| 97 | for i in range(10): |
| 98 | try: |
| 99 | x = cPickle.loads('garyp') |
| 100 | except cPickle.BadPickleGet, y: |
| 101 | del y |
| 102 | else: |
| 103 | print "unexpected success!" |
| 104 | break |
| 105 | |
| 106 | |
Guido van Rossum | aa3828a | 1999-03-25 22:38:49 +0000 | [diff] [blame] | 107 | dotest() |