I_getiter(): Function for the tp_iter slot of Itype so that

cStringIO's can participate in the iterator protocol.

Fill the Itype.tp_iter slot with I_getiter()
This commit is contained in:
Barry Warsaw 2001-09-22 04:36:49 +00:00
parent bdefa0b3de
commit 3e8be722d9
1 changed files with 45 additions and 20 deletions

View File

@ -653,6 +653,24 @@ I_getattr(Iobject *self, char *name) {
return Py_FindMethod(I_methods, (PyObject *)self, name);
}
static PyObject *
I_getiter(Iobject *self)
{
PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
"readline");
PyObject *emptystring = PyString_FromString("");
PyObject *iter = NULL;
if (!myreadline || !emptystring)
goto finally;
iter = PyCallIter_New(myreadline, emptystring);
finally:
Py_XDECREF(myreadline);
Py_XDECREF(emptystring);
return iter;
}
static char Itype__doc__[] =
"Simple type for treating strings as input file streams"
;
@ -676,10 +694,17 @@ static PyTypeObject Itype = {
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Itype__doc__ /* Documentation string */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
Itype__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)I_getiter, /* tp_iter */
0, /* tp_iternext */
};
static PyObject *