]> granicus.if.org Git - python/commitdiff
Fix pickling of rangeiter. rangeiter_setstate would not allow setting it
authorKristján Valur Jónsson <kristjan@ccpgames.com>
Tue, 4 Mar 2014 23:19:24 +0000 (23:19 +0000)
committerKristján Valur Jónsson <kristjan@ccpgames.com>
Tue, 4 Mar 2014 23:19:24 +0000 (23:19 +0000)
to the exhausted state.

Lib/test/test_range.py
Objects/rangeobject.c

index 2a13bfeabd0cc1f2c7bf2176f6d7064581c6b874..063f320bfe6cd71b3bc8fcbf82dd9cf652e43e70 100644 (file)
@@ -379,6 +379,18 @@ class RangeTest(unittest.TestCase):
                 it = pickle.loads(d)
                 self.assertEqual(list(it), data[1:])
 
+    def test_exhausted_iterator_pickling(self):
+        r = range(20)
+        i = iter(r)
+        while True:
+            r = next(i)
+            if r == 19:
+                break
+        d = pickle.dumps(i)
+        i2 = pickle.loads(d)
+        self.assertEqual(list(i), [])
+        self.assertEqual(list(i2), [])
+
     def test_odd_bug(self):
         # This used to raise a "SystemError: NULL result without error"
         # because the range validation step was eating the exception
index d790ca8f476d3bc94c9ac7b1b2593b300007cb94..4b218b89617f3cddf2616019fd80d8b28be66d28 100644 (file)
@@ -1000,7 +1000,7 @@ rangeiter_setstate(rangeiterobject *r, PyObject *state)
     long index = PyLong_AsLong(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0 || index >= r->len) {
+    if (index < 0 || index > r->len) {
         PyErr_SetString(PyExc_ValueError, "index out of range");
         return NULL;
     }