mirror of
https://github.com/python/cpython.git
synced 2025-07-31 23:23:11 +00:00
Issue 2582: Fix pickling of xrange objects.
This commit is contained in:
parent
5c4d3d0e4c
commit
1f2f61a78f
2 changed files with 21 additions and 0 deletions
|
@ -57,6 +57,16 @@ class XrangeTest(unittest.TestCase):
|
||||||
self.assertEqual(len(r), sys.maxint)
|
self.assertEqual(len(r), sys.maxint)
|
||||||
self.assertRaises(OverflowError, xrange, -sys.maxint-1, sys.maxint, 2)
|
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():
|
def test_main():
|
||||||
test.test_support.run_unittest(XrangeTest)
|
test.test_support.run_unittest(XrangeTest)
|
||||||
|
|
||||||
|
|
|
@ -129,6 +129,16 @@ range_repr(rangeobject *r)
|
||||||
return rtn;
|
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 = {
|
static PySequenceMethods range_as_sequence = {
|
||||||
(lenfunc)range_length, /* sq_length */
|
(lenfunc)range_length, /* sq_length */
|
||||||
0, /* sq_concat */
|
0, /* sq_concat */
|
||||||
|
@ -145,6 +155,7 @@ PyDoc_STRVAR(reverse_doc,
|
||||||
|
|
||||||
static PyMethodDef range_methods[] = {
|
static PyMethodDef range_methods[] = {
|
||||||
{"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
|
{"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
|
||||||
|
{"__getnewargs__", (PyCFunction)range_getnewargs, METH_NOARGS},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue