diff -r 5f0465d0e91e Lib/test/test_xrange.py --- a/Lib/test/test_xrange.py Tue Sep 25 02:48:21 2012 -0700 +++ b/Lib/test/test_xrange.py Tue Sep 25 14:10:46 2012 +0100 @@ -46,6 +46,31 @@ self.fail('{}: wrong element at position {};' 'expected {}, got {}'.format(test_id, i, y, x)) + def assert_xranges_equivalent(self, x, y): + # Check that two xrange objects are equivalent, in the sense of the + # associated sequences being the same. We want to use this for large + # xrange objects, so instead of converting to lists and comparing + # directly we do a number of indirect checks. + if len(x) != len(y): + self.fail('{} and {} have different ' + 'lengths: {} and {} '.format(x, y, len(x), len(y))) + # Provided that there's at least one element, + # check first and last elements match. + if len(x) >= 1: + if x[0] != y[0]: + self.fail('{} and {} have different initial ' + 'elements: {} and {} '.format(x, y, x[0], y[0])) + if x[-1] != y[-1]: + self.fail('{} and {} have different final ' + 'elements: {} and {} '.format(x, y, x[-1], y[-1])) + # If there are at least two elements, check the step. + if len(x) >= 2: + x_step = x[1] - x[0] + y_step = y[1] - y[0] + if x_step != y_step: + self.fail('{} and {} have different step: ' + '{} and {} '.format(x, y, x_step, y_step)) + def test_xrange(self): self.assertEqual(list(xrange(3)), [0, 1, 2]) self.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4]) @@ -104,6 +129,60 @@ self.assertEqual(list(pickle.loads(pickle.dumps(r, proto))), list(r)) + M = min(sys.maxint, sys.maxsize) + large_testcases = testcases + [ + (0, M, 1), + (M, 0, -1), + (0, M, M - 1), + (M // 2, M, 1), + (0, -M, -1), + (0, -M, 1 - M), + (-M, M, 2), + (-M, M, 1024), + (-M, M, 10585), + (M, -M, -2), + (M, -M, -1024), + (M, -M, -10585), + ] + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + for t in large_testcases: + r = xrange(*t) + r_out = pickle.loads(pickle.dumps(r, proto)) + self.assert_xranges_equivalent(r_out, r) + + def test_repr(self): + # Check that repr of an xrange is a valid representation + # of that xrange. + + # Valid xranges have at most min(sys.maxint, sys.maxsize) elements. + M = min(sys.maxint, sys.maxsize) + + testcases = [ + (13,), + (0, 11), + (-22, 10), + (20, 3, -1), + (13, 21, 3), + (-2, 2, 2), + (0, M, 1), + (M, 0, -1), + (0, M, M - 1), + (M // 2, M, 1), + (0, -M, -1), + (0, -M, 1 - M), + (-M, M, 2), + (-M, M, 1024), + (-M, M, 10585), + (M, -M, -2), + (M, -M, -1024), + (M, -M, -10585), + ] + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + for t in testcases: + r = xrange(*t) + r_out = eval(repr(r)) + self.assert_xranges_equivalent(r, r_out) + def test_range_iterators(self): # see issue 7298 limits = [base + jiggle diff -r 5f0465d0e91e Objects/rangeobject.c --- a/Objects/rangeobject.c Tue Sep 25 02:48:21 2012 -0700 +++ b/Objects/rangeobject.c Tue Sep 25 14:10:46 2012 +0100 @@ -105,6 +105,29 @@ return (Py_ssize_t)(r->len); } +/* Stop value suitable for use in repr and reduce. + * + * Computes start + len * step, clipped to the range [LONG_MIN, LONG_MAX]. + */ +static long +range_stop(rangeobject *r) +{ + long last; + + if (r->len == 0) + return r->start; + + /* The tricky bit is avoiding overflow. Here we first compute the last + entry in the xrange (start + (len - 1) * step), which is guaranteed to + lie within the range of a long, and then add step to it. */ + last = (long)(r->start + (unsigned long)(r->len - 1) * r->step); + if (r->step > 0) + return last > LONG_MAX - r->step ? LONG_MAX : last + r->step; + else + return last < LONG_MIN - r->step ? LONG_MIN : last + r->step; +} + + static PyObject * range_repr(rangeobject *r) { @@ -112,17 +135,17 @@ if (r->start == 0 && r->step == 1) rtn = PyString_FromFormat("xrange(%ld)", - r->start + r->len * r->step); + range_stop(r)); else if (r->step == 1) rtn = PyString_FromFormat("xrange(%ld, %ld)", r->start, - r->start + r->len * r->step); + range_stop(r)); else rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)", r->start, - r->start + r->len * r->step, + range_stop(r), r->step); return rtn; } @@ -131,9 +154,9 @@ static PyObject * range_reduce(rangeobject *r, PyObject *args) { - return Py_BuildValue("(O(iii))", Py_TYPE(r), + return Py_BuildValue("(O(lll))", Py_TYPE(r), r->start, - r->start + r->len * r->step, + range_stop(r), r->step); }