mirror of
https://github.com/python/cpython.git
synced 2024-11-29 21:05:33 +08:00
04f357cffe
imports e.g. test_support must do so using an absolute package name such as "import test.test_support" or "from test import test_support". This also updates the README in Lib/test, and gets rid of the duplicate data dirctory in Lib/test/data (replaced by Lib/email/test/data). Now Tim and Jack can have at it. :)
101 lines
2.7 KiB
Python
101 lines
2.7 KiB
Python
import cPickle
|
|
import unittest
|
|
from cStringIO import StringIO
|
|
from pickletester import AbstractPickleTests, AbstractPickleModuleTests
|
|
from test import test_support
|
|
|
|
class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
|
|
|
|
def setUp(self):
|
|
self.dumps = cPickle.dumps
|
|
self.loads = cPickle.loads
|
|
|
|
error = cPickle.BadPickleGet
|
|
module = cPickle
|
|
|
|
class cPicklePicklerTests(AbstractPickleTests):
|
|
|
|
def dumps(self, arg, bin=0):
|
|
f = StringIO()
|
|
p = cPickle.Pickler(f, bin)
|
|
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):
|
|
|
|
def dumps(self, arg, bin=0):
|
|
p = cPickle.Pickler(bin)
|
|
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):
|
|
|
|
def dumps(self, arg, bin=0):
|
|
f = StringIO()
|
|
p = cPickle.Pickler(f, bin)
|
|
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)
|
|
|
|
def test_nonrecursive_deep(self):
|
|
a = []
|
|
for i in range(100):
|
|
a = [a]
|
|
b = self.loads(self.dumps(a))
|
|
self.assertEqual(a, b)
|
|
|
|
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)
|
|
|
|
if __name__ == "__main__":
|
|
test_main()
|