2 from test
.test_support
import TestFailed
, have_unicode
, TESTFN
5 def __cmp__(self
, other
):
6 return cmp(self
.__dict
__, other
.__dict
__)
10 C
.__module
__ = "__main__"
13 def __init__(self
, x
):
18 __safe_for_unpickling__
= 1
20 def __init__(self
, a
, b
):
24 def __getinitargs__(self
):
27 class metaclass(type):
30 class use_metaclass(object):
31 __metaclass__
= metaclass
33 # break into multiple strings to avoid confusing font-lock-mode
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' + \
84 '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' + \
85 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' + \
92 x
= [0, 1L, 2.0, 3.0+0j
]
93 # Append some integer test cases at cPickle.c's internal size
99 uint1max
, -uint1max
, -uint1max
-1,
100 uint2max
, -uint2max
, -uint2max
-1,
101 int4max
, -int4max
, -int4max
-1])
102 y
= ('abc', 'abc', c
, c
)
108 class AbstractPickleTests(unittest
.TestCase
):
110 _testdata
= create_data()
113 # subclass must define self.dumps, self.loads, self.error
117 # test various datatypes not tested by testdata
121 self
.assertEqual(x
, y
)
126 self
.assertEqual(x
, y
)
131 self
.assertEqual(x
, y
)
133 # XXX test __reduce__ protocol?
135 def test_identity(self
):
136 s
= self
.dumps(self
._testdata
)
138 self
.assertEqual(x
, self
._testdata
)
140 def test_constant(self
):
142 self
.assertEqual(x
, self
._testdata
)
143 x
= self
.loads(BINDATA
)
144 self
.assertEqual(x
, self
._testdata
)
146 def test_binary(self
):
147 s
= self
.dumps(self
._testdata
, 1)
149 self
.assertEqual(x
, self
._testdata
)
151 def test_recursive_list(self
):
156 self
.assertEqual(x
, l
)
157 self
.assertEqual(x
, x
[0])
158 self
.assertEqual(id(x
), id(x
[0]))
160 def test_recursive_dict(self
):
165 self
.assertEqual(x
, d
)
166 self
.assertEqual(x
[1], x
)
167 self
.assertEqual(id(x
[1]), id(x
))
169 def test_recursive_inst(self
):
174 self
.assertEqual(x
, i
)
175 self
.assertEqual(x
.attr
, x
)
176 self
.assertEqual(id(x
.attr
), id(x
))
178 def test_recursive_multi(self
):
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
)
193 def test_garyp(self
):
194 self
.assertRaises(self
.error
, self
.loads
, 'garyp')
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 "'\\'", # trailing backslash
203 # some tests of the quoting rules
205 #"'\\\\a\'\'\'\\\'\\\\\''",
208 buf
= "S" + s
+ "\012p0\012."
209 self
.assertRaises(ValueError, self
.loads
, buf
)
212 def test_unicode(self
):
213 endcases
= [unicode(''), unicode('<\\u>'), unicode('<\\\u1234>'),
214 unicode('<\n>'), unicode('<\\>')]
218 self
.assertEqual(u2
, u
)
224 for expected
in (-n
, n
):
225 s
= self
.dumps(expected
)
227 self
.assertEqual(expected
, n2
)
230 def test_maxint64(self
):
231 maxint64
= (1L << 63) - 1
232 data
= 'I' + str(maxint64
) + '\n.'
233 got
= self
.loads(data
)
234 self
.assertEqual(got
, maxint64
)
236 # Try too with a bogus literal.
237 data
= 'I' + str(maxint64
) + 'JUNK\n.'
238 self
.assertRaises(ValueError, self
.loads
, data
)
240 def test_reduce(self
):
243 def test_getinitargs(self
):
246 def test_metaclass(self
):
250 self
.assertEqual(a
.__class
__, b
.__class
__)
252 def test_structseq(self
):
257 self
.assertEqual(t
, u
)
259 if hasattr(os
, "stat"):
260 t
= os
.stat(os
.curdir
)
263 self
.assertEqual(t
, u
)
264 if hasattr(os
, "statvfs"):
265 t
= os
.statvfs(os
.curdir
)
268 self
.assertEqual(t
, u
)
270 class AbstractPickleModuleTests(unittest
.TestCase
):
272 def test_dump_closed_file(self
):
274 f
= open(TESTFN
, "w")
277 self
.assertRaises(ValueError, self
.module
.dump
, 123, f
)
281 def test_load_closed_file(self
):
283 f
= open(TESTFN
, "w")
286 self
.assertRaises(ValueError, self
.module
.dump
, 123, f
)