8000 bpo-45018: Fix rangeiter_reduce in rangeobject.c by chilaxan · Pull Request #27938 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45018: Fix rangeiter_reduce in rangeobject.c #27938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions Lib/test/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,27 +374,46 @@ def test_pickling(self):
list(r))

def test_iterator_pickling(self):
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
(13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)]
testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), (13, 21, 3),
(-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 t in testcases:
it = itorg = iter(range(*t))
data = list(range(*t))
with self.subTest(proto=proto, t=t):
it = itorg = iter(range(*t))
data = list(range(*t))

d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(type(itorg), type(it))
self.assertEqual(list(it), data)

it = pickle.loads(d)
try:
next(it)
except StopIteration:
continue
d = pickle.dumps(it, proto)
it = pickle.loads(d)
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 = itorg = iter(range(2**32 + 2))
data = list(range(2**32 + 2)[2**32:])

d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(type(itorg), type(it))
self.assertEqual(list(it), data)

it = pickle.loads(d)
try:
next(it)
except StopIteration:
continue
it.__setstate__(2**32 + 1) # undocumented way to set r->index
d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(list(it), data[1:])


def test_exhausted_iterator_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
r = range(2**65, 2**65+2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pickling of range iterators that iterated for over 2**32 times.
2 changes: 1 addition & 1 deletion Objects/rangeobject.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored))
if (range == NULL)
goto err;
/* 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);
err:
Py_XDECREF(start);
Expand Down
0