From 1f2f61a78f80933a3e703df1ab08f14e70ea87d5 Mon Sep 17 00:00:00 2001 From: Alexandre Vassalotti Date: Tue, 10 Jun 2008 03:34:53 +0000 Subject: [PATCH] Issue 2582: Fix pickling of xrange objects. --- Lib/test/test_xrange.py | 10 ++++++++++ Objects/rangeobject.c | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/Lib/test/test_xrange.py b/Lib/test/test_xrange.py index eafb7fad569..ab68c3621c7 100644 --- a/Lib/test/test_xrange.py +++ b/Lib/test/test_xrange.py @@ -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) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index da4356b9f16..bfc038ed2db 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -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 */ };