| 
									
										
										
										
											2008-06-22 23:19:14 +00:00
										 |  |  | import cPickle, unittest | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  | from cStringIO import StringIO | 
					
						
							| 
									
										
										
										
											2006-03-24 08:58:38 +00:00
										 |  |  | from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): | 
					
						
							| 
									
										
										
										
											2001-10-18 21:57:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.dumps = cPickle.dumps | 
					
						
							|  |  |  |         self.loads = cPickle.loads | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     error = cPickle.BadPickleGet | 
					
						
							|  |  |  |     module = cPickle | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class cPicklePicklerTests(AbstractPickleTests): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-28 22:26:28 +00:00
										 |  |  |     def dumps(self, arg, proto=0): | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |         f = StringIO() | 
					
						
							| 
									
										
										
										
											2003-01-28 22:26:28 +00:00
										 |  |  |         p = cPickle.Pickler(f, proto) | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |         p.dump(arg) | 
					
						
							|  |  |  |         f.seek(0) | 
					
						
							|  |  |  |         return f.read() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def loads(self, buf): | 
					
						
							|  |  |  |         f = StringIO(buf) | 
					
						
							|  |  |  |         p = cPickle.Unpickler(f) | 
					
						
							|  |  |  |         return p.load() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     error = cPickle.BadPickleGet | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class cPickleListPicklerTests(AbstractPickleTests): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-28 22:26:28 +00:00
										 |  |  |     def dumps(self, arg, proto=0): | 
					
						
							|  |  |  |         p = cPickle.Pickler(proto) | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |         p.dump(arg) | 
					
						
							|  |  |  |         return p.getvalue() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def loads(self, *args): | 
					
						
							|  |  |  |         f = StringIO(args[0]) | 
					
						
							|  |  |  |         p = cPickle.Unpickler(f) | 
					
						
							|  |  |  |         return p.load() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     error = cPickle.BadPickleGet | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class cPickleFastPicklerTests(AbstractPickleTests): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-28 22:26:28 +00:00
										 |  |  |     def dumps(self, arg, proto=0): | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |         f = StringIO() | 
					
						
							| 
									
										
										
										
											2003-01-28 22:26:28 +00:00
										 |  |  |         p = cPickle.Pickler(f, proto) | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  |         p.fast = 1 | 
					
						
							|  |  |  |         p.dump(arg) | 
					
						
							|  |  |  |         f.seek(0) | 
					
						
							|  |  |  |         return f.read() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def loads(self, *args): | 
					
						
							|  |  |  |         f = StringIO(args[0]) | 
					
						
							|  |  |  |         p = cPickle.Unpickler(f) | 
					
						
							|  |  |  |         return p.load() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     error = cPickle.BadPickleGet | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_recursive_list(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, | 
					
						
							|  |  |  |                           AbstractPickleTests.test_recursive_list, | 
					
						
							|  |  |  |                           self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_recursive_inst(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, | 
					
						
							|  |  |  |                           AbstractPickleTests.test_recursive_inst, | 
					
						
							|  |  |  |                           self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_recursive_dict(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, | 
					
						
							|  |  |  |                           AbstractPickleTests.test_recursive_dict, | 
					
						
							|  |  |  |                           self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_recursive_multi(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, | 
					
						
							|  |  |  |                           AbstractPickleTests.test_recursive_multi, | 
					
						
							|  |  |  |                           self) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-21 20:04:22 +00:00
										 |  |  |     def test_nonrecursive_deep(self): | 
					
						
							| 
									
										
										
										
											2003-02-21 20:14:35 +00:00
										 |  |  |         # If it's not cyclic, it should pickle OK even if the nesting | 
					
						
							|  |  |  |         # depth exceeds PY_CPICKLE_FAST_LIMIT.  That happens to be | 
					
						
							|  |  |  |         # 50 today.  Jack Jansen reported stack overflow on Mac OS 9 | 
					
						
							|  |  |  |         # at 64. | 
					
						
							| 
									
										
										
										
											2001-12-21 20:04:22 +00:00
										 |  |  |         a = [] | 
					
						
							| 
									
										
										
										
											2003-02-21 20:14:35 +00:00
										 |  |  |         for i in range(60): | 
					
						
							| 
									
										
										
										
											2001-12-21 20:04:22 +00:00
										 |  |  |             a = [a] | 
					
						
							|  |  |  |         b = self.loads(self.dumps(a)) | 
					
						
							|  |  |  |         self.assertEqual(a, b) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-22 23:19:14 +00:00
										 |  |  | class Node(object): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class cPickleDeepRecursive(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2008-06-30 01:10:55 +00:00
										 |  |  |     def test_issue2702(self): | 
					
						
							|  |  |  |         # This should raise a RecursionLimit but in some | 
					
						
							|  |  |  |         # platforms (FreeBSD, win32) sometimes raises KeyError instead, | 
					
						
							|  |  |  |         # or just silently terminates the interpreter (=crashes). | 
					
						
							|  |  |  |         nodes = [Node() for i in range(500)] | 
					
						
							|  |  |  |         for n in nodes: | 
					
						
							|  |  |  |             n.connections = list(nodes) | 
					
						
							|  |  |  |             n.connections.remove(n) | 
					
						
							|  |  |  |         self.assertRaises(RuntimeError, cPickle.dumps, n) | 
					
						
							| 
									
										
										
										
											2008-06-25 19:24:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_issue3179(self): | 
					
						
							| 
									
										
										
										
											2008-06-30 01:10:55 +00:00
										 |  |  |         # Safe test, because I broke this case when fixing the | 
					
						
							|  |  |  |         # behaviour for the previous test. | 
					
						
							| 
									
										
										
										
											2008-06-25 19:24:53 +00:00
										 |  |  |         res=[] | 
					
						
							|  |  |  |         for x in range(1,2000): | 
					
						
							|  |  |  |             res.append(dict(doc=x, similar=[])) | 
					
						
							|  |  |  |         cPickle.dumps(res) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-22 23:19:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-19 16:42:15 +00:00
										 |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     test_support.run_unittest( | 
					
						
							|  |  |  |         cPickleTests, | 
					
						
							|  |  |  |         cPicklePicklerTests, | 
					
						
							|  |  |  |         cPickleListPicklerTests, | 
					
						
							| 
									
										
										
										
											2008-06-22 23:19:14 +00:00
										 |  |  |         cPickleFastPicklerTests, | 
					
						
							|  |  |  |         cPickleDeepRecursive, | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2001-12-19 16:42:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-15 21:38:56 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2001-12-19 16:42:15 +00:00
										 |  |  |     test_main() |