Kaydet (Commit) 602d8db2 authored tarafından Alexandre Vassalotti's avatar Alexandre Vassalotti

Added better pickling support to xrange objects.

Cleaned up the unit test.
üst 1f2f61a7
......@@ -2,6 +2,7 @@
import test.test_support, unittest
import sys
import pickle
import warnings
warnings.filterwarnings("ignore", "integer argument expected",
......@@ -57,15 +58,15 @@ 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_pickling(self):
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
(13, 21, 3), (-2, 2, 2)]
for proto in range(pickle.HIGHEST_PROTOCOL):
for t in testcases:
r = xrange(*t)
self.assertEquals(list(pickle.loads(pickle.dumps(r, proto))),
list(r))
def test_main():
test.test_support.run_unittest(XrangeTest)
......
......@@ -131,9 +131,9 @@ range_repr(rangeobject *r)
/* Pickling support */
static PyObject *
range_getnewargs(rangeobject *r)
range_reduce(rangeobject *r, PyObject *args)
{
return Py_BuildValue("(iii)",
return Py_BuildValue("(O(iii))", Py_TYPE(r),
r->start,
r->start + r->len * r->step,
r->step);
......@@ -155,7 +155,7 @@ PyDoc_STRVAR(reverse_doc,
static PyMethodDef range_methods[] = {
{"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
{"__getnewargs__", (PyCFunction)range_getnewargs, METH_NOARGS},
{"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment