blob: 11254f4d8e9b97a0a1fbe697da6eafd6b3fa4900 [file] [log] [blame]
Jeremy Hyltonbe467e52000-09-15 15:14:51 +00001import pickle
Fred Drake694ed092001-12-19 16:42:15 +00002import unittest
Guido van Rossumcfe5f202007-05-08 21:26:54 +00003import io
Tim Peters47a6b132003-01-28 22:34:11 +00004
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Jeremy Hylton66426532001-10-15 21:38:56 +00006
Tim Peters47a6b132003-01-28 22:34:11 +00007from test.pickletester import AbstractPickleTests
Tim Peters47a6b132003-01-28 22:34:11 +00008from test.pickletester import AbstractPickleModuleTests
9from test.pickletester import AbstractPersistentPicklerTests
10
Tim Peters080c88b2003-02-15 03:01:11 +000011class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
Jeremy Hylton66426532001-10-15 21:38:56 +000012
Guido van Rossum5d9113d2003-01-29 17:58:45 +000013 def dumps(self, arg, proto=0, fast=0):
14 # Ignore fast
15 return pickle.dumps(arg, proto)
16
17 def loads(self, buf):
18 # Ignore fast
19 return pickle.loads(buf)
Tim Peterse0c446b2001-10-18 21:57:37 +000020
Jeremy Hylton66426532001-10-15 21:38:56 +000021 module = pickle
22 error = KeyError
23
24class PicklerTests(AbstractPickleTests):
25
26 error = KeyError
27
Guido van Rossum5d9113d2003-01-29 17:58:45 +000028 def dumps(self, arg, proto=0, fast=0):
Guido van Rossumcfe5f202007-05-08 21:26:54 +000029 f = io.BytesIO()
Guido van Rossum9d32bb12003-01-28 03:51:53 +000030 p = pickle.Pickler(f, proto)
Guido van Rossum5d9113d2003-01-29 17:58:45 +000031 if fast:
32 p.fast = fast
Jeremy Hylton66426532001-10-15 21:38:56 +000033 p.dump(arg)
34 f.seek(0)
Guido van Rossumcfe5f202007-05-08 21:26:54 +000035 return bytes(f.read())
Jeremy Hylton66426532001-10-15 21:38:56 +000036
37 def loads(self, buf):
Guido van Rossumcfe5f202007-05-08 21:26:54 +000038 f = io.BytesIO(buf)
Jeremy Hylton66426532001-10-15 21:38:56 +000039 u = pickle.Unpickler(f)
40 return u.load()
41
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +000042class PersPicklerTests(AbstractPersistentPicklerTests):
43
Guido van Rossum5d9113d2003-01-29 17:58:45 +000044 def dumps(self, arg, proto=0, fast=0):
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +000045 class PersPickler(pickle.Pickler):
46 def persistent_id(subself, obj):
47 return self.persistent_id(obj)
Guido van Rossumcfe5f202007-05-08 21:26:54 +000048 f = io.BytesIO()
Guido van Rossum9d32bb12003-01-28 03:51:53 +000049 p = PersPickler(f, proto)
Guido van Rossum5d9113d2003-01-29 17:58:45 +000050 if fast:
51 p.fast = fast
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +000052 p.dump(arg)
53 f.seek(0)
54 return f.read()
55
56 def loads(self, buf):
57 class PersUnpickler(pickle.Unpickler):
58 def persistent_load(subself, obj):
59 return self.persistent_load(obj)
Guido van Rossumcfe5f202007-05-08 21:26:54 +000060 f = io.BytesIO(buf)
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +000061 u = PersUnpickler(f)
62 return u.load()
63
Fred Drake694ed092001-12-19 16:42:15 +000064def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000065 test_support.run_unittest(
66 PickleTests,
67 PicklerTests,
68 PersPicklerTests
69 )
Tim Peterse14295c2003-01-30 21:27:37 +000070 test_support.run_doctest(pickle)
Fred Drake694ed092001-12-19 16:42:15 +000071
Jeremy Hylton66426532001-10-15 21:38:56 +000072if __name__ == "__main__":
Fred Drake694ed092001-12-19 16:42:15 +000073 test_main()