diff -r 92003d9aae0e Lib/test/test_cpickle.py --- a/Lib/test/test_cpickle.py Tue Feb 26 12:37:07 2013 +0000 +++ b/Lib/test/test_cpickle.py Sat Mar 09 18:10:00 2013 +0530 @@ -1,5 +1,8 @@ -import cPickle, unittest -from cStringIO import StringIO +import cPickle +import unittest +import cStringIO +import io +import os from test.pickletester import (AbstractPickleTests, AbstractPickleModuleTests, AbstractPicklerUnpicklerObjectTests, @@ -15,23 +18,55 @@ error = cPickle.BadPickleGet module = cPickle -class cPicklePicklerTests(AbstractPickleTests): +class AbstractIOCPicklerTests(AbstractPickleTests): def dumps(self, arg, proto=0): - f = StringIO() + f = self.output() p = cPickle.Pickler(f, proto) p.dump(arg) f.seek(0) - return f.read() + contents = f.read() + self.close(f) + return contents def loads(self, buf): - f = StringIO(buf) + f = self.input(buf) p = cPickle.Unpickler(f) - return p.load() + contents = p.load() + self.close(f) + return contents + + def close(self, f): + pass error = cPickle.BadPickleGet -class cPickleListPicklerTests(AbstractPickleTests): +class StringIOCPicklerTests(AbstractIOCPicklerTests): + output = input = cStringIO.StringIO + +class BytesIOCPicklerTests(AbstractIOCPicklerTests): + output = input = io.BytesIO + +class FileIOCPicklerTests(AbstractIOCPicklerTests): + + def output(self): + return open(test_support.TESTFN, 'w+') + + def input(self, data): + f = open(test_support.TESTFN, 'w+') + try: + f.write(data) + f.seek(0) + return f + except: + f.close() + raise + + def close(self, f): + f.close() + os.remove(test_support.TESTFN) + +class AbstractIOCPicklerListTests(AbstractPickleTests): def dumps(self, arg, proto=0): p = cPickle.Pickler(proto) @@ -39,26 +74,59 @@ return p.getvalue() def loads(self, *args): - f = StringIO(args[0]) + f = self.input(args[0]) p = cPickle.Unpickler(f) - return p.load() + contents = p.load() + self.close(f) + return contents + + def close(self, f): + pass error = cPickle.BadPickleGet -class cPickleFastPicklerTests(AbstractPickleTests): +class StringIOCPicklerListTests(AbstractIOCPicklerListTests): + input = cStringIO.StringIO + +class BytesIOCPicklerListTests(AbstractIOCPicklerListTests): + input = io.BytesIO + +class FileIOCPicklerListTests(AbstractIOCPicklerListTests): + def input(self, data): + f = open(test_support.TESTFN, 'w+') + try: + f.write(data) + f.seek(0) + return f + except: + f.close() + raise + + def close(self, f): + f.close() + os.remove(test_support.TESTFN) + +class AbstractIOCPicklerFastTests(AbstractPickleTests): def dumps(self, arg, proto=0): - f = StringIO() + f = self.output() p = cPickle.Pickler(f, proto) p.fast = 1 p.dump(arg) f.seek(0) - return f.read() + contents = f.read() + self.close(f) + return contents def loads(self, *args): - f = StringIO(args[0]) + f = self.input(args[0]) p = cPickle.Unpickler(f) - return p.load() + contents = p.load() + self.close(f) + return contents + + def close(self, f): + pass error = cPickle.BadPickleGet @@ -98,6 +166,31 @@ b = self.loads(self.dumps(a)) self.assertEqual(a, b) +class StringIOCPicklerFastTests(AbstractIOCPicklerFastTests): + output = input = cStringIO.StringIO + +class BytesIOCPicklerFastTests(AbstractIOCPicklerFastTests): + output = input = io.BytesIO + +class FileIOCPicklerFastTests(AbstractIOCPicklerFastTests): + + def output(self): + return open(test_support.TESTFN, 'w+') + + def input(self, data): + f = open(test_support.TESTFN, 'w+') + try: + f.write(data) + f.seek(0) + return f + except: + f.close() + raise + + def close(self, f): + f.close() + os.remove(test_support.TESTFN) + class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests): pickler_class = cPickle.Pickler @@ -140,9 +233,15 @@ def test_main(): test_support.run_unittest( cPickleTests, - cPicklePicklerTests, - cPickleListPicklerTests, - cPickleFastPicklerTests, + StringIOCPicklerTests, + BytesIOCPicklerTests, + FileIOCPicklerTests, + StringIOCPicklerListTests, + BytesIOCPicklerListTests, + FileIOCPicklerListTests, + StringIOCPicklerFastTests, + BytesIOCPicklerFastTests, + FileIOCPicklerFastTests, cPickleDeepRecursive, cPicklePicklerUnpicklerObjectTests, cPickleBigmemPickleTests,

AltStyle によって変換されたページ (->オリジナル) /