2000-09-15 23:14:51 +08:00
|
|
|
import pickle
|
2007-05-09 05:26:54 +08:00
|
|
|
import io
|
2003-01-29 06:34:11 +08:00
|
|
|
|
2008-05-21 05:35:26 +08:00
|
|
|
from test import support
|
2001-10-16 05:38:56 +08:00
|
|
|
|
2003-01-29 06:34:11 +08:00
|
|
|
from test.pickletester import AbstractPickleTests
|
|
|
|
from test.pickletester import AbstractPickleModuleTests
|
|
|
|
from test.pickletester import AbstractPersistentPicklerTests
|
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
try:
|
|
|
|
import _pickle
|
|
|
|
has_c_implementation = True
|
|
|
|
except ImportError:
|
|
|
|
has_c_implementation = False
|
2001-10-16 05:38:56 +08:00
|
|
|
|
2007-11-07 05:34:58 +08:00
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
class PickleTests(AbstractPickleModuleTests):
|
|
|
|
pass
|
2003-01-30 01:58:45 +08:00
|
|
|
|
2001-10-19 05:57:37 +08:00
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
class PyPicklerTests(AbstractPickleTests):
|
2001-10-16 05:38:56 +08:00
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
2001-10-16 05:38:56 +08:00
|
|
|
|
2008-03-18 06:56:06 +08:00
|
|
|
def dumps(self, arg, proto=None):
|
2007-05-09 05:26:54 +08:00
|
|
|
f = io.BytesIO()
|
2008-06-13 02:26:05 +08:00
|
|
|
p = self.pickler(f, proto)
|
2001-10-16 05:38:56 +08:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
2007-05-09 05:26:54 +08:00
|
|
|
return bytes(f.read())
|
2001-10-16 05:38:56 +08:00
|
|
|
|
|
|
|
def loads(self, buf):
|
2007-05-09 05:26:54 +08:00
|
|
|
f = io.BytesIO(buf)
|
2008-06-13 02:26:05 +08:00
|
|
|
u = self.unpickler(f)
|
2001-10-16 05:38:56 +08:00
|
|
|
return u.load()
|
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
|
|
|
|
class PyPersPicklerTests(AbstractPersistentPicklerTests):
|
|
|
|
|
|
|
|
pickler = pickle._Pickler
|
|
|
|
unpickler = pickle._Unpickler
|
2002-11-14 06:01:27 +08:00
|
|
|
|
2008-03-18 06:56:06 +08:00
|
|
|
def dumps(self, arg, proto=None):
|
2008-06-13 02:26:05 +08:00
|
|
|
class PersPickler(self.pickler):
|
2002-11-14 06:01:27 +08:00
|
|
|
def persistent_id(subself, obj):
|
|
|
|
return self.persistent_id(obj)
|
2007-05-09 05:26:54 +08:00
|
|
|
f = io.BytesIO()
|
2003-01-28 11:51:53 +08:00
|
|
|
p = PersPickler(f, proto)
|
2002-11-14 06:01:27 +08:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def loads(self, buf):
|
2008-06-13 02:26:05 +08:00
|
|
|
class PersUnpickler(self.unpickler):
|
2002-11-14 06:01:27 +08:00
|
|
|
def persistent_load(subself, obj):
|
|
|
|
return self.persistent_load(obj)
|
2007-05-09 05:26:54 +08:00
|
|
|
f = io.BytesIO(buf)
|
2002-11-14 06:01:27 +08:00
|
|
|
u = PersUnpickler(f)
|
|
|
|
return u.load()
|
|
|
|
|
2008-06-13 02:26:05 +08:00
|
|
|
|
|
|
|
if has_c_implementation:
|
|
|
|
class CPicklerTests(PyPicklerTests):
|
|
|
|
pickler = _pickle.Pickler
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
|
|
|
|
class CPersPicklerTests(PyPersPicklerTests):
|
|
|
|
pickler = _pickle.Pickler
|
|
|
|
unpickler = _pickle.Unpickler
|
|
|
|
|
|
|
|
|
2001-12-20 00:42:15 +08:00
|
|
|
def test_main():
|
2008-06-13 02:26:05 +08:00
|
|
|
tests = [PickleTests, PyPicklerTests, PyPersPicklerTests]
|
|
|
|
if has_c_implementation:
|
|
|
|
tests.extend([CPicklerTests, CPersPicklerTests])
|
|
|
|
support.run_unittest(*tests)
|
2008-05-21 05:35:26 +08:00
|
|
|
support.run_doctest(pickle)
|
2001-12-20 00:42:15 +08:00
|
|
|
|
2001-10-16 05:38:56 +08:00
|
|
|
if __name__ == "__main__":
|
2001-12-20 00:42:15 +08:00
|
|
|
test_main()
|