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