Moved unpickling tests with prepickled data to separate class.

This commit is contained in:
Serhiy Storchaka 2015-09-29 15:36:28 +03:00
parent 958a9c7520
commit 4d2cf5587c
3 changed files with 88 additions and 47 deletions

View File

@ -420,11 +420,54 @@ def create_data():
x.append(5)
return x
class AbstractPickleTests(unittest.TestCase):
# Subclass must define self.dumps, self.loads, self.error.
class AbstractUnpickleTests(unittest.TestCase):
# Subclass must define self.loads, self.error.
_testdata = create_data()
def test_load_from_canned_string(self):
expected = self._testdata
for canned in DATA0, DATA1, DATA2:
got = self.loads(canned)
self.assertEqual(expected, got)
def test_garyp(self):
self.assertRaises(self.error, self.loads, 'garyp')
def test_maxint64(self):
maxint64 = (1L << 63) - 1
data = 'I' + str(maxint64) + '\n.'
got = self.loads(data)
self.assertEqual(got, maxint64)
# Try too with a bogus literal.
data = 'I' + str(maxint64) + 'JUNK\n.'
self.assertRaises(ValueError, self.loads, data)
def test_insecure_strings(self):
insecure = ["abc", "2 + 2", # not quoted
#"'abc' + 'def'", # not a single quoted string
"'abc", # quote is not closed
"'abc\"", # open quote and close quote don't match
"'abc' ?", # junk after close quote
"'\\'", # trailing backslash
"'", # issue #17710
"' ", # issue #17710
# some tests of the quoting rules
#"'abc\"\''",
#"'\\\\a\'\'\'\\\'\\\\\''",
]
for s in insecure:
buf = "S" + s + "\012p0\012."
self.assertRaises(ValueError, self.loads, buf)
class AbstractPickleTests(unittest.TestCase):
# Subclass must define self.dumps, self.loads.
_testdata = AbstractUnpickleTests._testdata
def setUp(self):
pass
@ -455,12 +498,6 @@ class AbstractPickleTests(unittest.TestCase):
got = self.loads(s)
self.assertEqual(expected, got)
def test_load_from_canned_string(self):
expected = self._testdata
for canned in DATA0, DATA1, DATA2:
got = self.loads(canned)
self.assertEqual(expected, got)
# There are gratuitous differences between pickles produced by
# pickle and cPickle, largely because cPickle starts PUT indices at
# 1 and pickle starts them at 0. See XXX comment in cPickle's put2() --
@ -528,26 +565,6 @@ class AbstractPickleTests(unittest.TestCase):
self.assertEqual(x[0].attr.keys(), [1])
self.assertTrue(x[0].attr[1] is x)
def test_garyp(self):
self.assertRaises(self.error, self.loads, 'garyp')
def test_insecure_strings(self):
insecure = ["abc", "2 + 2", # not quoted
#"'abc' + 'def'", # not a single quoted string
"'abc", # quote is not closed
"'abc\"", # open quote and close quote don't match
"'abc' ?", # junk after close quote
"'\\'", # trailing backslash
"'", # issue #17710
"' ", # issue #17710
# some tests of the quoting rules
#"'abc\"\''",
#"'\\\\a\'\'\'\\\'\\\\\''",
]
for s in insecure:
buf = "S" + s + "\012p0\012."
self.assertRaises(ValueError, self.loads, buf)
if have_unicode:
def test_unicode(self):
endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>',
@ -576,16 +593,6 @@ class AbstractPickleTests(unittest.TestCase):
self.assertEqual(expected, n2)
n = n >> 1
def test_maxint64(self):
maxint64 = (1L << 63) - 1
data = 'I' + str(maxint64) + '\n.'
got = self.loads(data)
self.assertEqual(got, maxint64)
# Try too with a bogus literal.
data = 'I' + str(maxint64) + 'JUNK\n.'
self.assertRaises(ValueError, self.loads, data)
def test_long(self):
for proto in protocols:
# 256 bytes is where LONG4 begins.

View File

@ -2,7 +2,8 @@ import cPickle
import cStringIO
import io
import unittest
from test.pickletester import (AbstractPickleTests,
from test.pickletester import (AbstractUnpickleTests,
AbstractPickleTests,
AbstractPickleModuleTests,
AbstractPicklerUnpicklerObjectTests,
BigmemPickleTests)
@ -40,7 +41,8 @@ class FileIOMixin:
test_support.unlink(test_support.TESTFN)
class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
class cPickleTests(AbstractUnpickleTests, AbstractPickleTests,
AbstractPickleModuleTests):
def setUp(self):
self.dumps = cPickle.dumps
@ -49,6 +51,28 @@ class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
error = cPickle.BadPickleGet
module = cPickle
class cPickleUnpicklerTests(AbstractUnpickleTests):
def loads(self, buf):
f = self.input(buf)
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
error = cPickle.BadPickleGet
class cStringIOCUnpicklerTests(cStringIOMixin, cPickleUnpicklerTests):
pass
class BytesIOCUnpicklerTests(BytesIOMixin, cPickleUnpicklerTests):
pass
class FileIOCUnpicklerTests(FileIOMixin, cPickleUnpicklerTests):
pass
class cPicklePicklerTests(AbstractPickleTests):
def dumps(self, arg, proto=0):
@ -69,8 +93,6 @@ class cPicklePicklerTests(AbstractPickleTests):
finally:
self.close(f)
error = cPickle.BadPickleGet
class cStringIOCPicklerTests(cStringIOMixin, cPicklePicklerTests):
pass
@ -129,8 +151,6 @@ class cPickleFastPicklerTests(AbstractPickleTests):
finally:
self.close(f)
error = cPickle.BadPickleGet
def test_recursive_list(self):
self.assertRaises(ValueError,
AbstractPickleTests.test_recursive_list,
@ -219,6 +239,9 @@ class cPickleDeepRecursive(unittest.TestCase):
def test_main():
test_support.run_unittest(
cPickleTests,
cStringIOCUnpicklerTests,
BytesIOCUnpicklerTests,
FileIOCUnpicklerTests,
cStringIOCPicklerTests,
BytesIOCPicklerTests,
FileIOCPicklerTests,

View File

@ -3,13 +3,15 @@ from cStringIO import StringIO
from test import test_support
from test.pickletester import (AbstractPickleTests,
from test.pickletester import (AbstractUnpickleTests,
AbstractPickleTests,
AbstractPickleModuleTests,
AbstractPersistentPicklerTests,
AbstractPicklerUnpicklerObjectTests,
BigmemPickleTests)
class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
class PickleTests(AbstractUnpickleTests, AbstractPickleTests,
AbstractPickleModuleTests):
def dumps(self, arg, proto=0, fast=0):
# Ignore fast
@ -22,10 +24,18 @@ class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
module = pickle
error = KeyError
class PicklerTests(AbstractPickleTests):
class UnpicklerTests(AbstractUnpickleTests):
error = KeyError
def loads(self, buf):
f = StringIO(buf)
u = pickle.Unpickler(f)
return u.load()
class PicklerTests(AbstractPickleTests):
def dumps(self, arg, proto=0, fast=0):
f = StringIO()
p = pickle.Pickler(f, proto)
@ -81,6 +91,7 @@ class PickleBigmemPickleTests(BigmemPickleTests):
def test_main():
test_support.run_unittest(
PickleTests,
UnpicklerTests,
PicklerTests,
PersPicklerTests,
PicklerUnpicklerObjectTests,