Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 1 | import pickle |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 2 | import unittest |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 3 | import io |
Tim Peters | 47a6b13 | 2003-01-28 22:34:11 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 5 | from test import test_support |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 6 | |
Tim Peters | 47a6b13 | 2003-01-28 22:34:11 +0000 | [diff] [blame] | 7 | from test.pickletester import AbstractPickleTests |
Tim Peters | 47a6b13 | 2003-01-28 22:34:11 +0000 | [diff] [blame] | 8 | from test.pickletester import AbstractPickleModuleTests |
| 9 | from test.pickletester import AbstractPersistentPicklerTests |
| 10 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 11 | class PickleTests(AbstractPickleTests, AbstractPickleModuleTests): |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13 | 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 Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 20 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 21 | module = pickle |
| 22 | error = KeyError |
| 23 | |
| 24 | class PicklerTests(AbstractPickleTests): |
| 25 | |
| 26 | error = KeyError |
| 27 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 28 | def dumps(self, arg, proto=0, fast=0): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 29 | f = io.BytesIO() |
Guido van Rossum | 9d32bb1 | 2003-01-28 03:51:53 +0000 | [diff] [blame] | 30 | p = pickle.Pickler(f, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 31 | if fast: |
| 32 | p.fast = fast |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 33 | p.dump(arg) |
| 34 | f.seek(0) |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 35 | return bytes(f.read()) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 36 | |
| 37 | def loads(self, buf): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 38 | f = io.BytesIO(buf) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 39 | u = pickle.Unpickler(f) |
| 40 | return u.load() |
| 41 | |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 42 | class PersPicklerTests(AbstractPersistentPicklerTests): |
| 43 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 44 | def dumps(self, arg, proto=0, fast=0): |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 45 | class PersPickler(pickle.Pickler): |
| 46 | def persistent_id(subself, obj): |
| 47 | return self.persistent_id(obj) |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 48 | f = io.BytesIO() |
Guido van Rossum | 9d32bb1 | 2003-01-28 03:51:53 +0000 | [diff] [blame] | 49 | p = PersPickler(f, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 50 | if fast: |
| 51 | p.fast = fast |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 52 | 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 Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 60 | f = io.BytesIO(buf) |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 61 | u = PersUnpickler(f) |
| 62 | return u.load() |
| 63 | |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 64 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 65 | test_support.run_unittest( |
| 66 | PickleTests, |
| 67 | PicklerTests, |
| 68 | PersPicklerTests |
| 69 | ) |
Tim Peters | e14295c | 2003-01-30 21:27:37 +0000 | [diff] [blame] | 70 | test_support.run_doctest(pickle) |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 71 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 72 | if __name__ == "__main__": |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 73 | test_main() |