mirror of https://github.com/python/cpython
Issue 2582: Fix pickling of xrange objects.
This commit is contained in:
parent
5c4d3d0e4c
commit
1f2f61a78f
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 */
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue