diff options
author | Raymond Hettinger <python@rcn.com> | 2015-07-07 22:29:24 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-07-07 22:29:24 (GMT) |
commit | 9632a7d735071b56f2361f09fde559b3b5edef69 (patch) | |
tree | 3356ccc8401fa44ef16a2329a954ff55aa9ecb90 /Objects | |
parent | 9f20bdb321b683aa1c4a9c8b37e860557fedc170 (diff) | |
download | cpython-9632a7d735071b56f2361f09fde559b3b5edef69.zip cpython-9632a7d735071b56f2361f09fde559b3b5edef69.tar.gz cpython-9632a7d735071b56f2361f09fde559b3b5edef69.tar.bz2 |
Issue 24581: Revert c9782a9ac031 pending a stronger test for mutation during iteration.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/setobject.c | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 12f82f3..7c91c38 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -766,8 +766,8 @@ typedef struct { PyObject_HEAD PySetObject *si_set; /* Set to NULL when iterator is exhausted */ Py_ssize_t si_used; + Py_ssize_t si_pos; Py_ssize_t len; - setentry *entry; } setiterobject; static void @@ -845,6 +845,8 @@ static PyMethodDef setiter_methods[] = { static PyObject *setiter_iternext(setiterobject *si) { + PyObject *key; + Py_ssize_t i, mask; setentry *entry; PySetObject *so = si->si_set; @@ -858,18 +860,25 @@ static PyObject *setiter_iternext(setiterobject *si) si->si_used = -1; /* Make this state sticky */ return NULL; } - if (si->len <= 0) { - Py_DECREF(so); - si->si_set = NULL; - return NULL; - } - entry = si->entry; - while (entry->key == NULL || entry->key == dummy) - entry++; + + i = si->si_pos; + assert(i>=0); + entry = so->table; + mask = so->mask; + while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) + i++; + si->si_pos = i+1; + if (i > mask) + goto fail; si->len--; - si->entry = entry + 1; - Py_INCREF(entry->key); - return entry->key; + key = entry[i].key; + Py_INCREF(key); + return key; + +fail: + Py_DECREF(so); + si->si_set = NULL; + return NULL; } PyTypeObject PySetIter_Type = { @@ -914,8 +923,8 @@ set_iter(PySetObject *so) Py_INCREF(so); si->si_set = so; si->si_used = so->used; + si->si_pos = 0; si->len = so->used; - si->entry = so->table; _PyObject_GC_TRACK(si); return (PyObject *)si; } |