Kaydet (Commit) 2556c838 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #17299: Add test coverage for cPickle with file objects and general IO

objects.  Original patch by Aman Shah.
üst 69c66f9a
import cPickle, unittest import cPickle
from cStringIO import StringIO import cStringIO
import io
import unittest
from test.pickletester import (AbstractPickleTests, from test.pickletester import (AbstractPickleTests,
AbstractPickleModuleTests, AbstractPickleModuleTests,
AbstractPicklerUnpicklerObjectTests, AbstractPicklerUnpicklerObjectTests,
BigmemPickleTests) BigmemPickleTests)
from test import test_support from test import test_support
class cStringIOMixin:
output = input = cStringIO.StringIO
def close(self, f):
pass
class BytesIOMixin:
output = input = io.BytesIO
def close(self, f):
pass
class FileIOMixin:
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()
test_support.unlink(test_support.TESTFN)
class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
def setUp(self): def setUp(self):
...@@ -18,19 +52,35 @@ class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): ...@@ -18,19 +52,35 @@ class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
class cPicklePicklerTests(AbstractPickleTests): class cPicklePicklerTests(AbstractPickleTests):
def dumps(self, arg, proto=0): def dumps(self, arg, proto=0):
f = StringIO() f = self.output()
p = cPickle.Pickler(f, proto) try:
p.dump(arg) p = cPickle.Pickler(f, proto)
f.seek(0) p.dump(arg)
return f.read() f.seek(0)
return f.read()
finally:
self.close(f)
def loads(self, buf): def loads(self, buf):
f = StringIO(buf) f = self.input(buf)
p = cPickle.Unpickler(f) try:
return p.load() p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
error = cPickle.BadPickleGet error = cPickle.BadPickleGet
class cStringIOCPicklerTests(cStringIOMixin, cPicklePicklerTests):
pass
class BytesIOCPicklerTests(BytesIOMixin, cPicklePicklerTests):
pass
class FileIOCPicklerTests(FileIOMixin, cPicklePicklerTests):
pass
class cPickleListPicklerTests(AbstractPickleTests): class cPickleListPicklerTests(AbstractPickleTests):
def dumps(self, arg, proto=0): def dumps(self, arg, proto=0):
...@@ -39,26 +89,45 @@ class cPickleListPicklerTests(AbstractPickleTests): ...@@ -39,26 +89,45 @@ class cPickleListPicklerTests(AbstractPickleTests):
return p.getvalue() return p.getvalue()
def loads(self, *args): def loads(self, *args):
f = StringIO(args[0]) f = self.input(args[0])
p = cPickle.Unpickler(f) try:
return p.load() p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
error = cPickle.BadPickleGet error = cPickle.BadPickleGet
class cStringIOCPicklerListTests(cStringIOMixin, cPickleListPicklerTests):
pass
class BytesIOCPicklerListTests(BytesIOMixin, cPickleListPicklerTests):
pass
class FileIOCPicklerListTests(FileIOMixin, cPickleListPicklerTests):
pass
class cPickleFastPicklerTests(AbstractPickleTests): class cPickleFastPicklerTests(AbstractPickleTests):
def dumps(self, arg, proto=0): def dumps(self, arg, proto=0):
f = StringIO() f = self.output()
p = cPickle.Pickler(f, proto) try:
p.fast = 1 p = cPickle.Pickler(f, proto)
p.dump(arg) p.fast = 1
f.seek(0) p.dump(arg)
return f.read() f.seek(0)
return f.read()
finally:
self.close(f)
def loads(self, *args): def loads(self, *args):
f = StringIO(args[0]) f = self.input(args[0])
p = cPickle.Unpickler(f) try:
return p.load() p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
error = cPickle.BadPickleGet error = cPickle.BadPickleGet
...@@ -98,6 +167,16 @@ class cPickleFastPicklerTests(AbstractPickleTests): ...@@ -98,6 +167,16 @@ class cPickleFastPicklerTests(AbstractPickleTests):
b = self.loads(self.dumps(a)) b = self.loads(self.dumps(a))
self.assertEqual(a, b) self.assertEqual(a, b)
class cStringIOCPicklerFastTests(cStringIOMixin, cPickleFastPicklerTests):
pass
class BytesIOCPicklerFastTests(BytesIOMixin, cPickleFastPicklerTests):
pass
class FileIOCPicklerFastTests(FileIOMixin, cPickleFastPicklerTests):
pass
class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests): class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
pickler_class = cPickle.Pickler pickler_class = cPickle.Pickler
...@@ -140,9 +219,15 @@ class cPickleDeepRecursive(unittest.TestCase): ...@@ -140,9 +219,15 @@ class cPickleDeepRecursive(unittest.TestCase):
def test_main(): def test_main():
test_support.run_unittest( test_support.run_unittest(
cPickleTests, cPickleTests,
cPicklePicklerTests, cStringIOCPicklerTests,
cPickleListPicklerTests, BytesIOCPicklerTests,
cPickleFastPicklerTests, FileIOCPicklerTests,
cStringIOCPicklerListTests,
BytesIOCPicklerListTests,
FileIOCPicklerListTests,
cStringIOCPicklerFastTests,
BytesIOCPicklerFastTests,
FileIOCPicklerFastTests,
cPickleDeepRecursive, cPickleDeepRecursive,
cPicklePicklerUnpicklerObjectTests, cPicklePicklerUnpicklerObjectTests,
cPickleBigmemPickleTests, cPickleBigmemPickleTests,
......
...@@ -910,6 +910,7 @@ Jerry Seutter ...@@ -910,6 +910,7 @@ Jerry Seutter
Pete Sevander Pete Sevander
Denis Severson Denis Severson
Ian Seyer Ian Seyer
Aman Shah
Ha Shao Ha Shao
Mark Shannon Mark Shannon
Richard Shapiro Richard Shapiro
......
...@@ -816,6 +816,9 @@ Extension Modules ...@@ -816,6 +816,9 @@ Extension Modules
Tests Tests
----- -----
- Issue #17299: Add test coverage for cPickle with file objects and general IO
objects. Original patch by Aman Shah.
- Issue #11963: remove human verification from test_parser and test_subprocess. - Issue #11963: remove human verification from test_parser and test_subprocess.
- Issue #17249: convert a test in test_capi to use unittest and reap threads. - Issue #17249: convert a test in test_capi to use unittest and reap threads.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment