summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pickle.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_pickle.py')
-rw-r--r--Lib/test/test_pickle.py53
1 files changed, 34 insertions, 19 deletions
diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py
index 42fb33e..65a7adc 100644
--- a/Lib/test/test_pickle.py
+++ b/Lib/test/test_pickle.py
@@ -7,37 +7,42 @@ from test.pickletester import AbstractPickleTests
from test.pickletester import AbstractPickleModuleTests
from test.pickletester import AbstractPersistentPicklerTests
-class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
+try:
+ import _pickle
+ has_c_implementation = True
+except ImportError:
+ has_c_implementation = False
- module = pickle
- error = KeyError
- def dumps(self, arg, proto=None):
- return pickle.dumps(arg, proto)
+class PickleTests(AbstractPickleModuleTests):
+ pass
- def loads(self, buf):
- return pickle.loads(buf)
-class PicklerTests(AbstractPickleTests):
+class PyPicklerTests(AbstractPickleTests):
- error = KeyError
+ pickler = pickle._Pickler
+ unpickler = pickle._Unpickler
def dumps(self, arg, proto=None):
f = io.BytesIO()
- p = pickle.Pickler(f, proto)
+ p = self.pickler(f, proto)
p.dump(arg)
f.seek(0)
return bytes(f.read())
def loads(self, buf):
f = io.BytesIO(buf)
- u = pickle.Unpickler(f)
+ u = self.unpickler(f)
return u.load()
-class PersPicklerTests(AbstractPersistentPicklerTests):
+
+class PyPersPicklerTests(AbstractPersistentPicklerTests):
+
+ pickler = pickle._Pickler
+ unpickler = pickle._Unpickler
def dumps(self, arg, proto=None):
- class PersPickler(pickle.Pickler):
+ class PersPickler(self.pickler):
def persistent_id(subself, obj):
return self.persistent_id(obj)
f = io.BytesIO()
@@ -47,19 +52,29 @@ class PersPicklerTests(AbstractPersistentPicklerTests):
return f.read()
def loads(self, buf):
- class PersUnpickler(pickle.Unpickler):
+ class PersUnpickler(self.unpickler):
def persistent_load(subself, obj):
return self.persistent_load(obj)
f = io.BytesIO(buf)
u = PersUnpickler(f)
return u.load()
+
+if has_c_implementation:
+ class CPicklerTests(PyPicklerTests):
+ pickler = _pickle.Pickler
+ unpickler = _pickle.Unpickler
+
+ class CPersPicklerTests(PyPersPicklerTests):
+ pickler = _pickle.Pickler
+ unpickler = _pickle.Unpickler
+
+
def test_main():
- support.run_unittest(
- PickleTests,
- PicklerTests,
- PersPicklerTests
- )
+ tests = [PickleTests, PyPicklerTests, PyPersPicklerTests]
+ if has_c_implementation:
+ tests.extend([CPicklerTests, CPersPicklerTests])
+ support.run_unittest(*tests)
support.run_doctest(pickle)
if __name__ == "__main__":