Issue 2582: Fix pickling of xrange objects.

This commit is contained in:
Alexandre Vassalotti 2008-06-10 03:34:53 +00:00
parent 5c4d3d0e4c
commit 1f2f61a78f
2 changed files with 21 additions and 0 deletions

View File

@ -57,6 +57,16 @@ class XrangeTest(unittest.TestCase):
self.assertEqual(len(r), sys.maxint)
self.assertRaises(OverflowError, xrange, -sys.maxint-1, sys.maxint, 2)
def test_getnewargs(self):
def test(*args):
r = xrange(*args)
return list(r) == list(xrange(*r.__getnewargs__()))
tests = [(13,), (0, 11), (-22, 10), (20, 3, -1),
(13, 21, 3), (-2, 2, 2)]
for t in tests:
self.assert_(test(*t),
"xrange.__getnewargs__() failed with %r" % (t,))
def test_main():
test.test_support.run_unittest(XrangeTest)

View File

@ -129,6 +129,16 @@ range_repr(rangeobject *r)
return rtn;
}
/* Pickling support */
static PyObject *
range_getnewargs(rangeobject *r)
{
return Py_BuildValue("(iii)",
r->start,
r->start + r->len * r->step,
r->step);
}
static PySequenceMethods range_as_sequence = {
(lenfunc)range_length, /* sq_length */
0, /* sq_concat */
@ -145,6 +155,7 @@ PyDoc_STRVAR(reverse_doc,
static PyMethodDef range_methods[] = {
{"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
{"__getnewargs__", (PyCFunction)range_getnewargs, METH_NOARGS},
{NULL, NULL} /* sentinel */
};