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 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 3 | from test_support import TestFailed, have_unicode |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 4 | import sys |
| 5 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 6 | # break into multiple strings to please font-lock-mode |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 7 | DATA = """(lp1 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 8 | I0 |
| 9 | aL1L |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 10 | aF2 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 11 | ac__builtin__ |
| 12 | complex |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 13 | p2 |
| 14 | """ + \ |
| 15 | """(F3 |
| 16 | F0 |
| 17 | tRp3 |
| 18 | aI1 |
| 19 | aI-1 |
| 20 | aI255 |
| 21 | aI-255 |
| 22 | aI-256 |
| 23 | aI65535 |
| 24 | aI-65535 |
| 25 | aI-65536 |
| 26 | aI2147483647 |
| 27 | aI-2147483647 |
| 28 | aI-2147483648 |
| 29 | a""" + \ |
| 30 | """(S'abc' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 31 | p4 |
| 32 | g4 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 33 | """ + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 34 | """(i__main__ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 35 | C |
| 36 | p5 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 37 | """ + \ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 38 | """(dp6 |
| 39 | S'foo' |
| 40 | p7 |
| 41 | I1 |
| 42 | sS'bar' |
| 43 | p8 |
| 44 | I2 |
| 45 | sbg5 |
| 46 | tp9 |
| 47 | ag9 |
| 48 | aI5 |
| 49 | a. |
| 50 | """ |
| 51 | |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 52 | BINDATA = ']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' + \ |
| 53 | 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' + \ |
| 54 | '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' + \ |
| 55 | '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' + \ |
| 56 | 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 57 | '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' + \ |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 58 | 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' + \ |
| 59 | '\x06tq\nh\nK\x05e.' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 60 | |
| 61 | class C: |
| 62 | def __cmp__(self, other): |
| 63 | return cmp(self.__dict__, other.__dict__) |
| 64 | |
| 65 | import __main__ |
| 66 | __main__.C = C |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 67 | C.__module__ = "__main__" |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 68 | |
| 69 | # Call this with the module to be tested (pickle or cPickle). |
| 70 | |
| 71 | def dotest(pickle): |
| 72 | c = C() |
| 73 | c.foo = 1 |
| 74 | c.bar = 2 |
| 75 | x = [0, 1L, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 76 | # Append some integer test cases at cPickle.c's internal size |
| 77 | # cutoffs. |
| 78 | uint1max = 0xff |
| 79 | uint2max = 0xffff |
| 80 | int4max = 0x7fffffff |
| 81 | x.extend([1, -1, |
| 82 | uint1max, -uint1max, -uint1max-1, |
| 83 | uint2max, -uint2max, -uint2max-1, |
| 84 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 85 | y = ('abc', 'abc', c, c) |
| 86 | x.append(y) |
| 87 | x.append(y) |
| 88 | x.append(5) |
| 89 | r = [] |
| 90 | r.append(r) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 91 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 92 | print "dumps()" |
| 93 | s = pickle.dumps(x) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 94 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 95 | print "loads()" |
| 96 | x2 = pickle.loads(s) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 97 | if x2 == x: |
| 98 | print "ok" |
| 99 | else: |
| 100 | print "bad" |
| 101 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 102 | print "loads() DATA" |
| 103 | x2 = pickle.loads(DATA) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 104 | if x2 == x: |
| 105 | print "ok" |
| 106 | else: |
| 107 | print "bad" |
| 108 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 109 | print "dumps() binary" |
| 110 | s = pickle.dumps(x, 1) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 111 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 112 | print "loads() binary" |
| 113 | x2 = pickle.loads(s) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 114 | if x2 == x: |
| 115 | print "ok" |
| 116 | else: |
| 117 | print "bad" |
| 118 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 119 | print "loads() BINDATA" |
| 120 | x2 = pickle.loads(BINDATA) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 121 | if x2 == x: |
| 122 | print "ok" |
| 123 | else: |
| 124 | print "bad" |
| 125 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 126 | print "dumps() RECURSIVE" |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 127 | s = pickle.dumps(r) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 128 | x2 = pickle.loads(s) |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 129 | if x2 == r: |
| 130 | print "ok" |
| 131 | else: |
| 132 | print "bad" |
| 133 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 134 | # don't create cyclic garbage |
| 135 | del x2[0] |
| 136 | del r[0] |
| 137 | |
| 138 | # Test protection against closed files |
| 139 | import tempfile, os |
| 140 | fn = tempfile.mktemp() |
| 141 | f = open(fn, "w") |
| 142 | f.close() |
| 143 | try: |
| 144 | pickle.dump(123, f) |
| 145 | except ValueError: |
| 146 | pass |
| 147 | else: |
| 148 | print "dump to closed file should raise ValueError" |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 149 | |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 150 | f = open(fn, "r") |
| 151 | f.close() |
| 152 | try: |
| 153 | pickle.load(f) |
| 154 | except ValueError: |
| 155 | pass |
| 156 | else: |
| 157 | print "load from closed file should raise ValueError" |
| 158 | os.remove(fn) |
| 159 | |
| 160 | # Test specific bad cases |
| 161 | for i in range(10): |
| 162 | try: |
| 163 | x = pickle.loads('garyp') |
| 164 | except KeyError, y: |
| 165 | # pickle |
| 166 | del y |
| 167 | except pickle.BadPickleGet, y: |
| 168 | # cPickle |
| 169 | del y |
| 170 | else: |
| 171 | print "unexpected success!" |
| 172 | break |
| 173 | |
| 174 | # Test insecure strings |
| 175 | insecure = ["abc", "2 + 2", # not quoted |
| 176 | "'abc' + 'def'", # not a single quoted string |
| 177 | "'abc", # quote is not closed |
| 178 | "'abc\"", # open quote and close quote don't match |
| 179 | "'abc' ?", # junk after close quote |
| 180 | # some tests of the quoting rules |
| 181 | "'abc\"\''", |
| 182 | "'\\\\a\'\'\'\\\'\\\\\''", |
| 183 | ] |
| 184 | for s in insecure: |
| 185 | buf = "S" + s + "\012p0\012." |
| 186 | try: |
| 187 | x = pickle.loads(buf) |
| 188 | except ValueError: |
| 189 | pass |
| 190 | else: |
| 191 | print "accepted insecure string: %s" % repr(buf) |
| 192 | |
| 193 | # Test some Unicode end cases |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 194 | if have_unicode: |
| 195 | endcases = [unicode(''), unicode('<\\u>'), unicode('<\\\u1234>'), |
| 196 | unicode('<\n>'), unicode('<\\>')] |
| 197 | else: |
| 198 | endcases = [] |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 199 | for u in endcases: |
| 200 | try: |
| 201 | u2 = pickle.loads(pickle.dumps(u)) |
| 202 | except Exception, msg: |
| 203 | print "Endcase exception: %s => %s(%s)" % \ |
| 204 | (`u`, msg.__class__.__name__, str(msg)) |
| 205 | else: |
| 206 | if u2 != u: |
| 207 | print "Endcase failure: %s => %s" % (`u`, `u2`) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 208 | |
| 209 | # Test the full range of Python ints. |
| 210 | n = sys.maxint |
| 211 | while n: |
| 212 | for expected in (-n, n): |
| 213 | for binary_mode in (0, 1): |
| 214 | s = pickle.dumps(expected, binary_mode) |
| 215 | got = pickle.loads(s) |
| 216 | if expected != got: |
| 217 | raise TestFailed("for %s-mode pickle of %d, pickle " |
| 218 | "string is %s, loaded back as %s" % ( |
| 219 | binary_mode and "binary" or "text", |
| 220 | expected, |
| 221 | repr(s), |
| 222 | got)) |
| 223 | n = n >> 1 |