2000-09-15 12:14:51 -03:00
|
|
|
import pickle
|
2007-05-08 18:26:54 -03:00
|
|
|
import io
|
2012-03-04 13:31:48 -04:00
|
|
|
import collections
|
2014-12-16 13:39:08 -04:00
|
|
|
import struct
|
|
|
|
import sys
|
2003-01-28 18:34:11 -04:00
|
|
|
|
2014-12-16 13:39:08 -04:00
|
|
|
import unittest
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2003-01-28 18:34:11 -04:00
|
|
|
from test.pickletester import AbstractPickleTests
|
|
|
|
from test.pickletester import AbstractPickleModuleTests
|
|
|
|
from test.pickletester import AbstractPersistentPicklerTests
|
2009-04-16 00:18:06 -03:00
|
|
|
from test.pickletester import AbstractPicklerUnpicklerObjectTests
|
2012-03-04 13:31:48 -04:00
|
|
|
from test.pickletester import AbstractDispatchTableTests
|
2011-08-29 18:09:33 -03:00
|
|
|
from test.pickletester import BigmemPickleTests
|
2003-01-28 18:34:11 -04:00
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
try:
|
|
|
|
import _pickle
|
|
|
|
has_c_implementation = True
|
|
|
|
except ImportError:
|
|
|
|
has_c_implementation = False
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2007-11-06 17:34:58 -04:00
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
class PickleTests(AbstractPickleModuleTests):
|
|
|
|
pass
|
2003-01-29 13:58:45 -04:00
|
|
|
|
2001-10-18 18:57:37 -03:00
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
class PyPicklerTests(AbstractPickleTests):
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2008-03-17 19:56:06 -03:00
|
|
|
def dumps(self, arg, proto=None):
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO()
|
2008-06-12 15:26:05 -03:00
|
|
|
p = self.pickler(f, proto)
|
2001-10-15 18:38:56 -03:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
2007-05-08 18:26:54 -03:00
|
|
|
return bytes(f.read())
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2011-02-24 16:34:38 -04:00
|
|
|
def loads(self, buf, **kwds):
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO(buf)
|
2011-02-24 16:34:38 -04:00
|
|
|
u = self.unpickler(f, **kwds)
|
2001-10-15 18:38:56 -03:00
|
|
|
return u.load()
|
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
|
2011-08-29 18:09:33 -03:00
|
|
|
class InMemoryPickleTests(AbstractPickleTests, BigmemPickleTests):
|
2010-09-09 15:33:21 -03:00
|
|
|
|
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
|
|
|
|
2011-08-29 18:09:33 -03:00
|
|
|
def dumps(self, arg, protocol=None):
|
|
|
|
return pickle.dumps(arg, protocol)
|
2010-09-09 15:33:21 -03:00
|
|
|
|
2011-02-24 16:34:38 -04:00
|
|
|
def loads(self, buf, **kwds):
|
|
|
|
return pickle.loads(buf, **kwds)
|
2010-09-09 15:33:21 -03:00
|
|
|
|
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
class PyPersPicklerTests(AbstractPersistentPicklerTests):
|
|
|
|
|
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
2002-11-13 18:01:27 -04:00
|
|
|
|
2008-03-17 19:56:06 -03:00
|
|
|
def dumps(self, arg, proto=None):
|
2008-06-12 15:26:05 -03:00
|
|
|
class PersPickler(self.pickler):
|
2002-11-13 18:01:27 -04:00
|
|
|
def persistent_id(subself, obj):
|
|
|
|
return self.persistent_id(obj)
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO()
|
2003-01-27 23:51:53 -04:00
|
|
|
p = PersPickler(f, proto)
|
2002-11-13 18:01:27 -04:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
2011-02-24 16:34:38 -04:00
|
|
|
def loads(self, buf, **kwds):
|
2008-06-12 15:26:05 -03:00
|
|
|
class PersUnpickler(self.unpickler):
|
2002-11-13 18:01:27 -04:00
|
|
|
def persistent_load(subself, obj):
|
|
|
|
return self.persistent_load(obj)
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO(buf)
|
2011-02-24 16:34:38 -04:00
|
|
|
u = PersUnpickler(f, **kwds)
|
2002-11-13 18:01:27 -04:00
|
|
|
return u.load()
|
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
|
2009-04-16 00:18:06 -03:00
|
|
|
class PyPicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
|
|
|
|
|
|
|
|
pickler_class = pickle._Pickler
|
|
|
|
unpickler_class = pickle._Unpickler
|
|
|
|
|
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
class PyDispatchTableTests(AbstractDispatchTableTests):
|
2013-12-07 05:09:27 -04:00
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
pickler_class = pickle._Pickler
|
2013-12-07 05:09:27 -04:00
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
def get_dispatch_table(self):
|
|
|
|
return pickle.dispatch_table.copy()
|
|
|
|
|
|
|
|
|
|
|
|
class PyChainDispatchTableTests(AbstractDispatchTableTests):
|
2013-12-07 05:09:27 -04:00
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
pickler_class = pickle._Pickler
|
2013-12-07 05:09:27 -04:00
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
def get_dispatch_table(self):
|
|
|
|
return collections.ChainMap({}, pickle.dispatch_table)
|
|
|
|
|
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
if has_c_implementation:
|
|
|
|
class CPicklerTests(PyPicklerTests):
|
|
|
|
pickler = _pickle.Pickler
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
|
|
|
|
class CPersPicklerTests(PyPersPicklerTests):
|
|
|
|
pickler = _pickle.Pickler
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
|
2009-04-16 00:18:06 -03:00
|
|
|
class CDumpPickle_LoadPickle(PyPicklerTests):
|
|
|
|
pickler = _pickle.Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
|
|
|
|
|
|
|
class DumpPickle_CLoadPickle(PyPicklerTests):
|
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
|
|
|
|
class CPicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
|
|
|
|
pickler_class = _pickle.Pickler
|
|
|
|
unpickler_class = _pickle.Unpickler
|
|
|
|
|
2013-07-01 10:17:45 -03:00
|
|
|
def test_issue18339(self):
|
|
|
|
unpickler = self.unpickler_class(io.BytesIO())
|
2013-07-01 18:00:13 -03:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
unpickler.memo = object
|
2013-07-01 10:17:45 -03:00
|
|
|
# used to cause a segfault
|
2013-07-01 18:00:13 -03:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
unpickler.memo = {-1: None}
|
2013-07-01 10:17:45 -03:00
|
|
|
unpickler.memo = {1: None}
|
|
|
|
|
2012-03-04 13:31:48 -04:00
|
|
|
class CDispatchTableTests(AbstractDispatchTableTests):
|
|
|
|
pickler_class = pickle.Pickler
|
|
|
|
def get_dispatch_table(self):
|
|
|
|
return pickle.dispatch_table.copy()
|
|
|
|
|
|
|
|
class CChainDispatchTableTests(AbstractDispatchTableTests):
|
|
|
|
pickler_class = pickle.Pickler
|
|
|
|
def get_dispatch_table(self):
|
|
|
|
return collections.ChainMap({}, pickle.dispatch_table)
|
|
|
|
|
2014-12-16 13:39:08 -04:00
|
|
|
@support.cpython_only
|
|
|
|
class SizeofTests(unittest.TestCase):
|
|
|
|
check_sizeof = support.check_sizeof
|
|
|
|
|
|
|
|
def test_pickler(self):
|
|
|
|
basesize = support.calcobjsize('5P2n3i2n3iP')
|
|
|
|
p = _pickle.Pickler(io.BytesIO())
|
|
|
|
self.assertEqual(object.__sizeof__(p), basesize)
|
|
|
|
MT_size = struct.calcsize('3nP0n')
|
|
|
|
ME_size = struct.calcsize('Pn0P')
|
|
|
|
check = self.check_sizeof
|
|
|
|
check(p, basesize +
|
|
|
|
MT_size + 8 * ME_size + # Minimal memo table size.
|
|
|
|
sys.getsizeof(b'x'*4096)) # Minimal write buffer size.
|
|
|
|
for i in range(6):
|
|
|
|
p.dump(chr(i))
|
|
|
|
check(p, basesize +
|
|
|
|
MT_size + 32 * ME_size + # Size of memo table required to
|
|
|
|
# save references to 6 objects.
|
|
|
|
0) # Write buffer is cleared after every dump().
|
|
|
|
|
|
|
|
def test_unpickler(self):
|
|
|
|
basesize = support.calcobjsize('2Pn2P 2P2n2i5P 2P3n6P2n2i')
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
P = struct.calcsize('P') # Size of memo table entry.
|
|
|
|
n = struct.calcsize('n') # Size of mark table entry.
|
|
|
|
check = self.check_sizeof
|
|
|
|
for encoding in 'ASCII', 'UTF-16', 'latin-1':
|
|
|
|
for errors in 'strict', 'replace':
|
|
|
|
u = unpickler(io.BytesIO(),
|
|
|
|
encoding=encoding, errors=errors)
|
|
|
|
self.assertEqual(object.__sizeof__(u), basesize)
|
|
|
|
check(u, basesize +
|
|
|
|
32 * P + # Minimal memo table size.
|
|
|
|
len(encoding) + 1 + len(errors) + 1)
|
|
|
|
|
|
|
|
stdsize = basesize + len('ASCII') + 1 + len('strict') + 1
|
|
|
|
def check_unpickler(data, memo_size, marks_size):
|
|
|
|
dump = pickle.dumps(data)
|
|
|
|
u = unpickler(io.BytesIO(dump),
|
|
|
|
encoding='ASCII', errors='strict')
|
|
|
|
u.load()
|
|
|
|
check(u, stdsize + memo_size * P + marks_size * n)
|
|
|
|
|
|
|
|
check_unpickler(0, 32, 0)
|
|
|
|
# 20 is minimal non-empty mark stack size.
|
|
|
|
check_unpickler([0] * 100, 32, 20)
|
|
|
|
# 128 is memo table size required to save references to 100 objects.
|
|
|
|
check_unpickler([chr(i) for i in range(100)], 128, 20)
|
|
|
|
def recurse(deep):
|
|
|
|
data = 0
|
|
|
|
for i in range(deep):
|
|
|
|
data = [data, data]
|
|
|
|
return data
|
|
|
|
check_unpickler(recurse(0), 32, 0)
|
|
|
|
check_unpickler(recurse(1), 32, 20)
|
|
|
|
check_unpickler(recurse(20), 32, 58)
|
|
|
|
check_unpickler(recurse(50), 64, 58)
|
|
|
|
check_unpickler(recurse(100), 128, 134)
|
|
|
|
|
|
|
|
u = unpickler(io.BytesIO(pickle.dumps('a', 0)),
|
|
|
|
encoding='ASCII', errors='strict')
|
|
|
|
u.load()
|
|
|
|
check(u, stdsize + 32 * P + 2 + 1)
|
|
|
|
|
2008-06-12 15:26:05 -03:00
|
|
|
|
2001-12-19 12:42:15 -04:00
|
|
|
def test_main():
|
2012-03-04 13:31:48 -04:00
|
|
|
tests = [PickleTests, PyPicklerTests, PyPersPicklerTests,
|
|
|
|
PyDispatchTableTests, PyChainDispatchTableTests]
|
2008-06-12 15:26:05 -03:00
|
|
|
if has_c_implementation:
|
2009-04-16 00:18:06 -03:00
|
|
|
tests.extend([CPicklerTests, CPersPicklerTests,
|
|
|
|
CDumpPickle_LoadPickle, DumpPickle_CLoadPickle,
|
|
|
|
PyPicklerUnpicklerObjectTests,
|
2010-09-09 15:33:21 -03:00
|
|
|
CPicklerUnpicklerObjectTests,
|
2012-03-04 13:31:48 -04:00
|
|
|
CDispatchTableTests, CChainDispatchTableTests,
|
2014-12-16 13:39:08 -04:00
|
|
|
InMemoryPickleTests, SizeofTests])
|
2008-06-12 15:26:05 -03:00
|
|
|
support.run_unittest(*tests)
|
2008-05-20 18:35:26 -03:00
|
|
|
support.run_doctest(pickle)
|
2001-12-19 12:42:15 -04:00
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
if __name__ == "__main__":
|
2001-12-19 12:42:15 -04:00
|
|
|
test_main()
|