Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ca688e

Browse files
author
Kristján Valur Jónsson
committed
Fix pickling of rangeiter. rangeiter_setstate would not allow setting it
to the exhausted state.
1 parent 682ea5f commit 4ca688e

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_range.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,18 @@ def test_iterator_pickling(self):
379379
it = pickle.loads(d)
380380
self.assertEqual(list(it), data[1:])
381381

382+
def test_exhausted_iterator_pickling(self):
383+
r = range(20)
384+
i = iter(r)
385+
while True:
386+
r = next(i)
387+
if r == 19:
388+
break
389+
d = pickle.dumps(i)
390+
i2 = pickle.loads(d)
391+
self.assertEqual(list(i), [])
392+
self.assertEqual(list(i2), [])
393+
382394
def test_odd_bug(self):
383395
# This used to raise a "SystemError: NULL result without error"
384396
# because the range validation step was eating the exception

Objects/rangeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ rangeiter_setstate(rangeiterobject *r, PyObject *state)
10001000
long index = PyLong_AsLong(state);
10011001
if (index == -1 && PyErr_Occurred())
10021002
return NULL;
1003-
if (index < 0 || index >= r->len) {
1003+
if (index < 0 || index > r->len) {
10041004
PyErr_SetString(PyExc_ValueError, "index out of range");
10051005
return NULL;
10061006
}

0 commit comments

Comments
 (0)