diff options
author | Raymond Hettinger <python@rcn.com> | 2004-04-12 18:10:01 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-04-12 18:10:01 (GMT) |
commit | 7892b1c651d72a5bd08372f40309dec08a7065f0 (patch) | |
tree | 7ae71a1e81651c4fa7f786ebfbdbc8364a41730e /Objects | |
parent | 45d0b5cc44ffb6227a2379a39b00d480f253edd5 (diff) | |
download | cpython-7892b1c651d72a5bd08372f40309dec08a7065f0.zip cpython-7892b1c651d72a5bd08372f40309dec08a7065f0.tar.gz cpython-7892b1c651d72a5bd08372f40309dec08a7065f0.tar.bz2 |
* Add unittests for iterators that report their length
* Document the differences between them
* Fix corner cases covered by the unittests
* Use Py_RETURN_NONE where possible for dictionaries
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 14 | ||||
-rw-r--r-- | Objects/enumobject.c | 13 | ||||
-rw-r--r-- | Objects/iterobject.c | 12 |
3 files changed, 29 insertions, 10 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 0f2a271..84cf482 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1088,10 +1088,9 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methnam static PyObject * dict_update(PyObject *self, PyObject *args, PyObject *kwds) { - if (dict_update_common(self, args, kwds, "update") == -1) - return NULL; - Py_INCREF(Py_None); - return Py_None; + if (dict_update_common(self, args, kwds, "update") != -1) + Py_RETURN_NONE; + return NULL; } /* Update unconditionally replaces existing items. @@ -1593,8 +1592,7 @@ static PyObject * dict_clear(register dictobject *mp) { PyDict_Clear((PyObject *)mp); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2050,7 +2048,9 @@ dictiter_dealloc(dictiterobject *di) static int dictiter_len(dictiterobject *di) { - return di->len; + if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) + return di->len; + return 0; } static PySequenceMethods dictiter_as_sequence = { diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 28719a9..549fc9f 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -225,6 +225,9 @@ reversed_next(reversedobject *ro) ro->index--; return item; } + if (PyErr_ExceptionMatches(PyExc_IndexError) || + PyErr_ExceptionMatches(PyExc_StopIteration)) + PyErr_Clear(); } ro->index = -1; if (ro->seq != NULL) { @@ -242,7 +245,15 @@ PyDoc_STRVAR(reversed_doc, static int reversed_len(reversedobject *ro) { - return ro->index + 1; + int position, seqsize; + + if (ro->seq == NULL) + return 0; + seqsize = PySequence_Size(ro->seq); + if (seqsize == -1) + return -1; + position = ro->index + 1; + return (seqsize < position) ? 0 : position; } static PySequenceMethods reversed_as_sequence = { diff --git a/Objects/iterobject.c b/Objects/iterobject.c index a407dd5..25e4e11 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -74,8 +74,16 @@ iter_iternext(PyObject *iterator) static int iter_len(seqiterobject *it) { - if (it->it_seq) - return PyObject_Size(it->it_seq) - it->it_index; + int seqsize, len; + + if (it->it_seq) { + seqsize = PySequence_Size(it->it_seq); + if (seqsize == -1) + return -1; + len = seqsize - it->it_index; + if (len >= 0) + return len; + } return 0; } |