1 # test_pickle and test_cpickle both use this.
3 from test_support
import TestFailed
6 # break into multiple strings to please font-lock-mode
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' + \
57 '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' + \
58 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' + \
62 def __cmp__(self
, other
):
63 return cmp(self
.__dict
__, other
.__dict
__)
67 C
.__module
__ = "__main__"
69 # Call this with the module to be tested (pickle or cPickle).
75 x
= [0, 1L, 2.0, 3.0+0j
]
76 # Append some integer test cases at cPickle.c's internal size
82 uint1max
, -uint1max
, -uint1max
-1,
83 uint2max
, -uint2max
, -uint2max
-1,
84 int4max
, -int4max
, -int4max
-1])
85 y
= ('abc', 'abc', c
, c
)
103 x2
= pickle
.loads(DATA
)
109 print "dumps() binary"
110 s
= pickle
.dumps(x
, 1)
112 print "loads() binary"
119 print "loads() BINDATA"
120 x2
= pickle
.loads(BINDATA
)
126 print "dumps() RECURSIVE"
134 # don't create cyclic garbage
138 # Test protection against closed files
140 fn
= tempfile
.mktemp()
148 print "dump to closed file should raise ValueError"
157 print "load from closed file should raise ValueError"
160 # Test specific bad cases
163 x
= pickle
.loads('garyp')
167 except pickle
.BadPickleGet
, y
:
171 print "unexpected success!"
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
182 "'\\\\a\'\'\'\\\'\\\\\''",
185 buf
= "S" + s
+ "\012p0\012."
187 x
= pickle
.loads(buf
)
191 print "accepted insecure string: %s" % repr(buf
)
193 # Test some Unicode end cases
194 endcases
= [u
'', u
'<\\u>', u
'<\\\u1234>', u
'<\n>', u
'<\\>']
197 u2
= pickle
.loads(pickle
.dumps(u
))
198 except Exception, msg
:
199 print "Endcase exception: %s => %s(%s)" % \
200 (`u`
, msg
.__class
__.__name
__, str(msg
))
203 print "Endcase failure: %s => %s" % (`u`
, `u2`
)
205 # Test the full range of Python ints.
208 for expected
in (-n
, n
):
209 for binary_mode
in (0, 1):
210 s
= pickle
.dumps(expected
, binary_mode
)
211 got
= pickle
.loads(s
)
213 raise TestFailed("for %s-mode pickle of %d, pickle "
214 "string is %s, loaded back as %s" % (
215 binary_mode
and "binary" or "text",