2000-09-15 12:14:51 -03:00
|
|
|
import pickle
|
2001-12-19 12:42:15 -04:00
|
|
|
import unittest
|
2001-10-15 18:38:56 -03:00
|
|
|
from cStringIO import StringIO
|
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
|
|
|
|
2003-01-29 13:58:45 -04:00
|
|
|
def dumps(self, arg, proto=0, fast=0):
|
|
|
|
# Ignore fast
|
|
|
|
return pickle.dumps(arg, proto)
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
# Ignore fast
|
|
|
|
return pickle.loads(buf)
|
2001-10-18 18:57:37 -03:00
|
|
|
|
2001-10-15 18:38:56 -03:00
|
|
|
module = pickle
|
|
|
|
error = KeyError
|
|
|
|
|
|
|
|
class PicklerTests(AbstractPickleTests):
|
|
|
|
|
|
|
|
error = KeyError
|
|
|
|
|
2003-01-29 13:58:45 -04:00
|
|
|
def dumps(self, arg, proto=0, fast=0):
|
2001-10-15 18:38:56 -03:00
|
|
|
f = StringIO()
|
2003-01-27 23:51:53 -04:00
|
|
|
p = pickle.Pickler(f, proto)
|
2003-01-29 13:58:45 -04:00
|
|
|
if fast:
|
|
|
|
p.fast = fast
|
2001-10-15 18:38:56 -03:00
|
|
|
p.dump(arg)
|
|
|
|
f.seek(0)
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
def loads(self, buf):
|
|
|
|
f = StringIO(buf)
|
|
|
|
u = pickle.Unpickler(f)
|
|
|
|
return u.load()
|
|
|
|
|
2002-11-13 18:01:27 -04:00
|
|
|
class PersPicklerTests(AbstractPersistentPicklerTests):
|
|
|
|
|
2003-01-29 13:58:45 -04:00
|
|
|
def dumps(self, arg, proto=0, fast=0):
|
2002-11-13 18:01:27 -04:00
|
|
|
class PersPickler(pickle.Pickler):
|
|
|
|
def persistent_id(subself, obj):
|
|
|
|
return self.persistent_id(obj)
|
|
|
|
f = StringIO()
|
2003-01-27 23:51:53 -04:00
|
|
|
p = PersPickler(f, proto)
|
2003-01-29 13:58:45 -04:00
|
|
|
if fast:
|
|
|
|
p.fast = fast
|
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)
|
|
|
|
f = StringIO(buf)
|
|
|
|
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()
|