]> granicus.if.org Git - python/commitdiff
Fixing the problem stated in issue 2702 with the patch submitted
authorFacundo Batista <facundobatista@gmail.com>
Sun, 22 Jun 2008 23:19:14 +0000 (23:19 +0000)
committerFacundo Batista <facundobatista@gmail.com>
Sun, 22 Jun 2008 23:19:14 +0000 (23:19 +0000)
in the issue 3165. Now cPickle does not fails with uncontrolled
behaviour when pickling into a very deep nested structure.

Lib/test/test_cpickle.py
Modules/cPickle.c

index d6e703a7fa5cebdcba8724fe0b834a0655987a14..7f6c35aeffc020843863bdf5b376d4a9b503b6d2 100644 (file)
@@ -1,4 +1,4 @@
-import cPickle
+import cPickle, unittest
 from cStringIO import StringIO
 from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
 from test import test_support
@@ -90,12 +90,28 @@ class cPickleFastPicklerTests(AbstractPickleTests):
         b = self.loads(self.dumps(a))
         self.assertEqual(a, b)
 
+class Node(object):
+    pass
+
+class cPickleDeepRecursive(unittest.TestCase):
+    '''Issue 2702. This should raise a RecursionLimit but in some
+    platforms (FreeBSD, win32) sometimes raises KeyError instead,
+    or just silently terminates the interpreter (=crashes).
+    '''
+    def test_deep_recursive(self):
+        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)
+
 def test_main():
     test_support.run_unittest(
         cPickleTests,
         cPicklePicklerTests,
         cPickleListPicklerTests,
-        cPickleFastPicklerTests
+        cPickleFastPicklerTests,
+        cPickleDeepRecursive,
     )
 
 if __name__ == "__main__":
index afa75fd5fe36ff2931e91cc26340c78a429f411c..e4bb7a183bd98fe410156853f61e4d86a5df1bd5 100644 (file)
@@ -1519,6 +1519,7 @@ batch_list(Picklerobject *self, PyObject *iter)
        PyObject *obj;
        PyObject *slice[BATCHSIZE];
        int i, n;
+    self->nesting++;
 
        static char append = APPEND;
        static char appends = APPENDS;
@@ -1658,6 +1659,7 @@ batch_dict(Picklerobject *self, PyObject *iter)
        PyObject *p;
        PyObject *slice[BATCHSIZE];
        int i, n;
+    self->nesting++;
 
        static char setitem = SETITEM;
        static char setitems = SETITEMS;