Guido van Rossum | aa3828a | 1999-03-25 22:38:49 +0000 | [diff] [blame] | 1 | import cPickle |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 2 | import unittest |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 3 | from cStringIO import StringIO |
Neal Norwitz | cdb7948 | 2006-03-24 08:58:38 +0000 | [diff] [blame] | 4 | from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests |
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 | |
| 7 | class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 8 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 9 | def setUp(self): |
| 10 | self.dumps = cPickle.dumps |
| 11 | self.loads = cPickle.loads |
| 12 | |
| 13 | error = cPickle.BadPickleGet |
| 14 | module = cPickle |
| 15 | |
| 16 | class cPicklePicklerTests(AbstractPickleTests): |
| 17 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 18 | def dumps(self, arg, proto=0): |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 19 | f = StringIO() |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 20 | p = cPickle.Pickler(f, proto) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 21 | p.dump(arg) |
| 22 | f.seek(0) |
| 23 | return f.read() |
| 24 | |
| 25 | def loads(self, buf): |
| 26 | f = StringIO(buf) |
| 27 | p = cPickle.Unpickler(f) |
| 28 | return p.load() |
| 29 | |
| 30 | error = cPickle.BadPickleGet |
| 31 | |
| 32 | class cPickleListPicklerTests(AbstractPickleTests): |
| 33 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 34 | def dumps(self, arg, proto=0): |
| 35 | p = cPickle.Pickler(proto) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 36 | p.dump(arg) |
| 37 | return p.getvalue() |
| 38 | |
| 39 | def loads(self, *args): |
| 40 | f = StringIO(args[0]) |
| 41 | p = cPickle.Unpickler(f) |
| 42 | return p.load() |
| 43 | |
| 44 | error = cPickle.BadPickleGet |
| 45 | |
| 46 | class cPickleFastPicklerTests(AbstractPickleTests): |
| 47 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 48 | def dumps(self, arg, proto=0): |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 49 | f = StringIO() |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 50 | p = cPickle.Pickler(f, proto) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 51 | p.fast = 1 |
| 52 | p.dump(arg) |
| 53 | f.seek(0) |
| 54 | return f.read() |
| 55 | |
| 56 | def loads(self, *args): |
| 57 | f = StringIO(args[0]) |
| 58 | p = cPickle.Unpickler(f) |
| 59 | return p.load() |
| 60 | |
| 61 | error = cPickle.BadPickleGet |
| 62 | |
| 63 | def test_recursive_list(self): |
| 64 | self.assertRaises(ValueError, |
| 65 | AbstractPickleTests.test_recursive_list, |
| 66 | self) |
| 67 | |
| 68 | def test_recursive_inst(self): |
| 69 | self.assertRaises(ValueError, |
| 70 | AbstractPickleTests.test_recursive_inst, |
| 71 | self) |
| 72 | |
| 73 | def test_recursive_dict(self): |
| 74 | self.assertRaises(ValueError, |
| 75 | AbstractPickleTests.test_recursive_dict, |
| 76 | self) |
| 77 | |
| 78 | def test_recursive_multi(self): |
| 79 | self.assertRaises(ValueError, |
| 80 | AbstractPickleTests.test_recursive_multi, |
| 81 | self) |
| 82 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 83 | def test_nonrecursive_deep(self): |
Tim Peters | 7107a7f | 2003-02-21 20:14:35 +0000 | [diff] [blame] | 84 | # If it's not cyclic, it should pickle OK even if the nesting |
| 85 | # depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be |
| 86 | # 50 today. Jack Jansen reported stack overflow on Mac OS 9 |
| 87 | # at 64. |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 88 | a = [] |
Tim Peters | 7107a7f | 2003-02-21 20:14:35 +0000 | [diff] [blame] | 89 | for i in range(60): |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 90 | a = [a] |
| 91 | b = self.loads(self.dumps(a)) |
| 92 | self.assertEqual(a, b) |
| 93 | |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 94 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 95 | test_support.run_unittest( |
| 96 | cPickleTests, |
| 97 | cPicklePicklerTests, |
| 98 | cPickleListPicklerTests, |
| 99 | cPickleFastPicklerTests |
| 100 | ) |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 101 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 102 | if __name__ == "__main__": |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 103 | test_main() |