Skip to content

Commit da87e1d

Browse files
author
gvanrossum
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@27643 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 0a51716 commit da87e1d

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

Objects/enumobject.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,26 @@ enum_next(enumobject *en)
5454
{
5555
PyObject *result;
5656
PyObject *next_index;
57+
PyObject *next_item;
5758

58-
PyObject *next_item = PyIter_Next(en->en_sit);
59-
if (next_item == NULL)
59+
result = PyTuple_New(2);
60+
if (result == NULL)
6061
return NULL;
6162

62-
result = PyTuple_New(2);
63-
if (result == NULL) {
64-
Py_DECREF(next_item);
63+
next_index = PyInt_FromLong(en->en_index);
64+
if (next_index == NULL) {
65+
Py_DECREF(result);
6566
return NULL;
6667
}
68+
PyTuple_SET_ITEM(result, 0, next_index);
6769

68-
next_index = PyInt_FromLong(en->en_index++);
69-
if (next_index == NULL) {
70-
Py_DECREF(next_item);
70+
next_item = PyIter_Next(en->en_sit);
71+
if (next_item == NULL) {
7172
Py_DECREF(result);
7273
return NULL;
7374
}
7475

75-
PyTuple_SET_ITEM(result, 0, next_index);
76+
en->en_index++;
7677
PyTuple_SET_ITEM(result, 1, next_item);
7778
return result;
7879
}
@@ -84,12 +85,6 @@ enum_getiter(PyObject *en)
8485
return en;
8586
}
8687

87-
static PyMethodDef enum_methods[] = {
88-
{"next", (PyCFunction)enum_next, METH_NOARGS,
89-
"return the next (index, value) pair, or raise StopIteration"},
90-
{NULL, NULL} /* sentinel */
91-
};
92-
9388
PyDoc_STRVAR(enum_doc,
9489
"enumerate(iterable) -> create an enumerating-iterator");
9590

@@ -124,7 +119,7 @@ PyTypeObject PyEnum_Type = {
124119
0, /* tp_weaklistoffset */
125120
(getiterfunc)enum_getiter, /* tp_iter */
126121
(iternextfunc)enum_next, /* tp_iternext */
127-
enum_methods, /* tp_methods */
122+
0, /* tp_methods */
128123
0, /* tp_members */
129124
0, /* tp_getset */
130125
0, /* tp_base */

0 commit comments

Comments
 (0)