2013-03-14 15:59:09 -03:00
|
|
|
import cPickle
|
|
|
|
import cStringIO
|
|
|
|
import io
|
2015-11-07 05:15:32 -04:00
|
|
|
import functools
|
2013-03-14 15:59:09 -03:00
|
|
|
import unittest
|
2015-09-29 09:36:28 -03:00
|
|
|
from test.pickletester import (AbstractUnpickleTests,
|
|
|
|
AbstractPickleTests,
|
2013-02-12 15:36:47 -04:00
|
|
|
AbstractPickleModuleTests,
|
|
|
|
AbstractPicklerUnpicklerObjectTests,
|
|
|
|
BigmemPickleTests)
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2013-03-14 15:59:09 -03:00
|
|
|
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):
|
2013-03-18 01:28:29 -03:00
|
|
|
return open(test_support.TESTFN, 'wb+')
|
2013-03-14 15:59:09 -03:00
|
|
|
|
|
|
|
def input(self, data):
|
2013-03-18 01:28:29 -03:00
|
|
|
f = open(test_support.TESTFN, 'wb+')
|
2013-03-14 15:59:09 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2015-09-29 09:36:28 -03:00
|
|
|
class cPickleTests(AbstractUnpickleTests, 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
|
2015-11-23 09:20:43 -04:00
|
|
|
bad_stack_errors = (cPickle.UnpicklingError,)
|
2015-11-29 07:12:40 -04:00
|
|
|
bad_mark_errors = (EOFError,)
|
|
|
|
truncated_errors = (cPickle.UnpicklingError, EOFError,
|
|
|
|
AttributeError, ValueError)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2015-09-29 09:36:28 -03:00
|
|
|
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
|
2015-11-23 09:20:43 -04:00
|
|
|
bad_stack_errors = (cPickle.UnpicklingError,)
|
2015-11-29 07:12:40 -04:00
|
|
|
bad_mark_errors = (EOFError,)
|
|
|
|
truncated_errors = (cPickle.UnpicklingError, EOFError,
|
|
|
|
AttributeError, ValueError)
|
2015-09-29 09:36:28 -03:00
|
|
|
|
|
|
|
class cStringIOCUnpicklerTests(cStringIOMixin, cPickleUnpicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BytesIOCUnpicklerTests(BytesIOMixin, cPickleUnpicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileIOCUnpicklerTests(FileIOMixin, cPickleUnpicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
class cPicklePicklerTests(AbstractPickleTests):
|
|
|
|
|
2003-01-28 18:26:28 -04:00
|
|
|
def dumps(self, arg, proto=0):
|
2013-03-14 15:59:09 -03:00
|
|
|
f = self.output()
|
|
|
|
try:
|
|
|
|
p = cPickle.Pickler(f, proto)
|
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
finally:
|
|
|
|
self.close(f)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
|
|
|
def loads(self, buf):
|
2013-03-14 15:59:09 -03:00
|
|
|
f = self.input(buf)
|
|
|
|
try:
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
finally:
|
|
|
|
self.close(f)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2013-03-14 15:59:09 -03:00
|
|
|
class cStringIOCPicklerTests(cStringIOMixin, cPicklePicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BytesIOCPicklerTests(BytesIOMixin, cPicklePicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileIOCPicklerTests(FileIOMixin, cPicklePicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
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):
|
2013-03-14 15:59:09 -03:00
|
|
|
f = self.input(args[0])
|
|
|
|
try:
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
finally:
|
|
|
|
self.close(f)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
|
|
|
error = cPickle.BadPickleGet
|
|
|
|
|
2013-03-14 15:59:09 -03:00
|
|
|
class cStringIOCPicklerListTests(cStringIOMixin, cPickleListPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BytesIOCPicklerListTests(BytesIOMixin, cPickleListPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileIOCPicklerListTests(FileIOMixin, cPickleListPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
class cPickleFastPicklerTests(AbstractPickleTests):
|
|
|
|
|
2003-01-28 18:26:28 -04:00
|
|
|
def dumps(self, arg, proto=0):
|
2013-03-14 15:59:09 -03:00
|
|
|
f = self.output()
|
|
|
|
try:
|
|
|
|
p = cPickle.Pickler(f, proto)
|
|
|
|
p.fast = 1
|
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
finally:
|
|
|
|
self.close(f)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
|
|
|
def loads(self, *args):
|
2013-03-14 15:59:09 -03:00
|
|
|
f = self.input(args[0])
|
|
|
|
try:
|
|
|
|
p = cPickle.Unpickler(f)
|
|
|
|
return p.load()
|
|
|
|
finally:
|
|
|
|
self.close(f)
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2001-12-21 16:04:22 -04:00
|
|
|
def test_nonrecursive_deep(self):
|
2003-02-21 16:14:35 -04:00
|
|
|
# If it's not cyclic, it should pickle OK even if the nesting
|
|
|
|
# depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be
|
|
|
|
# 50 today. Jack Jansen reported stack overflow on Mac OS 9
|
|
|
|
# at 64.
|
2001-12-21 16:04:22 -04:00
|
|
|
a = []
|
2003-02-21 16:14:35 -04:00
|
|
|
for i in range(60):
|
2001-12-21 16:04:22 -04:00
|
|
|
a = [a]
|
|
|
|
b = self.loads(self.dumps(a))
|
|
|
|
self.assertEqual(a, b)
|
|
|
|
|
2015-11-07 05:15:32 -04:00
|
|
|
for name in dir(AbstractPickleTests):
|
|
|
|
if name.startswith('test_recursive_'):
|
|
|
|
func = getattr(AbstractPickleTests, name)
|
|
|
|
if '_subclass' in name and '_and_inst' not in name:
|
|
|
|
assert_args = RuntimeError, 'maximum recursion depth exceeded'
|
|
|
|
else:
|
|
|
|
assert_args = ValueError, "can't pickle cyclic objects"
|
|
|
|
def wrapper(self, func=func, assert_args=assert_args):
|
|
|
|
with self.assertRaisesRegexp(*assert_args):
|
|
|
|
func(self)
|
|
|
|
functools.update_wrapper(wrapper, func)
|
|
|
|
setattr(cPickleFastPicklerTests, name, wrapper)
|
|
|
|
|
2013-03-14 15:59:09 -03:00
|
|
|
class cStringIOCPicklerFastTests(cStringIOMixin, cPickleFastPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BytesIOCPicklerFastTests(BytesIOMixin, cPickleFastPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileIOCPicklerFastTests(FileIOMixin, cPickleFastPicklerTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2009-04-09 13:46:46 -03:00
|
|
|
class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
|
|
|
|
|
|
|
|
pickler_class = cPickle.Pickler
|
|
|
|
unpickler_class = cPickle.Unpickler
|
|
|
|
|
2013-02-12 15:36:47 -04:00
|
|
|
class cPickleBigmemPickleTests(BigmemPickleTests):
|
|
|
|
|
|
|
|
def dumps(self, arg, proto=0, fast=0):
|
|
|
|
# Ignore fast
|
|
|
|
return cPickle.dumps(arg, proto)
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
# Ignore fast
|
|
|
|
return cPickle.loads(buf)
|
|
|
|
|
2009-04-09 13:46:46 -03:00
|
|
|
|
2008-06-22 20:19:14 -03:00
|
|
|
class Node(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class cPickleDeepRecursive(unittest.TestCase):
|
2008-06-29 22:10:55 -03:00
|
|
|
def test_issue2702(self):
|
|
|
|
# This should raise a RecursionLimit but in some
|
|
|
|
# platforms (FreeBSD, win32) sometimes raises KeyError instead,
|
|
|
|
# or just silently terminates the interpreter (=crashes).
|
|
|
|
nodes = [Node() for i in range(500)]
|
|
|
|
for n in nodes:
|
|
|
|
n.connections = list(nodes)
|
|
|
|
n.connections.remove(n)
|
2009-03-22 19:24:58 -03:00
|
|
|
self.assertRaises((AttributeError, RuntimeError), cPickle.dumps, n)
|
2008-06-25 16:24:53 -03:00
|
|
|
|
|
|
|
def test_issue3179(self):
|
2008-06-29 22:10:55 -03:00
|
|
|
# Safe test, because I broke this case when fixing the
|
|
|
|
# behaviour for the previous test.
|
2008-06-25 16:24:53 -03:00
|
|
|
res=[]
|
|
|
|
for x in range(1,2000):
|
|
|
|
res.append(dict(doc=x, similar=[]))
|
|
|
|
cPickle.dumps(res)
|
|
|
|
|
2008-06-22 20:19:14 -03:00
|
|
|
|
2001-12-19 12:42:15 -04:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(
|
|
|
|
cPickleTests,
|
2015-09-29 09:36:28 -03:00
|
|
|
cStringIOCUnpicklerTests,
|
|
|
|
BytesIOCUnpicklerTests,
|
|
|
|
FileIOCUnpicklerTests,
|
2013-03-14 15:59:09 -03:00
|
|
|
cStringIOCPicklerTests,
|
|
|
|
BytesIOCPicklerTests,
|
|
|
|
FileIOCPicklerTests,
|
|
|
|
cStringIOCPicklerListTests,
|
|
|
|
BytesIOCPicklerListTests,
|
|
|
|
FileIOCPicklerListTests,
|
|
|
|
cStringIOCPicklerFastTests,
|
|
|
|
BytesIOCPicklerFastTests,
|
|
|
|
FileIOCPicklerFastTests,
|
2008-06-22 20:19:14 -03:00
|
|
|
cPickleDeepRecursive,
|
2009-04-09 13:46:46 -03:00
|
|
|
cPicklePicklerUnpicklerObjectTests,
|
2013-02-12 15:36:47 -04:00
|
|
|
cPickleBigmemPickleTests,
|
2003-05-01 14:45:56 -03:00
|
|
|
)
|
2001-12-19 12:42:15 -04:00
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
if __name__ == "__main__":
|
2001-12-19 12:42:15 -04:00
|
|
|
test_main()
|