Tim Peters | 763cb0a | 2001-01-22 22:43:35 +0000 | [diff] [blame^] | 1 | # test_pickle and test_cpickle both use this. |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 2 | |
| 3 | # break into multiple strings to please font-lock-mode |
| 4 | DATA = """(lp0 |
| 5 | I0 |
| 6 | aL1L |
| 7 | aF2.0 |
| 8 | ac__builtin__ |
| 9 | complex |
| 10 | p1 |
| 11 | """ \ |
| 12 | """(F3.0 |
| 13 | F0.0 |
| 14 | tp2 |
| 15 | Rp3 |
| 16 | a(S'abc' |
| 17 | p4 |
| 18 | g4 |
| 19 | """ \ |
| 20 | """(i__main__ |
| 21 | C |
| 22 | p5 |
| 23 | """ \ |
| 24 | """(dp6 |
| 25 | S'foo' |
| 26 | p7 |
| 27 | I1 |
| 28 | sS'bar' |
| 29 | p8 |
| 30 | I2 |
| 31 | sbg5 |
| 32 | tp9 |
| 33 | ag9 |
| 34 | aI5 |
| 35 | a. |
| 36 | """ |
| 37 | |
| 38 | 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.' |
| 39 | |
| 40 | class C: |
| 41 | def __cmp__(self, other): |
| 42 | return cmp(self.__dict__, other.__dict__) |
| 43 | |
| 44 | import __main__ |
| 45 | __main__.C = C |
| 46 | |
| 47 | # Call this with the module to be tested (pickle or cPickle). |
| 48 | |
| 49 | def dotest(pickle): |
| 50 | c = C() |
| 51 | c.foo = 1 |
| 52 | c.bar = 2 |
| 53 | x = [0, 1L, 2.0, 3.0+0j] |
| 54 | y = ('abc', 'abc', c, c) |
| 55 | x.append(y) |
| 56 | x.append(y) |
| 57 | x.append(5) |
| 58 | r = [] |
| 59 | r.append(r) |
| 60 | print "dumps()" |
| 61 | s = pickle.dumps(x) |
| 62 | print "loads()" |
| 63 | x2 = pickle.loads(s) |
| 64 | if x2 == x: print "ok" |
| 65 | else: print "bad" |
| 66 | print "loads() DATA" |
| 67 | x2 = pickle.loads(DATA) |
| 68 | if x2 == x: print "ok" |
| 69 | else: print "bad" |
| 70 | print "dumps() binary" |
| 71 | s = pickle.dumps(x, 1) |
| 72 | print "loads() binary" |
| 73 | x2 = pickle.loads(s) |
| 74 | if x2 == x: print "ok" |
| 75 | else: print "bad" |
| 76 | print "loads() BINDATA" |
| 77 | x2 = pickle.loads(BINDATA) |
| 78 | if x2 == x: print "ok" |
| 79 | else: print "bad" |
| 80 | s = pickle.dumps(r) |
| 81 | print "dumps() RECURSIVE" |
| 82 | x2 = pickle.loads(s) |
| 83 | if x2 == r: print "ok" |
| 84 | else: print "bad" |
| 85 | # don't create cyclic garbage |
| 86 | del x2[0] |
| 87 | del r[0] |
| 88 | |
| 89 | # Test protection against closed files |
| 90 | import tempfile, os |
| 91 | fn = tempfile.mktemp() |
| 92 | f = open(fn, "w") |
| 93 | f.close() |
| 94 | try: |
| 95 | pickle.dump(123, f) |
| 96 | except ValueError: |
| 97 | pass |
| 98 | else: |
| 99 | print "dump to closed file should raise ValueError" |
| 100 | f = open(fn, "r") |
| 101 | f.close() |
| 102 | try: |
| 103 | pickle.load(f) |
| 104 | except ValueError: |
| 105 | pass |
| 106 | else: |
| 107 | print "load from closed file should raise ValueError" |
| 108 | os.remove(fn) |
| 109 | |
| 110 | # Test specific bad cases |
| 111 | for i in range(10): |
| 112 | try: |
| 113 | x = pickle.loads('garyp') |
| 114 | except KeyError, y: |
| 115 | # pickle |
| 116 | del y |
| 117 | except pickle.BadPickleGet, y: |
| 118 | # cPickle |
| 119 | del y |
| 120 | else: |
| 121 | print "unexpected success!" |
| 122 | break |
| 123 | |
| 124 | # Test insecure strings |
| 125 | insecure = ["abc", "2 + 2", # not quoted |
| 126 | "'abc' + 'def'", # not a single quoted string |
| 127 | "'abc", # quote is not closed |
| 128 | "'abc\"", # open quote and close quote don't match |
| 129 | "'abc' ?", # junk after close quote |
| 130 | # some tests of the quoting rules |
| 131 | "'abc\"\''", |
| 132 | "'\\\\a\'\'\'\\\'\\\\\''", |
| 133 | ] |
| 134 | for s in insecure: |
| 135 | buf = "S" + s + "\012p0\012." |
| 136 | try: |
| 137 | x = pickle.loads(buf) |
| 138 | except ValueError: |
| 139 | pass |
| 140 | else: |
| 141 | print "accepted insecure string: %s" % repr(buf) |
| 142 | |
| 143 | # Test some Unicode end cases |
| 144 | endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>', u'<\\>'] |
| 145 | for u in endcases: |
| 146 | try: |
| 147 | u2 = pickle.loads(pickle.dumps(u)) |
| 148 | except Exception, msg: |
| 149 | print "Endcase exception: %s => %s(%s)" % \ |
| 150 | (`u`, msg.__class__.__name__, str(msg)) |
| 151 | else: |
| 152 | if u2 != u: |
| 153 | print "Endcase failure: %s => %s" % (`u`, `u2`) |