bpo-45018: Fix rangeiter_reduce in rangeobject.c (GH-27938)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
chilaxan 2021-08-27 06:27:19 -04:00 committed by GitHub
parent 3331fd264d
commit 94a3d2a632
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 16 deletions

View File

@ -374,10 +374,12 @@ class RangeTest(unittest.TestCase):
list(r)) list(r))
def test_iterator_pickling(self): def test_iterator_pickling(self):
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), (13, 21, 3),
(13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)] (-2, 2, 2), (2**31-3, 2**31-1), (2**33, 2**33+2),
(2**63-3, 2**63-1), (2**65, 2**65+2)]
for proto in range(pickle.HIGHEST_PROTOCOL + 1): for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for t in testcases: for t in testcases:
with self.subTest(proto=proto, t=t):
it = itorg = iter(range(*t)) it = itorg = iter(range(*t))
data = list(range(*t)) data = list(range(*t))
@ -395,6 +397,19 @@ class RangeTest(unittest.TestCase):
it = pickle.loads(d) it = pickle.loads(d)
self.assertEqual(list(it), data[1:]) self.assertEqual(list(it), data[1:])
def test_iterator_pickling_overflowing_index(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
it = iter(range(2**32 + 2))
_, _, idx = it.__reduce__()
self.assertEqual(idx, 0)
it.__setstate__(2**32 + 1) # undocumented way to set r->index
_, _, idx = it.__reduce__()
self.assertEqual(idx, 2**32 + 1)
d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(next(it), 2**32 + 1)
def test_exhausted_iterator_pickling(self): def test_exhausted_iterator_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1): for proto in range(pickle.HIGHEST_PROTOCOL + 1):
r = range(2**65, 2**65+2) r = range(2**65, 2**65+2)

View File

@ -0,0 +1 @@
Fixed pickling of range iterators that iterated for over 2**32 times.

View File

@ -813,7 +813,7 @@ rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored))
if (range == NULL) if (range == NULL)
goto err; goto err;
/* return the result */ /* return the result */
return Py_BuildValue("N(N)i", _PyEval_GetBuiltinId(&PyId_iter), return Py_BuildValue("N(N)l", _PyEval_GetBuiltinId(&PyId_iter),
range, r->index); range, r->index);
err: err:
Py_XDECREF(start); Py_XDECREF(start);