115
121
class use_metaclass(object, metaclass=metaclass):
124
class pickling_metaclass(type):
125
def __eq__(self, other):
126
return (type(self) == type(other) and
127
self.reduce_args == other.reduce_args)
129
def __reduce__(self):
130
return (create_dynamic_class, self.reduce_args)
132
def create_dynamic_class(name, bases):
133
result = pickling_metaclass(name, bases, dict())
134
result.reduce_args = (name, bases)
118
137
# DATA0 .. DATA2 are the pickles we expect under the various protocols, for
119
138
# the object returned by create_data().
618
637
def test_bytes(self):
619
638
for proto in protocols:
620
for u in b'', b'xyz', b'xyz'*100:
622
self.assertEqual(self.loads(p), u)
639
for s in b'', b'xyz', b'xyz'*100:
641
self.assertEqual(self.loads(p), s)
642
for s in [bytes([i]) for i in range(256)]:
644
self.assertEqual(self.loads(p), s)
645
for s in [bytes([i, i]) for i in range(256)]:
647
self.assertEqual(self.loads(p), s)
624
649
def test_ints(self):
1098
1131
empty = self.loads(b'\x80\x03U\x00q\x00.', encoding='koi8-r')
1099
1132
self.assertEqual(empty, '')
1134
def check_negative_32b_binXXX(self, dumped):
1135
if sys.maxsize > 2**32:
1136
self.skipTest("test is only meaningful on 32-bit builds")
1137
# XXX Pure Python pickle reads lengths as signed and passes
1138
# them directly to read() (hence the EOFError)
1139
with self.assertRaises((pickle.UnpicklingError, EOFError,
1140
ValueError, OverflowError)):
1143
def test_negative_32b_binbytes(self):
1144
# On 32-bit builds, a BINBYTES of 2**31 or more is refused
1145
self.check_negative_32b_binXXX(b'\x80\x03B\xff\xff\xff\xffxyzq\x00.')
1147
def test_negative_32b_binunicode(self):
1148
# On 32-bit builds, a BINUNICODE of 2**31 or more is refused
1149
self.check_negative_32b_binXXX(b'\x80\x03X\xff\xff\xff\xffxyzq\x00.')
1151
def test_negative_put(self):
1153
dumped = b'Va\np-1\n.'
1154
self.assertRaises(ValueError, self.loads, dumped)
1156
def test_negative_32b_binput(self):
1158
if sys.maxsize > 2**32:
1159
self.skipTest("test is only meaningful on 32-bit builds")
1160
dumped = b'\x80\x03X\x01\x00\x00\x00ar\xff\xff\xff\xff.'
1161
self.assertRaises(ValueError, self.loads, dumped)
1164
class BigmemPickleTests(unittest.TestCase):
1166
# Binary protocols can serialize longs of up to 2GB-1
1168
@bigmemtest(size=_2G, memuse=1 + 1, dry_run=False)
1169
def test_huge_long_32b(self, size):
1170
data = 1 << (8 * size)
1172
for proto in protocols:
1175
with self.assertRaises((ValueError, OverflowError)):
1176
self.dumps(data, protocol=proto)
1180
# Protocol 3 can serialize up to 4GB-1 as a bytes object
1181
# (older protocols don't have a dedicated opcode for bytes and are
1184
@bigmemtest(size=_2G, memuse=1 + 1, dry_run=False)
1185
def test_huge_bytes_32b(self, size):
1186
data = b"abcd" * (size // 4)
1188
for proto in protocols:
1192
pickled = self.dumps(data, protocol=proto)
1193
self.assertTrue(b"abcd" in pickled[:15])
1194
self.assertTrue(b"abcd" in pickled[-15:])
1200
@bigmemtest(size=_4G, memuse=1 + 1, dry_run=False)
1201
def test_huge_bytes_64b(self, size):
1204
for proto in protocols:
1207
with self.assertRaises((ValueError, OverflowError)):
1208
self.dumps(data, protocol=proto)
1212
# All protocols use 1-byte per printable ASCII character; we add another
1213
# byte because the encoded form has to be copied into the internal buffer.
1215
@bigmemtest(size=_2G, memuse=2 + character_size, dry_run=False)
1216
def test_huge_str_32b(self, size):
1217
data = "abcd" * (size // 4)
1219
for proto in protocols:
1221
pickled = self.dumps(data, protocol=proto)
1222
self.assertTrue(b"abcd" in pickled[:15])
1223
self.assertTrue(b"abcd" in pickled[-15:])
1229
# BINUNICODE (protocols 1, 2 and 3) cannot carry more than
1230
# 2**32 - 1 bytes of utf-8 encoded unicode.
1232
@bigmemtest(size=_4G, memuse=1 + character_size, dry_run=False)
1233
def test_huge_str_64b(self, size):
1236
for proto in protocols:
1239
with self.assertRaises((ValueError, OverflowError)):
1240
self.dumps(data, protocol=proto)
1101
1245
# Test classes for reduce_ex
1103
1247
class REX_one(object):