2000-09-15 12:14:51 -03:00
|
|
|
import pickle
|
2007-05-08 18:26:54 -03:00
|
|
|
import io
|
2003-01-28 18:34:11 -04:00
|
|
|
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2003-01-28 18:34:11 -04:00
|
|
|
from test.pickletester import AbstractPickleTests
|
|
|
|
from test.pickletester import AbstractPickleModuleTests
|
|
|
|
from test.pickletester import AbstractPersistentPicklerTests
|
|
|
|
|
2003-02-14 23:01:11 -04:00
|
|
|
class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
|
2001-10-15 18:38:56 -03:00
|
|
|
|
2007-11-06 17:34:58 -04:00
|
|
|
module = pickle
|
|
|
|
error = KeyError
|
|
|
|
|
2008-03-17 19:56:06 -03:00
|
|
|
def dumps(self, arg, proto=None):
|
2003-01-29 13:58:45 -04:00
|
|
|
return pickle.dumps(arg, proto)
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
return pickle.loads(buf)
|
2001-10-18 18:57:37 -03:00
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
class PicklerTests(AbstractPickleTests):
|
|
|
|
|
|
|
|
error = KeyError
|
|
|
|
|
2008-03-17 19:56:06 -03:00
|
|
|
def dumps(self, arg, proto=None):
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO()
|
2003-01-27 23:51:53 -04:00
|
|
|
p = pickle.Pickler(f, proto)
|
2001-10-15 18:38:56 -03:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
2007-05-08 18:26:54 -03:00
|
|
|
return bytes(f.read())
|
2001-10-15 18:38:56 -03:00
|
|
|
|
|
|
|
def loads(self, buf):
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO(buf)
|
2001-10-15 18:38:56 -03:00
|
|
|
u = pickle.Unpickler(f)
|
|
|
|
return u.load()
|
|
|
|
|
2002-11-13 18:01:27 -04:00
|
|
|
class PersPicklerTests(AbstractPersistentPicklerTests):
|
|
|
|
|
2008-03-17 19:56:06 -03:00
|
|
|
def dumps(self, arg, proto=None):
|
2002-11-13 18:01:27 -04:00
|
|
|
class PersPickler(pickle.Pickler):
|
|
|
|
def persistent_id(subself, obj):
|
|
|
|
return self.persistent_id(obj)
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO()
|
2003-01-27 23:51:53 -04:00
|
|
|
p = PersPickler(f, proto)
|
2002-11-13 18:01:27 -04:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
class PersUnpickler(pickle.Unpickler):
|
|
|
|
def persistent_load(subself, obj):
|
|
|
|
return self.persistent_load(obj)
|
2007-05-08 18:26:54 -03:00
|
|
|
f = io.BytesIO(buf)
|
2002-11-13 18:01:27 -04:00
|
|
|
u = PersUnpickler(f)
|
|
|
|
return u.load()
|
|
|
|
|
2001-12-19 12:42:15 -04:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(
|
|
|
|
PickleTests,
|
|
|
|
PicklerTests,
|
|
|
|
PersPicklerTests
|
|
|
|
)
|
2003-01-30 17:27:37 -04:00
|
|
|
test_support.run_doctest(pickle)
|
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()
|