diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2008-06-11 22:43:06 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2008-06-11 22:43:06 (GMT) |
commit | cc313061a5b45c12e9772e2f9f2ae623fc00e31d (patch) | |
tree | 2671b4f3883c561c60e1578d4b4e86590842b79e /Lib/test | |
parent | 1e637b7373c6a183524b2bfa16d7702ef451e632 (diff) | |
download | cpython-cc313061a5b45c12e9772e2f9f2ae623fc00e31d.zip cpython-cc313061a5b45c12e9772e2f9f2ae623fc00e31d.tar.gz cpython-cc313061a5b45c12e9772e2f9f2ae623fc00e31d.tar.bz2 |
Issue 2917: Merge the pickle and cPickle module.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/pickletester.py | 25 | ||||
-rw-r--r-- | Lib/test/test_pickle.py | 53 | ||||
-rw-r--r-- | Lib/test/test_pickletools.py | 2 |
3 files changed, 47 insertions, 33 deletions
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 783d361..c9ebdb8 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -362,7 +362,7 @@ def create_data(): return x class AbstractPickleTests(unittest.TestCase): - # Subclass must define self.dumps, self.loads, self.error. + # Subclass must define self.dumps, self.loads. _testdata = create_data() @@ -463,8 +463,9 @@ class AbstractPickleTests(unittest.TestCase): self.assertEqual(list(x[0].attr.keys()), [1]) self.assert_(x[0].attr[1] is x) - def test_garyp(self): - self.assertRaises(self.error, self.loads, b'garyp') + def test_get(self): + self.assertRaises(KeyError, self.loads, b'g0\np0') + self.assertEquals(self.loads(b'((Kdtp0\nh\x00l.))'), [(100,), (100,)]) def test_insecure_strings(self): # XXX Some of these tests are temporarily disabled @@ -955,7 +956,7 @@ class AbstractPickleModuleTests(unittest.TestCase): f = open(TESTFN, "wb") try: f.close() - self.assertRaises(ValueError, self.module.dump, 123, f) + self.assertRaises(ValueError, pickle.dump, 123, f) finally: os.remove(TESTFN) @@ -964,24 +965,24 @@ class AbstractPickleModuleTests(unittest.TestCase): f = open(TESTFN, "wb") try: f.close() - self.assertRaises(ValueError, self.module.dump, 123, f) + self.assertRaises(ValueError, pickle.dump, 123, f) finally: os.remove(TESTFN) def test_highest_protocol(self): # Of course this needs to be changed when HIGHEST_PROTOCOL changes. - self.assertEqual(self.module.HIGHEST_PROTOCOL, 3) + self.assertEqual(pickle.HIGHEST_PROTOCOL, 3) def test_callapi(self): from io import BytesIO f = BytesIO() # With and without keyword arguments - self.module.dump(123, f, -1) - self.module.dump(123, file=f, protocol=-1) - self.module.dumps(123, -1) - self.module.dumps(123, protocol=-1) - self.module.Pickler(f, -1) - self.module.Pickler(f, protocol=-1) + pickle.dump(123, f, -1) + pickle.dump(123, file=f, protocol=-1) + pickle.dumps(123, -1) + pickle.dumps(123, protocol=-1) + pickle.Pickler(f, -1) + pickle.Pickler(f, protocol=-1) class AbstractPersistentPicklerTests(unittest.TestCase): 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__": diff --git a/Lib/test/test_pickletools.py b/Lib/test/test_pickletools.py index ccb6c9c..3e701b0 100644 --- a/Lib/test/test_pickletools.py +++ b/Lib/test/test_pickletools.py @@ -12,8 +12,6 @@ class OptimizedPickleTests(AbstractPickleTests, AbstractPickleModuleTests): def loads(self, buf): return pickle.loads(buf) - module = pickle - error = KeyError def test_main(): support.run_unittest(OptimizedPickleTests) |