summaryrefslogtreecommitdiffstats
path: root/Objects/enumobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-07-16 21:02:42 (GMT)
committerGuido van Rossum <guido@python.org>2002-07-16 21:02:42 (GMT)
commitca5ed5b8753e71c15e1acbd753c4fd65cd3813a0 (patch)
treefba1c9406bece420751c24d73a7582f7d25a6029 /Objects/enumobject.c
parent86d593e110f61800069b2d488d5530477a3d8054 (diff)
downloadcpython-ca5ed5b8753e71c15e1acbd753c4fd65cd3813a0.zip
cpython-ca5ed5b8753e71c15e1acbd753c4fd65cd3813a0.tar.gz
cpython-ca5ed5b8753e71c15e1acbd753c4fd65cd3813a0.tar.bz2
Remove the next() method -- one is supplied automatically by
PyType_Ready() because the tp_iternext slot is set (fortunately, because using the tp_iternext implementation for the the next() implementation is buggy). Also changed the allocation order in enum_next() so that the underlying iterator is only moved ahead when we have successfully allocated the result tuple and index.
Diffstat (limited to 'Objects/enumobject.c')
-rw-r--r--Objects/enumobject.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 148ac3c..f69a002 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -54,25 +54,26 @@ enum_next(enumobject *en)
{
PyObject *result;
PyObject *next_index;
+ PyObject *next_item;
- PyObject *next_item = PyIter_Next(en->en_sit);
- if (next_item == NULL)
+ result = PyTuple_New(2);
+ if (result == NULL)
return NULL;
- result = PyTuple_New(2);
- if (result == NULL) {
- Py_DECREF(next_item);
+ next_index = PyInt_FromLong(en->en_index);
+ if (next_index == NULL) {
+ Py_DECREF(result);
return NULL;
}
+ PyTuple_SET_ITEM(result, 0, next_index);
- next_index = PyInt_FromLong(en->en_index++);
- if (next_index == NULL) {
- Py_DECREF(next_item);
+ next_item = PyIter_Next(en->en_sit);
+ if (next_item == NULL) {
Py_DECREF(result);
return NULL;
}
- PyTuple_SET_ITEM(result, 0, next_index);
+ en->en_index++;
PyTuple_SET_ITEM(result, 1, next_item);
return result;
}
@@ -84,12 +85,6 @@ enum_getiter(PyObject *en)
return en;
}
-static PyMethodDef enum_methods[] = {
- {"next", (PyCFunction)enum_next, METH_NOARGS,
- "return the next (index, value) pair, or raise StopIteration"},
- {NULL, NULL} /* sentinel */
-};
-
PyDoc_STRVAR(enum_doc,
"enumerate(iterable) -> create an enumerating-iterator");
@@ -124,7 +119,7 @@ PyTypeObject PyEnum_Type = {
0, /* tp_weaklistoffset */
(getiterfunc)enum_getiter, /* tp_iter */
(iternextfunc)enum_next, /* tp_iternext */
- enum_methods, /* tp_methods */
+ 0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */