1999-03-25 18:38:49 -04:00
|
|
|
import cPickle
|
2001-12-19 12:42:15 -04:00
|
|
|
import unittest
|
2001-10-15 18:38:56 -03:00
|
|
|
from cStringIO import StringIO
|
|
|
|
from pickletester import AbstractPickleTests, AbstractPickleModuleTests
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-10-15 18:38:56 -03:00
|
|
|
|
|
|
|
class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
|
2001-10-18 18:57:37 -03:00
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
def setUp(self):
|
|
|
|
self.dumps = cPickle.dumps
|
|
|
|
self.loads = cPickle.loads
|
|
|
|
|
|
|
|
error = cPickle.BadPickleGet
|
|
|
|
module = cPickle
|
|
|
|
|
|
|
|
class cPicklePicklerTests(AbstractPickleTests):
|
|
|
|
|
2003-01-28 18:26:28 -04:00
|
|
|
def dumps(self, arg, proto=0):
|
2001-10-15 18:38:56 -03:00
|
|
|
f = StringIO()
|
2003-01-28 18:26:28 -04:00
|
|
|
p = cPickle.Pickler(f, proto)
|
2001-10-15 18:38:56 -03:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
f = StringIO(buf)
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
|
|
|
|
error = cPickle.BadPickleGet
|
|
|
|
|
|
|
|
class cPickleListPicklerTests(AbstractPickleTests):
|
|
|
|
|
2003-01-28 18:26:28 -04:00
|
|
|
def dumps(self, arg, proto=0):
|
|
|
|
p = cPickle.Pickler(proto)
|
2001-10-15 18:38:56 -03:00
|
|
|
p.dump(arg)
|
|
|
|
return p.getvalue()
|
|
|
|
|
|
|
|
def loads(self, *args):
|
|
|
|
f = StringIO(args[0])
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
|
|
|
|
error = cPickle.BadPickleGet
|
|
|
|
|
|
|
|
class cPickleFastPicklerTests(AbstractPickleTests):
|
|
|
|
|
2003-01-28 18:26:28 -04:00
|
|
|
def dumps(self, arg, proto=0):
|
2001-10-15 18:38:56 -03:00
|
|
|
f = StringIO()
|
2003-01-28 18:26:28 -04:00
|
|
|
p = cPickle.Pickler(f, proto)
|
2001-10-15 18:38:56 -03:00
|
|
|
p.fast = 1
|
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def loads(self, *args):
|
|
|
|
f = StringIO(args[0])
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
|
|
|
|
error = cPickle.BadPickleGet
|
|
|
|
|
|
|
|
def test_recursive_list(self):
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
AbstractPickleTests.test_recursive_list,
|
|
|
|
self)
|
|
|
|
|
|
|
|
def test_recursive_inst(self):
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
AbstractPickleTests.test_recursive_inst,
|
|
|
|
self)
|
|
|
|
|
|
|
|
def test_recursive_dict(self):
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
AbstractPickleTests.test_recursive_dict,
|
|
|
|
self)
|
|
|
|
|
|
|
|
def test_recursive_multi(self):
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
AbstractPickleTests.test_recursive_multi,
|
|
|
|
self)
|
|
|
|
|
2001-12-21 16:04:22 -04:00
|
|
|
def test_nonrecursive_deep(self):
|
|
|
|
a = []
|
|
|
|
for i in range(100):
|
|
|
|
a = [a]
|
|
|
|
b = self.loads(self.dumps(a))
|
|
|
|
self.assertEqual(a, b)
|
|
|
|
|
2001-12-19 12:42:15 -04:00
|
|
|
def test_main():
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
suite.addTest(loader.loadTestsFromTestCase(cPickleTests))
|
|
|
|
suite.addTest(loader.loadTestsFromTestCase(cPicklePicklerTests))
|
|
|
|
suite.addTest(loader.loadTestsFromTestCase(cPickleListPicklerTests))
|
|
|
|
suite.addTest(loader.loadTestsFromTestCase(cPickleFastPicklerTests))
|
|
|
|
test_support.run_suite(suite)
|
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
if __name__ == "__main__":
|
2001-12-19 12:42:15 -04:00
|
|
|
test_main()
|