Index: Lib/test/pickletester.py =================================================================== --- Lib/test/pickletester.py (revision 72906) +++ Lib/test/pickletester.py (working copy) @@ -463,6 +463,16 @@ self.assertEqual(len(x), 1) self.assert_(x is x[0]) + def test_recursive_tuple(self): + t = ([],) + t[0].append(t) + for proto in protocols: + s = self.dumps(t, proto) + x = self.loads(s) + self.assertEqual(len(x), 1) + self.assertEqual(len(x[0]), 1) + self.assert_(x is x[0][0]) + def test_recursive_dict(self): d = {} d[1] = d Index: Lib/test/test_cpickle.py =================================================================== --- Lib/test/test_cpickle.py (revision 72906) +++ Lib/test/test_cpickle.py (working copy) @@ -65,6 +65,11 @@ AbstractPickleTests.test_recursive_list, self) + def test_recursive_tuple(self): + self.assertRaises(ValueError, + AbstractPickleTests.test_recursive_tuple, + self) + def test_recursive_inst(self): self.assertRaises(ValueError, AbstractPickleTests.test_recursive_inst, Index: Modules/cPickle.c =================================================================== --- Modules/cPickle.c (revision 72906) +++ Modules/cPickle.c (working copy) @@ -4017,25 +4017,24 @@ static int load_pop(Unpicklerobject *self) { - int len; + int len = self->stack->length; - if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); - /* Note that we split the (pickle.py) stack into two stacks, an object stack and a mark stack. We have to be clever and pop the right one. We do this by looking at the top of the - mark stack. + mark stack first, and only signalling a stack underflow if + the object stack is empty and the mark stack doesn't match + our expectations. */ - - if ((self->num_marks > 0) && - (self->marks[self->num_marks - 1] == len)) + if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { self->num_marks--; - else { + } else if (len >= 0) { len--; Py_DECREF(self->stack->data[len]); - self->stack->length=len; + self->stack->length = len; + } else { + return stackUnderflow(); } - return 0; }