[Python-checkins] cpython (merge 3.3 -> default): Make the various iterators' "setstate" sliently and consistently clip the

kristjan.jonsson python-checkins at python.org
Wed Mar 5 16:24:31 CET 2014


http://hg.python.org/cpython/rev/08cf35d4ef49
changeset:   89478:08cf35d4ef49
parent:      89476:23d9daed4b14
parent:      89477:3b2c28061184
user:        Kristján Valur Jónsson <sweskman at gmail.com>
date:        Wed Mar 05 15:23:07 2014 +0000
summary:
  Make the various iterators' "setstate" sliently and consistently clip the
index.  This avoids the possibility of setting an iterator to an invalid
state.

files:
  Lib/test/test_range.py    |  12 ++++++++++
  Modules/arraymodule.c     |   2 +
  Objects/bytearrayobject.c |  10 ++++++--
  Objects/bytesobject.c     |  10 ++++++--
  Objects/listobject.c      |   2 +
  Objects/rangeobject.c     |  31 +++++++++++++++++++++++---
  Objects/tupleobject.c     |   4 +-
  Objects/unicodeobject.c   |  10 ++++++--
  8 files changed, 66 insertions(+), 15 deletions(-)


diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -381,6 +381,18 @@
                 self.assertEqual(list(it), data[1:])
 
     def test_exhausted_iterator_pickling(self):
+        r = range(2**65, 2**65+2)
+        i = iter(r)
+        while True:
+            r = next(i)
+            if r == 2**65+1:
+                break
+        d = pickle.dumps(i)
+        i2 = pickle.loads(d)
+        self.assertEqual(list(i), [])
+        self.assertEqual(list(i2), [])
+
+    def test_large_exhausted_iterator_pickling(self):
         r = range(20)
         i = iter(r)
         while True:
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2838,6 +2838,8 @@
         return NULL;
     if (index < 0)
         index = 0;
+    else if (index > Py_SIZE(it->ao))
+        index = Py_SIZE(it->ao); /* iterator exhausted */
     it->index = index;
     Py_RETURN_NONE;
 }
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -3025,9 +3025,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyByteArray_GET_SIZE(it->it_seq))
+            index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2937,9 +2937,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyBytes_GET_SIZE(it->it_seq))
+            index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 
diff --git a/Objects/listobject.c b/Objects/listobject.c
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2811,6 +2811,8 @@
     if (it->it_seq != NULL) {
         if (index < 0)
             index = 0;
+        else if (index > PyList_GET_SIZE(it->it_seq))
+            index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
         it->it_index = index;
     }
     Py_RETURN_NONE;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -807,10 +807,11 @@
     long index = PyLong_AsLong(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0 || index > r->len) {
-        PyErr_SetString(PyExc_ValueError, "index out of range");
-        return NULL;
-    }
+    /* silently clip the index value */
+    if (index < 0)
+        index = 0;
+    else if (index > r->len)
+        index = r->len; /* exhausted iterator */
     r->index = index;
     Py_RETURN_NONE;
 }
@@ -985,6 +986,28 @@
 static PyObject *
 longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
 {
+    int cmp;
+   
+    /* clip the value */
+    PyObject *zero = PyLong_FromLong(0);
+    if (zero == NULL)
+        return NULL;
+    cmp = PyObject_RichCompareBool(state, zero, Py_LT);
+    if (cmp > 0) {
+        Py_CLEAR(r->index);
+        r->index = zero;
+        Py_RETURN_NONE;
+    }
+    Py_DECREF(zero);
+    if (cmp < 0)
+        return NULL;
+
+    cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+    if (cmp < 0)
+        return NULL;
+    if (cmp > 0)
+        state = r->len;
+    
     Py_CLEAR(r->index);
     r->index = state;
     Py_INCREF(r->index);
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -1011,8 +1011,8 @@
     if (it->it_seq != NULL) {
         if (index < 0)
             index = 0;
-        else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
-            index = PyTuple_GET_SIZE(it->it_seq);
+        else if (index > PyTuple_GET_SIZE(it->it_seq))
+            index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
         it->it_index = index;
     }
     Py_RETURN_NONE;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -15196,9 +15196,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyUnicode_GET_LENGTH(it->it_seq))
+            index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list