Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1 | import unittest |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2 | from test_support import TestFailed, have_unicode |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 3 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 4 | class C: |
| 5 | def __cmp__(self, other): |
| 6 | return cmp(self.__dict__, other.__dict__) |
| 7 | |
| 8 | import __main__ |
| 9 | __main__.C = C |
| 10 | C.__module__ = "__main__" |
| 11 | |
| 12 | class myint(int): |
| 13 | def __init__(self, x): |
| 14 | self.str = str(x) |
| 15 | |
| 16 | class initarg(C): |
| 17 | def __init__(self, a, b): |
| 18 | self.a = a |
| 19 | self.b = b |
| 20 | |
| 21 | def __getinitargs__(self): |
| 22 | return self.a, self.b |
| 23 | |
| 24 | # break into multiple strings to avoid confusing font-lock-mode |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 25 | DATA = """(lp1 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 26 | I0 |
| 27 | aL1L |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 28 | aF2 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 29 | ac__builtin__ |
| 30 | complex |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 31 | p2 |
| 32 | """ + \ |
| 33 | """(F3 |
| 34 | F0 |
| 35 | tRp3 |
| 36 | aI1 |
| 37 | aI-1 |
| 38 | aI255 |
| 39 | aI-255 |
| 40 | aI-256 |
| 41 | aI65535 |
| 42 | aI-65535 |
| 43 | aI-65536 |
| 44 | aI2147483647 |
| 45 | aI-2147483647 |
| 46 | aI-2147483648 |
| 47 | a""" + \ |
| 48 | """(S'abc' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 49 | p4 |
| 50 | g4 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 51 | """ + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 52 | """(i__main__ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 53 | C |
| 54 | p5 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 55 | """ + \ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 56 | """(dp6 |
| 57 | S'foo' |
| 58 | p7 |
| 59 | I1 |
| 60 | sS'bar' |
| 61 | p8 |
| 62 | I2 |
| 63 | sbg5 |
| 64 | tp9 |
| 65 | ag9 |
| 66 | aI5 |
| 67 | a. |
| 68 | """ |
| 69 | |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 70 | BINDATA = ']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' + \ |
| 71 | 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' + \ |
| 72 | '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' + \ |
| 73 | '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' + \ |
| 74 | '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] | 75 | '\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] | 76 | 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' + \ |
| 77 | '\x06tq\nh\nK\x05e.' |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 78 | |
| 79 | def create_data(): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 80 | c = C() |
| 81 | c.foo = 1 |
| 82 | c.bar = 2 |
| 83 | x = [0, 1L, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 84 | # Append some integer test cases at cPickle.c's internal size |
| 85 | # cutoffs. |
| 86 | uint1max = 0xff |
| 87 | uint2max = 0xffff |
| 88 | int4max = 0x7fffffff |
| 89 | x.extend([1, -1, |
| 90 | uint1max, -uint1max, -uint1max-1, |
| 91 | uint2max, -uint2max, -uint2max-1, |
| 92 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 93 | y = ('abc', 'abc', c, c) |
| 94 | x.append(y) |
| 95 | x.append(y) |
| 96 | x.append(5) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 97 | return x |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 98 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 99 | class AbstractPickleTests(unittest.TestCase): |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 100 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 101 | _testdata = create_data() |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 102 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 103 | def setUp(self): |
| 104 | # subclass must define self.dumps, self.loads, self.error |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 105 | pass |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 106 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 107 | def test_misc(self): |
| 108 | # test various datatypes not tested by testdata |
| 109 | x = myint(4) |
| 110 | s = self.dumps(x) |
| 111 | y = self.loads(s) |
| 112 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 114 | x = (1, ()) |
| 115 | s = self.dumps(x) |
| 116 | y = self.loads(s) |
| 117 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 118 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 119 | x = initarg(1, x) |
| 120 | s = self.dumps(x) |
| 121 | y = self.loads(s) |
| 122 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 123 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 124 | # XXX test __reduce__ protocol? |
| 125 | |
| 126 | def test_identity(self): |
| 127 | s = self.dumps(self._testdata) |
| 128 | x = self.loads(s) |
| 129 | self.assertEqual(x, self._testdata) |
| 130 | |
| 131 | def test_constant(self): |
| 132 | x = self.loads(DATA) |
| 133 | self.assertEqual(x, self._testdata) |
| 134 | x = self.loads(BINDATA) |
| 135 | self.assertEqual(x, self._testdata) |
| 136 | |
| 137 | def test_binary(self): |
| 138 | s = self.dumps(self._testdata, 1) |
| 139 | x = self.loads(s) |
| 140 | self.assertEqual(x, self._testdata) |
| 141 | |
| 142 | def test_recursive_list(self): |
| 143 | l = [] |
| 144 | l.append(l) |
| 145 | s = self.dumps(l) |
| 146 | x = self.loads(s) |
| 147 | self.assertEqual(x, l) |
| 148 | self.assertEqual(x, x[0]) |
| 149 | self.assertEqual(id(x), id(x[0])) |
| 150 | |
| 151 | def test_recursive_dict(self): |
| 152 | d = {} |
| 153 | d[1] = d |
| 154 | s = self.dumps(d) |
| 155 | x = self.loads(s) |
| 156 | self.assertEqual(x, d) |
| 157 | self.assertEqual(x[1], x) |
| 158 | self.assertEqual(id(x[1]), id(x)) |
| 159 | |
| 160 | def test_recursive_inst(self): |
| 161 | i = C() |
| 162 | i.attr = i |
| 163 | s = self.dumps(i) |
| 164 | x = self.loads(s) |
| 165 | self.assertEqual(x, i) |
| 166 | self.assertEqual(x.attr, x) |
| 167 | self.assertEqual(id(x.attr), id(x)) |
| 168 | |
| 169 | def test_recursive_multi(self): |
| 170 | l = [] |
| 171 | d = {1:l} |
| 172 | i = C() |
| 173 | i.attr = d |
| 174 | l.append(i) |
| 175 | s = self.dumps(l) |
| 176 | x = self.loads(s) |
| 177 | self.assertEqual(x, l) |
| 178 | self.assertEqual(x[0], i) |
| 179 | self.assertEqual(x[0].attr, d) |
| 180 | self.assertEqual(x[0].attr[1], x) |
| 181 | self.assertEqual(x[0].attr[1][0], i) |
| 182 | self.assertEqual(x[0].attr[1][0].attr, d) |
| 183 | |
| 184 | def test_garyp(self): |
| 185 | self.assertRaises(self.error, self.loads, 'garyp') |
| 186 | |
| 187 | def test_insecure_strings(self): |
| 188 | insecure = ["abc", "2 + 2", # not quoted |
| 189 | "'abc' + 'def'", # not a single quoted string |
| 190 | "'abc", # quote is not closed |
| 191 | "'abc\"", # open quote and close quote don't match |
| 192 | "'abc' ?", # junk after close quote |
| 193 | # some tests of the quoting rules |
| 194 | "'abc\"\''", |
| 195 | "'\\\\a\'\'\'\\\'\\\\\''", |
| 196 | ] |
| 197 | for s in insecure: |
| 198 | buf = "S" + s + "\012p0\012." |
| 199 | self.assertRaises(ValueError, self.loads, buf) |
| 200 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 201 | if have_unicode: |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 202 | def test_unicode(self): |
| 203 | endcases = [unicode(''), unicode('<\\u>'), unicode('<\\\u1234>'), |
| 204 | unicode('<\n>'), unicode('<\\>')] |
| 205 | for u in endcases: |
| 206 | p = self.dumps(u) |
| 207 | u2 = self.loads(p) |
| 208 | self.assertEqual(u2, u) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 209 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 210 | def test_ints(self): |
| 211 | import sys |
| 212 | n = sys.maxint |
| 213 | while n: |
| 214 | for expected in (-n, n): |
| 215 | s = self.dumps(expected) |
| 216 | n2 = self.loads(s) |
| 217 | self.assertEqual(expected, n2) |
| 218 | n = n >> 1 |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 219 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 220 | def test_maxint64(self): |
| 221 | maxint64 = (1L << 63) - 1 |
| 222 | data = 'I' + str(maxint64) + '\n.' |
| 223 | got = self.loads(data) |
| 224 | self.assertEqual(got, maxint64) |
| 225 | |
| 226 | # Try too with a bogus literal. |
| 227 | data = 'I' + str(maxint64) + 'JUNK\n.' |
| 228 | self.assertRaises(ValueError, self.loads, data) |
| 229 | |
| 230 | def test_reduce(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 231 | pass |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 232 | |
| 233 | def test_getinitargs(self): |
| 234 | pass |
| 235 | |
| 236 | class AbstractPickleModuleTests(unittest.TestCase): |
| 237 | |
| 238 | def test_dump_closed_file(self): |
| 239 | import tempfile, os |
| 240 | fn = tempfile.mktemp() |
| 241 | f = open(fn, "w") |
| 242 | f.close() |
| 243 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 244 | os.remove(fn) |
| 245 | |
| 246 | def test_load_closed_file(self): |
| 247 | import tempfile, os |
| 248 | fn = tempfile.mktemp() |
| 249 | f = open(fn, "w") |
| 250 | f.close() |
| 251 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 252 | os.remove(fn) |