From 01f94bda38b8f9956cfa1b227f2ebdb8ea2dfdd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Wed, 8 May 2002 08:44:21 +0000 Subject: Patch #552433: Special-case tuples. Avoid sub-type checking for lists. Avoid checks for negative indices and duplicate checks for support of the sequence protocol. --- Objects/iterobject.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'Objects/iterobject.c') diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 789eb6c689..de9f2f9e5d 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -12,6 +12,11 @@ PyObject * PySeqIter_New(PyObject *seq) { seqiterobject *it; + + if (!PySequence_Check(seq)) { + PyErr_BadInternalCall(); + return NULL; + } it = PyObject_GC_New(seqiterobject, &PySeqIter_Type); if (it == NULL) return NULL; @@ -63,7 +68,7 @@ iter_iternext(PyObject *iterator) it = (seqiterobject *)iterator; seq = it->it_seq; - if (PyList_Check(seq)) { + if (PyList_CheckExact(seq)) { PyObject *item; if (it->it_index >= PyList_GET_SIZE(seq)) { return NULL; @@ -73,8 +78,19 @@ iter_iternext(PyObject *iterator) Py_INCREF(item); return item; } + if (PyTuple_CheckExact(seq)) { + PyObject *item; + if (it->it_index >= PyTuple_GET_SIZE(seq)) { + return NULL; + } + item = PyTuple_GET_ITEM(seq, it->it_index); + it->it_index++; + Py_INCREF(item); + return item; + } else { - PyObject *result = PySequence_GetItem(seq, it->it_index++); + PyObject *result = PySequence_ITEM(seq, it->it_index); + it->it_index++; if (result != NULL) { return result; } -- cgit v1.2.1